Prof. Powershell

Moonlighting, PowerShell-Style

The upcoming PowerShell 2.0 has a nifty feature that pass off a PowerShell processing job to the

In PowerShell 1.0, if you run an expression that takes a long time to complete, your PowerShell session is blocked until the command completes. You can't to do anything else in the current session until your prompt returns.

In the upcoming version of PowerShell, version 2.0, you will be able to push those long-running tasks to the background and get your prompt back immediately. You'll then be able to retrieve your job results later. Likewise you’ll also be able to run PowerShell expressions on remote computers as background jobs and retrieve the results.

Before you can use background jobs, you'll need to configure the computer for remoting. Even if you will be running background jobs locally, the computer still must be enabled for remoting. Use Enable-PSRemoting.

The Start-Job cmdlet defaults to the local computer:

PS C:\> $eventlogJob=start-job -ScriptBlock {get-eventlog -list}

This creates a background job that executes the PowerShell expression contained within the script block. I can use Get-Job to see the status of all my jobs, like this:

PS C:\> get-job

Id  Name State      HasMoreData  Location   Command
--  ----  -----     -----------  --------   -------
1   Job1  Running   True         localhost  get-eventlog –list

Or I could look at $eventlogJob which would show the same thing.

Notice the State property? That's how I can tell whether a job is completed. You can also use the – State parameter:

PS C:\> get-job -state completed

Id  Name  State      HasMoreData  Location   Command
--  ----  -----      -----------  --------   -------
1  Job1  Completed    False        localhost  get-eventlog –list

Now that I see the job is complete, I need to get the results:

PS C:\> Receive-Job –job $eventlogjob

The Receive-Job cmdlet retrieves the scriptblock's output. This example uses the job object but you can also retrieve jobs by Id and Name.

One important item to note is that when you receive a job, the data is removed from the job cache. If you try to run Receive-Job again, you'll get nothing. If you think you'll want to access the job results again, use the –Keep parameter.:

PS C:\> Receive-Job –job $eventlogjob -Keep

Of course, managing jobs on multiple machines isn't that much more difficult. There are plenty of examples in the help documentation or perhaps we'll visit this topic in a future lesson (and write me if you're indeed interested in more coverage here).

Note: This information is based on pre-release software and is subject to change.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.