Remote background job error with Using scope modifier

Welcome Forums General PowerShell Q&A Remote background job error with Using scope modifier

Viewing 2 reply threads
  • Author
    Posts
    • #181806
      Participant
      Topics: 5
      Replies: 287
      Points: 224
      Helping Hand
      Rank: Participant

      I receive the following error when attempting to run remote jobs on remote servers with $Using scope modifier. The local scope contains a scriptblock in a variable. Examples are shown in 'help about_remote_jobs' and 'help wait-job -example' with the same syntax I use. My example and error is below.

      $server = Get-Content C:\script\serverlist.txt
      
      $session = New-PSSession -ComputerName $server
      $scriptblock = {Get-Process | Select-Object -First 5}
      Invoke-Command -Session $session -ScriptBlock {Start-Job -ScriptBlock $Using:scriptblock} 
      

      Error:  The value of the using variable '$Using:scriptblock' cannot be retrieved because it has not been set in the local session.

      The syntax below works, but I would like the remote server to execute the scriptblock and start the background job.

       Invoke-Command -Session $session -ScriptBlock $scriptblock
    • #181821
      Senior Moderator
      Topics: 8
      Replies: 1155
      Points: 4,019
      Helping Hand
      Rank: Community Hero

      you could use Invoke-Command with -AsJob parameter.

    • #181962
      Participant
      Topics: 6
      Replies: 18
      Points: 70
      Rank: Member

      Invoke-Command -Session $session -ScriptBlock {param($sb) Start-Job -ScriptBlock $sb} -ArgumentList $scriptblock

Viewing 2 reply threads
  • The topic ‘Remote background job error with Using scope modifier’ is closed to new replies.