Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to use Powershell in order to call a batch file on remote machines. This batch file has arguments. Here's what I have so far:

$script = "\\fileshare\script.cmd"
$server = $args[0]
$args [string]::join(',',$args[1 .. ($args.count-1)])

Invoke-Command -computername $server {$script + ' ' + $args}

After a bit of searching, I found that the Invoke-Command function runs its scriptblock in a whole new process, so you can't put variables in it (they won't get expanded). That's what the -ArgumentList tag is for. So I tried this instead...

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

That didn't work either... my batch script tells me it's not being passed any arguments. I can't find anything that explicitly says so, but it looks like the -ArgumentList parameter only works on Powershell scripts (it won't feed them to a batch script).

Any ideas how I can use Invoke-Command to call a batch file with arguments?

like image 680
Jay Spang Avatar asked Dec 17 '11 00:12

Jay Spang


People also ask

How do you pass arguments to invoke a command?

To pass the argument in the Invoke-command, you need to use -ArgumentList parameter.

How do I pass a command line argument to a batch file?

Batch parameters (Command line parameters): In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.

Can a batch file take arguments?

Batch scripts support the concept of command line arguments wherein arguments can be passed to the batch file when invoked. The arguments can be called from the batch files through the variables %1, %2, %3, and so on.

How do you call a batch file from a PowerShell script?

To run the batch commands from PowerShell, we can use the Start-Process cmdlet as earlier explained and which executes a cmd command on the server.


2 Answers

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"
like image 90
zdan Avatar answered Sep 22 '22 09:09

zdan


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

like image 33
manojlds Avatar answered Sep 23 '22 09:09

manojlds