So everybody wants to be more
efficient with their work as well as at home. This chapter should help me
solve at least half of that problem. This chapter covers multitasking and
background jobs in PowerShell.
Normally with PowerShell you type a
command and hit return, then you sit and wait for the command to finish. You
cannot run another job until the first one completes. Now you could run a
second window but what happens if you have specific modules loaded or have set
variables for the task your completing. Or you could run the same command and have
it moved to the background. The benefit of running a command as a job is that
if it will be running for a while it will allow you to continue to use the
shell and store the results for later.
There are some pitfalls that deal with
having commands run in the background however. If it is in the background and prompts
you for input then that job will not complete and stop eventually because
you cannot reply. Running a command in the shell produces error messages for
you to see, but background errors will not be visible until the job is
retrieved. If the job is run in the shell you will see the display as soon as
the command completes, however background jobs will have to be retrieved. And
last but certainly not least remember this is not real time data but a snapshot of
what was going on at the exact moment the job ran.
There are a couple kinds of background
jobs discussed in this chapter. The first of which is a Local Job. A local job
runs for the most part, as the title states, on your local workstation. It can
request information from the remote PC's if necessary through the cmdlets, but allows your terminal
to do the heavy lifting. The cmdlet for this is Start-Job. This cmdlet has a lot of really useful parameters so
please read the help file for full details. The following is an example that
will start a background job named Local_Process to Get-Process.
Start-Job -scriptblock {get-service} -name
'Local_Process"
Some cmdlets have the ability to be
run as jobs due to parameters. One of the major ones that Don lists is Get-WmiObject. There are others and If
you want to see them do help * -parameter
asjob. You may also run jobs in the
remoting tools we covered in Chapter 10. The cmdlet to do this is Invoke-Command.
To check the status of running or
completed jobs type
Get-Job
This will display the list of jobs in
your current session only however. No previous jobs are cached. Note that when
you do this that it displays an ID number, Name, State of the job, and
HASMOREDATA column. This column shows if there is data to be retrieved for that
job (PowerShell removes the data once the job is retrieved). To retrieve the
results of the job you run Receive-Job.
With this cmdlet you can bring up the table or list you created in your
scriptblock, pipe it to the Format-List
or Format-Table for custom formatting,
or output it in any other way you like. It’s the same as any regular PowerShell
object.
There are many ways to do background
jobs, and as with anything in PowerShell, no two people do things alike. Just
remember that if your getting your results it's not wrong.
No comments:
Post a Comment