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