home *** CD-ROM | disk | FTP | other *** search
- TOPIC
- Background job
-
- SHORT DESCRIPTION
- A Windows PowerShell background job (PsJob) runs a command or expression
- "in the background" without interacting with the console. When you start
- a background job, the command prompt returns immediately, even if the job takes
- an extended time to complete. You can continue to use the console without
- interruption while the job runs. You can run background jobs on a local or
- remote computer.
-
- NOTE: The Windows PowerShell 2.0 background job (PsJob) feature relies
- on remoting technology. To use PsJob, remoting must be enabled on
- Windows PowerShell, even if the PsJob runs on the local computer.
-
-
- LONG DESCRIPTION
- A background job runs a command or expression asynchronously. It might run a
- cmdlet, a function, a script, a ScriptCmdlet, or any other command-based task.
- It is designed to run commands that take an extended period of time, but you
- can use it to run any command "in background".
-
- When a regular command runs, the Windows PowerShell command prompt is suppressed
- until the command completes. But a background job does not suppress the Windows
- PowerShell prompt; the prompt returns immediately after you start the job so you
- can work on other tasks while the background job runs.
-
- However, when you start a background job, you do not get the results immediately,
- even if the job runs very quickly. Instead of the result, you get a job object
- that represents the job. You can use the job object to request the result of the
- background job. If the job is not completed, you can get partial results or wait
- for the job to complete and then get complete results.
-
- To make the timing of a background job independent of other commands, each background
- job run in its own Windows PowerShell environment (a "runspace"). However, this
- can be a temporary runspace that is created only to run the job and is then destroyed,
- or a persistent runspace that you can use to run several related jobs or commands.
-
- You can create and manage background jobs on a local or remote computer. To run a
- background job remotely, use the ComputerName parameter of the Start-PsJob cmdlet
- specify the name of the remote computer, or use the New-Runspace cmdlet to create
- a persistent runspace on the remote computer, and then use Runspace parameter of
- Start-PsJob to specify the remote runspace.
-
-
-
- CREATING BACKGROUND JOBS
- Windows PowerShell has a set of PsJob cmdlets to help you create and manage
- background jobs.
-
- To create a job, use the New-PsJob or Start-PsJob cmdlet. The following command
- uses the Start-PsJob cmdlet to start a background job that runs a Get-Process
- command on the local computer.
-
- start-psjob -command "get-process"
-
- You can also start jobs on remote computers. The following command runs a
- similar background job in a temporary runspace on the Server01 computer.
-
- start-psjob -command "get-process" -computername Server01
-
- If the computer is in a different domain, you might need to use the Credential
- parameter to run the command with the permissions of a member of the Administrators
- group on the computer, as shown in the following command:
-
- start-psjob -command "get-process" -computername Server01 -credential domain01\admin01
-
- You can also run a background job in a runspace on a local or remote computer. The
- following command creates a runspace on the Server01 computer and then runs the
- background job in the new runspace.
-
- $r = new-runspace -computername Server01
-
- start-psjob -command "get-process" -runspace $r
-
-
-
- GETTING THE RESULTS OF A BACKGROUND JOB
-
- When you run a background job, the results do not appear immediately. Instead, the
- Start-PsJob cmdlet returns a job object that respresents the job. The following command
- saves the job object in the $job variable.
-
- $job = start-psjob -command "get-process" -computername Server01
-
- To get the results of the job, use the Receive-PsJob cmdlet. The following command gets
- the results of the job running on the Server01 computer. The command uses the $job object
- that represents the job.
-
- receive-psjob -job $job
-
- Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
- ------- ------ ----- ----- ----- ------ -- -----------
- 103 4 11328 9692 56 1176 audiodg
- 804 14 12228 14108 100 101.74 1740 CcmExec
- 668 7 2672 6168 104 32.26 488 csrss
- 79 4 11668 4076 90 2.50 552 csrss
- 374 5 3548 9284 143 10.19 836 csrss
- ...
-
-
- WAITING FOR THE RESULTS
-
- If you run a command that takes a long time to complete, you can use the properties
- of the job object to determine when the job is complete. The following command uses
- the Get-PsJob object to get all of the background jobs in the current console.
-
- get-psjob
-
- The result appear in a table. The status of the job appears in the State column.
-
- SessionId Name Command State
- --------- ---- ------- -----
- 2 get-eventlog -log syst... Completed
- 4 & .\search-files.ps1... Failed
- 6 get-process Failed
- 8 get-process Completed
- 10 get-eventlog -system Running
-
-
- In this case, the State property reveals that Job 10 is still running. If you were to
- use the Receive-PsJob cmdlet to get the job results now, the results would be incomplete.
- You can use the Receive-PsJob cmdlet repeatedly to get all of the results. By default, each
- time you use it, you get only the results that were not already received, but you can use
- the Keep parameter of the Receive-PsJob cmdlet to retain the results, even though they are
- received.
-
- At this point, you can write the results to a file and then append the newly received
- results as they arrive, or simply wait and check the state of the job later.
-
- Or, you can use the Wait-PsJob cmdlet to wait for any or all of the results of the job.
- Wait-PsJob lets you wait for a particular job, for all jobs, or for any of the job
- to complete.
-
- The following command uses the Wait-PsJob cmdlet to wait for a job with SessionID 10.
-
- wait-psjob -sessionID 10
-
- As a result, the Windows Powershell prompt is suppressed until the job completes.
-
- You can also wait for a predetermined period of time. This command uses the Timeout
- parameter to limit the wait to
-
-
- STOPPING A JOB
-
- To stop a background job, use the Stop-PsJob cmdlet. The following command
- starts a job to get every entry in the System event log. It saves the job
- object in the $job variable.
-
- $job = start-psjob -command "get-eventlog -log system"
-
- The following command stops the job. It uses a pipeline operator (|) to
- send the job in the $job variable to Stop-PsJob.
-
- $job | stop-psjob
-
-
- DELETING A JOB
-
- To delete a background job, use the Remove-PsJob cmdlet. The following command
- deletes all jobs in the current console.
-
- remove-psjob
-
-
- INVESTIGATING A FAILED JOB
-
- To find out why a job failed, use the Reason subproperty of the job object.
-
- The following command starts a job on a remote computer without the required
- credentials. It saves the job object in the $job variable.
-
-
- $job = start-psjob -computername Server02 -command "dir"
-
- SessionId Name Command State
- --------- ---- ------- -----
- 3 dir Failed
-
- The following command uses the Reason property to find the error that caused
- the job to fail.
-
- $job.ChildJobs[0].JobStateInfo.Reason
-
-
- In this case, the job failed because the remote computer required explicit
- credentials to run the command. The value of the Reason property is:
-
- Connection attempt failed.
-
-
- SEE ALSO
-
- For a list of background job (psjob) cmdlets, type:
-
- get-help *-psjob
-
-
- For a list of runspace cmdlets, type:
-
- get-help *-runspace
-
-
- For more information about Windows PowerShell remote operation, type:
-
- get-help about_remoting
-
-
-
-
-
-
-
-
-
-
-
-
-
-