title: How to execute script block on remote machine that uses local variable as an argument
body: <p>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:</p>
  
  <pre><code>foreach ($a in $args){
      &amp;"c:\Program Files\ChristianSteven\CRD\crd.exe" "-s schedulename=Vc_$($a)"
  }
  </code></pre>
  
  <p>I'm having problems running it remotely using: </p>
  
  <pre><code>foreach ($a in $args){     
      Invoke-Command -computername $serv -Credential $cred -ScriptBlock {param($b) &amp;"c:\Program Files\ChristianSteven\CRD\crd.exe" $b} -ArgumentList "-s schedulename=Vc_$($a)"
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>[scriptblock]::create(&lt;command&gt;)
  </code></pre>
  
  <p>I've tried many combinations and I can't get it to run. </p>