Question

I would 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.

Was it helpful?

Solution

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top