Quick Hits: Did I Really Lose My Output With Receive-Job By Not Using–Keep?

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

image

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

image

 

Now let’s go back to the job and see what is there.

image

HasMoreData is now showing false, which means that if we use Receive-Job again against this job, no data will be returned.

image

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 *

image

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 *

SNAGHTMLa7e8728

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

image

 

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.

About these ads

About Boe Prox

Microsoft PowerShell MVP working as a Senior Systems Administrator
This entry was posted in powershell and tagged , , , . Bookmark the permalink.

One Response to Quick Hits: Did I Really Lose My Output With Receive-Job By Not Using–Keep?

  1. Arnoud Jansveld says:

    Nice find! It’s a shame it does not work for Workflows…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s