Getting results from a remote job

Ask your Windows 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.
Locked
User avatar
StupidSexyFlanders
Posts: 107
Joined: Sun Nov 27, 2011 9:22 am

Getting results from a remote job

Post by StupidSexyFlanders » Tue Oct 30, 2012 10:39 am

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

User avatar
StupidSexyFlanders
Posts: 107
Joined: Sun Nov 27, 2011 9:22 am

Getting results from a remote job

Post by StupidSexyFlanders » Tue Oct 30, 2012 11:21 am

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

Locked