1

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.

dads
  • 17
  • 1
  • 5

1 Answers1

1

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
}
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Thanks but that never worked. I can see the arg is correctly evaluated to the string "s001" and the command runs without any errors but the program doesn't run. – dads Oct 12 '11 at 20:27
  • @dads - then make sure it does run in the other local command? I am not sure what you mean doesn't run, because I, of course, don't have that exe. – manojlds Oct 12 '11 at 20:33