18. PowerShell -- Start-Job, Get-Job, Remove-Job 等
· PowerShell Job Overview
This topic explains how to run background jobs in Windows PowerShell on a
local computer. For information about running background jobs on remote
computers, see about_Remote_Jobs.
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
work in the session without interruption while the job runs.
· PowerShell Job CMDLETS
Start-Job:
Starts a background job on a local computer.
Get-Job:
Gets the background jobs that were started in the current session.
Receive-Job:
Gets the results of background jobs.
Stop-Job:
Stops a background job.
Wait-Job:
Suppresses the command prompt until one or all jobs are complete.
Remove-Job:
Deletes a background job.
Invoke-Command:
The AsJob parameter runs any command as a background job on a
Remote computer. You can also use Invoke-Command to run
Any job command remotely, including a Start-Job command.
PowerShell Job 实例
o 实例一: Start-Job -ScriptBlock {Get-Process}
$job = Start-Job -ScriptBlock {Get-Process}
The above command starts a background job that runs a Get-Process command on the local computer.
o 实例二: PS C:\>Get-Job
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
Job1 ·BackgroundJob ·Running ·True ·localhost ·Get-··Process
To get object that represent the background jobs that were started in the current session, use the Get-Job cmdlet. Without parameters, Get-Job returns all of the jobs that were started in the current session.
The following command gets the job with ID 1 and saves it in the $job variable.
$job = Get-Job -Id 1
The job object contains the state of the job, which indicates whether the
job has finished. A finished job has a state of "Complete" or "Failed". A
job might also be blocked or running.
o 实例三: Receive-Job -Job $job
1. The Receive-Job cmdlet returns the results of the 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
...
2. Save the results of a job in a variable.
$results = Receive-Job -Job $job
3. Save the results of the job in a file by using the redirection operator (>) or the Out-File cmdlet.
Receive-Job -Job $job > results.txt
4. When Receive-Job returns results, by default, it deletes those results from the cache where job results are stored.
C:\PS> Receive-Job -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
5. To prevent Receive-Job from deleting the job results that it has returned, use the Keep parameter.
C:\PS> Receive-Job -Job $job -Keep
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
o 实例四: Wait-Job -ID 10
The above command uses the Wait-Job cmdlet to wait for a job with
ID 10.
As a result, the Windows PowerShell prompt is suppressed until the job
is completed.
You can also wait for a predetermined period of time. This command uses
the Timeout parameter to limit the wait to 120 seconds. When the time
expires, the command prompt returns, but the job continues to run in the
background.
Wait-Job -ID 10 -Timeout 120
o 实例五: STOPPING A JOB
$job = Start-Job -ScriptBlock {Get-EventLog -Log System}
$job | Stop-Job
To stop a background job, use the Stop-Job cmdlet. The above command
starts a job to get every entry in the System event log. It saves the job
object in the $job variable.
The above command stops the job. It uses a pipeline operator (|) to
send the job in the $job variable to Stop-Job.
o 实例六: DELETING A JOB
To delete a background job, use the Remove-Job cmdlet. The following
command deletes the job in the $job variable.
Remove-Job -Job $job
o 实例七: 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 without the required credentials. It
saves the job object in the $job variable.
$job = Start-Job -ScriptBlock
{New-Item -Path HKLM:\Software\MyCompany}
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 Job1 BackgroundJob Failed False localhost New-Item -Path HKLM:\S...
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:
Connecting to remote server failed with the following error
message : Access is denied.
o 实例八: Waiting JOB
Write-Host "Waiting job..."
Get-Job|Wait-Job|Remove-Job
SEE ALSO
about_Remote_Jobs
about_Job_Details
about_Remote
about_PSSessions
Start-Job
Get-Job
Receive-Job
Stop-Job
Wait-Job
Remove-Job
Invoke-Command
参考:
https://technet.microsoft.com/zh-cn/library/hh847783(v=wps.620).aspx#
https://technet.microsoft.com/en-us/library/dd347692.aspx
本文出自 “Ricky's Blog” 博客,请务必保留此出处http://57388.blog.51cto.com/47388/1642119
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。