• No results found

Using Background Jobs

In document Windows PowerShell 2.0 (Page 113-116)

PowerShell runs background jobs on the local computer by default. You can run background jobs on remote computers by

N Starting an interactive session with a remote computer and starting a

job in the interactive session. This approach allows you to work with the background job the same way as you would on the local computer.

N Running a background job on a remote computer that returns results

to the local computer. This approach allows you to collect the results of background jobs and maintain them from your computer.

N Running a background job on a remote computer and maintaining the results on the remote computer. This approach helps ensure the job data is secure.

PowerShell has several commands for working with background jobs. These commands include

N Get-Job Gets objects that represent the background jobs started in the

current session. Without parameters, Get-Job returns a list of all jobs in the current session. The job object returned does not contain the job results. To get the results, use the Receive-Job cmdlet. You can use the parameters of Get-Job to get background jobs by their command text, names, IDs, or instance IDs. For command text, type the command or the part of the com- mand with wildcards. For IDs, type an integer value that uniquely identifi es the job in the current session. For names, type the friendly names previously assigned to the job. An instance ID is a GUID that uniquely identifi es a job, even when you have multiple jobs running in PowerShell. To fi nd the names, IDs, or instance IDs of jobs, use Get-Jobs without parameters. You can use the –State parameter to get only jobs in the specifi ed state. Valid values are NotStarted, Running, Completed, Stopped, Failed, and Blocked.

Get-Job [-Command Commands] | [[-InstanceId] GUIDs] | [[-Name]

Names] | [[-Id] IDs] | [-State JobState]

N Receive-Job Gets the output and errors of the PowerShell background jobs

started in the current session. You can get the results of all jobs or identify jobs by their name, ID, instance ID, computer name, location, or session, or by inputting a job object. By default, job results are deleted after you receive them, but you can use the –Keep parameter to save the results so that you can receive them again. To delete the job results, receive them again without the –Keep parameter, close the session, or use the Remove-Job cmdlet to delete the job from the session.

Receive-Job [[-ComputerName Computers] | [-Location Locations] | [-Session Session]] [-Job] Jobs [BasicParams]

Receive-Job [[-Id] IDs] | [[-InstanceId] GUIDS] | [[-Name] Names] | [-State States] [BasicParams]

BasicParams=

[-Error] [-Keep] [-NoRecurse]

N Remove-Job Deletes PowerShell background jobs that were started by

using Start-Job or the –AsJob parameter of a cmdlet. Without parameters or parameter values, Remove-Job has no effect. You can delete all jobs or selected jobs based on their command, name, ID, instance ID, or state, or by passing a job object to Remove-Job. Before deleting a running job,

Get-Job [-Command Commands] | [[-InstanceId] GUIDs] | [[-Name]

Names] | [[-Id] IDs] | [-State JobState]

Receive-Job [[-ComputerName Computers] | [-Location Locations] | [-Session Session]] [-Job] Jobs [BasicParams]

Receive-Job [[-Id] IDs] | [[-InstanceId] GUIDS] | [[-Name] Names] | [-State States] [BasicParams]

BasicParams=

you should use Stop-Job to stop the job. If you try to delete a running job, Remove-Job fails. You can use the –Force parameter to delete a running job. If you do not delete a background job, the job remains in the global job cache until you close the session in which the job was created.

Remove-Job [-Force] [-Command Commands] | [[-Job] Jobs] | [[-Id] IDs] | [[-InstanceId] GUIDS] | [[-Name] Names] | [-State States]

N Start-Job Starts a Windows PowerShell background job on the local

computer. To run a background job on a remote computer, use the –AsJob parameter of a cmdlet that supports background jobs, or use the Invoke- Command cmdlet to run a Start-Job command on the remote computer. When you start a Windows PowerShell background job, the job starts, but the results do not appear immediately. Instead, the command returns an object that represents the background job. The job object contains useful information about the job, but it does not contain the results. This approach allows you to continue working while the job runs.

Start-Job [-FilePath Path] [AddtlParams]

Start-Job [-ScriptBlock] ScriptBlock [AddtlParams] AddtlParams=

[-ArgumentList Args] [-Authentication {<Default> | <Basic> | <Negotiate> | <NegotiateWithImplicitCredential> | <Credssp>}] [-ConfigurationName String] [-Credential Credential]

[-InputObject Object] [-Name String] [-NoCompression]

N Stop-Job Stops PowerShell background jobs that are in progress. You can

stop all jobs or stop selected jobs based on their name, ID, instance ID, or state, or by passing a job object to Stop-Job. When you stop a background job, PowerShell completes all tasks that are pending in that job queue and then ends the job. No new tasks are added to the queue after you stop the job. Stop-Job does not delete background jobs. To delete a job, use Remove-Job.

Stop-Job [-PassThru] [[-Job] Jobs] | [[-Id] IDs] | [[-InstanceId] GUIDS] | [[-Name] Names] | [-State States]

N Wait-Job Waits for PowerShell background jobs to complete before it dis-

plays the command prompt. You can wait until any specifi c background jobs are complete or until all background jobs are complete. Use the –Timeout parameter to set a maximum wait time for the job. When the commands in the job are complete, Wait-Job displays the command prompt and returns a job object so that you can pipe it to another command. Use the –Any param- eter to display the command prompt when any job completes. By default,

Remove-Job [-Force] [-Command Commands] | [[-Job] Jobs] | [[-Id] IDs] | [[-InstanceId] GUIDS] | [[-Name] Names] | [-State States]

Start-Job [-FilePath Path] [AddtlParams]

Start-Job [-ScriptBlock] ScriptBlock [AddtlParams]k

AddtlParams=

[-ArgumentList Args] [-Authentication {<Default> | <Basic> | <Negotiate> | <NegotiateWithImplicitCredential> | <Credssp>}] [-ConfigurationName String] [-Credential Credential]

[-InputObject Object] [-Name String] [-NoCompression]

Stop-Job [-PassThru] [[-Job] Jobs] | [[-Id] IDs] | [[-InstanceId] GUIDS] | [[-Name] Names] | [-State States]

Wait-Job waits until all of the specifi ed jobs are complete before displaying the prompt.

Stop-Job [-Any] [-TimeOut WaitTime] [[-Job] Jobs] | [[-Id] IDs] | [[-InstanceId] GUIDS] | [[-Name] Names] | [-State States]

Some cmdlets can be run as background jobs automatically using an –AsJob parameter. You can get a complete list of all cmdlets with an –AsJob parameter by typing the following command: get-help * -parameter AsJob. These cmdlets include :

N Invoke-Command Runs commands on local and remote computers. N Invoke-WmiMethod Calls Windows Management Instrumentation (WMI)

methods.

N Test-Connection Sends Internet Control Message Protocol (ICMP) echo

request packets (pings) to one or more computers.

N Restart-Computer Restarts (reboots) the operating system on local and

remote computers.

N Stop-Computer Stops (shuts down) local and remote computers.

The basic way background jobs work is as follows:

1. You start a background job using Start-Job or the –AsJob parameter of a cmdlet.

2. The job starts, but the results do not appear immediately. Instead, the com- mand returns an object that represents the background job.

3. As necessary, you work with the job object. The job object contains useful information about the job, but it does not contain the results. This approach allows you to continue working while the job runs.

4. To view the results of a job started in the current session, you use Receive-Job. You can identify jobs by their name, ID, instance ID, computer name, location, or session, or by inputting a job object to Receive-Job. After you receive a job, the job results are deleted (unless you use the –Keep parameter).

In document Windows PowerShell 2.0 (Page 113-116)