We have all probable been there at some point. We use Start-Job to run a command in the background like this:
Start-Job -ScriptBlock { Get-Process | Where { $_.WS -gt 100MB } } -Name HighMemProcess
Take note of the HasMoreData property. This means that we have data available which can be used with Receive-Job to get the output stream of the background job.
When completed, we typically used Receive-Job to get the data from the output of the PSJob.
Receive-Job -Name HighMemProcess
Now let’s go back to the job and see what is there.
HasMoreData is now showing false, which means that if we use Receive-Job again against this job, no data will be returned.
That could be problematic if we didn’t use the –Keep parameter to ensure that the data will still be available in the PSJob because now we have to re-run the code again.
But wait! There is a way to still pull the data without having to re-run the job. Let’s examine the job a little more and see what is available.
Get-Job -Name HighMemProcess | Select *
What we are seeing is actually a parent job of the child job (Job3) which is actually doing the work. This parent job is really just monitoring the state of the child job and let’s you know when it has completed. With that knowledge, let’s take a look at the child job and see what is there.
$Job = Get-Job -Name HighMemProcess $Job.ChildJobs | Select *
Check out the Output stream, all of the data we need that was originally available when using Receive-Job is still there!
$Job.ChildJobs.output
Now we know that even if we accidently forget to use the –Keep parameter on Receive-Job we can still dig into the child job of the parent job and pull the data from the Output stream.
Thanks alot sir, you helped me a lot
Nice find! It’s a shame it does not work for Workflows…