How to execute script block on remote machine that uses local variable as an argument
I wo开发者_如何学编程uld like to run an application passing it arguments from a remote machine. I've got the following to work on the remote machine by running it locally:
foreach ($a in $args){
&"c:\Program Files\ChristianSteven\CRD\crd.exe" "-s schedulename=Vc_$($a)"
}
I'm having problems running it remotely using:
foreach ($a in $args){
Invoke-Command -computername $serv -Credential $cred -ScriptBlock {param($b) &"c:\Program Files\ChristianSteven\CRD\crd.exe" $b} -ArgumentList "-s schedulename=Vc_$($a)"
}
From what I've read this is to do with the variables scope and the remedy is to create the scriptblock before you pass it to the remote machine using:
[scriptblock]::create(<command>)
I've tried many combinations and I can't get it to run.
You can do it like this:
$scriptBlock = {param($a) &"c:\Program Files\ChristianSteven\CRD\crd.exe" "-s schedulename=Vc_$($a)"}
foreach ($a in $args){
Invoke-Command -computername $serv -Credential $cred -ScriptBlock $scriptBlock -ArgumentList $a
}
精彩评论