PowerShell Receive-Job

PowerShell Background JobsPowerShell Recieve-Job Cmdlet

The purpose of this cmdlet is to view the data stored in a PowerShell job.  Receive-Job works in conjunction with Start-Job, and you can troubleshoot with Get-Job.

Windows PowerShell Receive-Job Topics

 ♣

 

The Relationship Between Start-Job and Receive-Job

You need Start-Job to create the object, Job# holds the results of instructions in {ScriptBlock}.  The particular role of Receive-Job is to display the contents of that Job#.  When getting testing or troubleshoot PowerShell jobs, I find that a third cmdlet Get-Job is invaluable.

Task Breakdown

a) Start-Job (Creates and stores the data; you see ID 2, Name Job2)
b) Get-Job (Lists the job names, their numbers, and their status)
c) Receive-Job (The climax, to actually view the results in the job)

Example 1: List Events in the System Log

a) Create the Job

Start-Job -ScriptBlock {Get-WinEvent -LogName system -MaxEvents 1000}

Note 1: I have explicitly added -ScriptBlock, even though PowerShell would assume that the instruction in the first position contains this required parameter.

b) View the existence and status of your jobs
This is an optional step, useful in testing and troubleshooting.

Clear-host
Get-Job | Format-Table Id, Name, State, HasMoreData, Command -AutoSize

Id Name State HasMoreData Command
— —- —– ———– ——-
2 Job2 Completed False Get-WinEvent -LogName system -MaxEvents 1000
4 Job4 Completed False Get-WinEvent -LogName system -MaxEvents 1000
6 Job6 Completed True Get-WinEvent -LogName system -MaxEvents 1000

Note 2: I ran the Start-Job command 3 times – just testing!

Note 3: Keep an eye on HasMoreData, False means it's run but the data has gone!  If you want to retain the results employ the -Keep parameter when you use Receive-Job.

c) Review a Specific Job

Get-Job -Name Job6 | Receive-Job -Keep

Note 4: There are several ways of viewing a completed job, here is a simpler command: Receive-Job -ID 6 -Keep

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Research Receive-Job

# Research PowerShell Job
Clear-Host
Get-Help Receive-Job -Full

Note 5: This is how I discovered the -Keep and -AutoRemove parameter.

Example 2: List Services on Remote Computers

As we have already seen, Receive-Job works well with Start-Job; however it can run jobs against remote computers using Invoke-Command.

Let us set up the jobs:

$Serv = Invoke-Command -ComputerName Victim1, Victim2, Victim3 `
 -ScriptBlock {Get-Service} -AsJob
$Serv.ChildJobs

Note 6: Observe the -AsJob parameter of Invoke-Command

View the contents of Job1

Receive-Job -Name Job1 -AutoRemove

Note 7: This time I used the -AutoRemove parameter to delete this job once we have seen the details.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Researching Similar PowerShell Job Cmdlets

Get-Command -Verb Job

Name
——————
Get-Job
Invoke-Command
Receive-Job
Remove-Job
Resume-Job
Start-Job
Stop-Job
Suspend-Job
Wait-Job
about_Job_Details
about_Remote_Jobs
about_Jobs

See also PowerShell -AsJob ยป

Summary of PowerShell Receive-Job

The purpose of this cmdlet is to obtain the results of a PowerShell background jobs; it relies on Start-Job or Invoke-Command -AsJob to actually create the job.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Home   • Real life tasks   • Invoke-Command   • PowerShell Windows 7

Remote PowerShell   • PowerShell WSMan  • -Online   • PowerShell WinRm   • Test-Connection

Jobs  -AsJob   • Receive-Job   • Get-Job   • Receive-Job   • Free WMI Monitor

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.