Remote background job error with Using scope modifier

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

This topic contains 2 replies, has 3 voices, and was last updated by

1 week, 4 days ago.

  • Author
  • #181806

    Topics: 5
    Replies: 274
    Points: 153
    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: 1085
    Points: 3,651
    Helping Hand
    Rank: Community Hero

    you could use Invoke-Command with -AsJob parameter.

  • #181962

    Topics: 6
    Replies: 17
    Points: 60
    Rank: Member

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

You must be logged in to reply to this topic.