I could not get this to work and eventually just copied/pasted the text directly from the PowerShell Get-Help information...still didn't work. So, I am stumped. Here is the code I am using, taken from Get-Help Receive-Job -full (Example 4):
$s = new-pssession -computername 'MyRemoteServer'
$j = invoke-command -session $s -scriptblock {start-job -scriptblock {get-eventlog -logname system}}
$results = Invoke-Command -Session $s -ScriptBlock {Receive-Job -Job $Using:j}
All I get back is this:
Cannot bind argument to parameter 'Job' because it is null.
+ CategoryInfo : InvalidData: (:) [Receive-Job], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ReceiveJobCommand
Getting results from a remote job
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
-
- Posts: 107
- Last visit: Thu Apr 29, 2021 8:47 am
-
- Posts: 107
- Last visit: Thu Apr 29, 2021 8:47 am
Getting results from a remote job
I made some changes and now it works.
The variable $s is the same. For $j I replaced 'start-job' with 'invoke-expression' and explicitly defined it as a background job. For the $results I dumped everything but receive-job.
New (working) code:
$s = new-pssession -computername 'MyRemoteServer'
$j = invoke-command -session $s -scriptblock {invoke-expression -command "get-eventlog -logname system"} -AsJob
$results = Receive-Job -Job $j
I don't know what the heck the official Get-Help was trying to do, but this seems more elegant and, as an added bonus, actually works.
The variable $s is the same. For $j I replaced 'start-job' with 'invoke-expression' and explicitly defined it as a background job. For the $results I dumped everything but receive-job.
New (working) code:
$s = new-pssession -computername 'MyRemoteServer'
$j = invoke-command -session $s -scriptblock {invoke-expression -command "get-eventlog -logname system"} -AsJob
$results = Receive-Job -Job $j
I don't know what the heck the official Get-Help was trying to do, but this seems more elegant and, as an added bonus, actually works.
Mike