I want to run multiple commands onmultiple client with Winrm in a powershell script with csv inputs for computers and commands
when i try it manually, like
invoke-command -ComputerName test.home.lan -Scriptblock{Start-Service WebClient}
This is working fine, but when i launch this command into the script this dont work, and i dont understand why ? where is my mistake ?
I'm in a domain and launch the script on the ADS, i've try with/without credentials.
Script :
#Init
$file = Import-Csv -Path ./computer.csv -Delimiter ";"
$commands = Import-Csv -Path ./command.csv -Delimiter ";"
Function RunRemote {
param( $fqdn, $command )
$scriptblock =
{
$command
}
Write-Host "Run remote Connection a FQDN" $fqdn
Write-Host "Run remote CMD" $command
Invoke-Command -ComputerName $fqdn -ScriptBlock $scriptblock
}
#Main Loop
foreach ($computer in $file)
{
foreach ($command in $commands)
{
RunRemote($computer.fqdn, $command.command)
}
}
Command.csv :
command;
start-service webclient;
Computer.csv
computer;
test.home.lan;
test2.home.lan;
No Error, but the command not work on the target, and no output of the command