PowerShell Start-Job Background

PowerShell Background JobsPowerShell Start-Job Cmdlet

The purpose of this cmdlet is to set off a chain of instructions that form a PowerShell background job.  Start-Job requires a scriptblock containing a file reference or normal PowerShell code.

Windows PowerShell Start-Job Topics

 ♣

 

Understanding the Start-Job Cmdlet

Start-Job cmdlet kicks-off a Windows PowerShell job, which runs "in the background", and thus does not interact with the current session.  The benefit is that with time-consuming tasks you can continue to execute other commands in the session, while the job runs silently in parallel.

It's crucial to realize that the PowerShell job object does not contain any results.  What you need to do is wait for the defined to job to complete, then call for the Receive-Job cmdlet to view the results of the job.

Make a Plan

a) Create a job (We need a vehicle such as Event Log or Processes)
b) List Jobs (View the job names and numbers )[Optional step]
c) Retreive the data (Important, vital step.)

Example 1: List System Events

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 status and existence of your jobs:

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 the job has run but the data has gone!  If you want to retain the results employ the -Keep parameter when you use Receive-Job

Note 4: I only get even-numbered IDs, the reason is the odd-numbers are ChildJobs, check for yourself with Get-Job | Format-List.

Review a Specific Job

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

Note 5: 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

Researching Start-Job

# Research PowerShell Background Job
Clear-Host
Get-Help Start-Job -Full

Note 6: Observe the variety of parameters in position 1.

  • Start-Job [-ScriptBlock]
  • Start-Job [-FilePath]
  • Start-Job [[-InitializationScript]<ScriptBlock>]
  • Start-Job [-DefinitionName]

Note 7: If you employ the -FilePath parameter then the -ArgumentList could be useful.

Example 2: Start-Job -FilePath

Create the Job

Clear-Host
$Location = "F:\PowerShell\Text\SEO"
Start-Job -Filepath $Location\Prime100.ps1 -ArgumentList $Location

Note 8: I have introduced the $Location variable to remind you to change the path to one of your PowerShell files.

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 Cmdlets

# PowerShell Write Cmdlet Research
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 more PowerShell Techniques »

Summary of PowerShell Start-Job

While Start-Job is the foundation of PowerShell background jobs, you need Receive-Job to actually review the results.

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.