0

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

AdminSec
  • 1
  • 1
  • In PowerShell, functions are invoked _like shell commands_ - `foo arg1 arg2` - _not_ like C# methods - `foo(arg1, arg2)`; see [`Get-Help about_Parsing`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing). If you use `,` to separate arguments, you'll construct an _array_ that a function sees as a _single_ argument. To prevent accidental use of method syntax, use [`Set-StrictMode -Version 2`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode) or higher, but note its other effects. – mklement0 Jun 12 '20 at 22:20
  • Also, use `$scriptblock = [scriptblock]::Create($command)` to create your script block. – mklement0 Jun 12 '20 at 22:24

0 Answers0