Using Powershell's Invoke-Command to call a batch file with arguments Using Powershell's Invoke-Command to call a batch file with arguments powershell powershell

Using Powershell's Invoke-Command to call a batch file with arguments


When you pass the argument list to the scriptblock, try to "receive them" using a PARAM directive. Like this:

Invoke-Command -computername $server {PARAM($myArg) \\fileshare\script.cmd $myArg} -ArgumentList "FirstArgument"

or you can just use the $args automatic variable:

Invoke-Command -computername $server {\\fileshare\script.cmd $args} -ArgumentList "FirstArgument"


The arguments will be passed as arguments to the scriptblock and not directly to your cmd. You have to do:

Invoke-Command {param($script,$arg1) &$script $arg1 } -computername $server -ArgumentList $script,"FirstArgument"

or

Invoke-Command {&$args[0] $args[1] } -computername $server -ArgumentList $script,"FirstArgument"

PS: I don't know what you are doing with $args [string]::join(',',$args[1 .. ($args.count-1)]), it is a syntax error