Getting results from a remote job

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
This topic is 11 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
StupidSexyFlanders
Posts: 107
Last visit: Thu Apr 29, 2021 8:47 am

Getting results from a remote job

Post by StupidSexyFlanders »

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
Mike
StupidSexyFlanders
Posts: 107
Last visit: Thu Apr 29, 2021 8:47 am

Getting results from a remote job

Post by StupidSexyFlanders »

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. :)
Mike
This topic is 11 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked