The cmdlet Wait-Job waits till one or all the PowerShell jobs working within the session are in a terminating state. On this weblog submit I’ll present you an instance you possibly can construct on. Let’s get began.
Begin-Job creates a number of PowerShell background jobs. These jobs are working hidden within the background and allow you to proceed your work in PowerShell. This instance begins a port scan background job.
Begin-Job -Identify PortScan -ScriptBlock {Take a look at-NetConnection 192.168.0.150 -Port 88}
To show the outcomes of this job, run Obtain-Job. Word you could show the consequence solely as soon as! Except you employ the -Maintain parameter.
Obtain-Job -Identify PortScan -Maintain | Choose-Object RemoteAddress,PingSucceeded,TcpTestSucceeded
Waits till one or all the PowerShell jobs working within the session are in a terminating state.
Microsoft describes it this fashion:
The Wait-Job
cmdlet waits for a job to be in a terminating state earlier than persevering with execution. The terminating states are:
- Accomplished
- Failed
- Stopped
- Suspended
- Disconnected
You may wait till a specified job, or all jobs are in a terminating state. You can too set a most wait time for the job utilizing the Timeout parameter, or use the Power parameter to attend for a job within the Suspended
or Disconnected
states.
Right here is an instance of how 2 jobs are created. Ready for the second job, which takes a really very long time. Each jobs usually are not associated, however are good for testing. Lastly, the second job known as, however solely when the primary job has been accomplished.
Get-Job | Take away-Job -Power
Begin-Job -Identify PortScan -ScriptBlock {Take a look at-NetConnection 192.168.0.150 -Port 88}
Begin-Job -Identify Information -ScriptBlock {Get-ChildItem C:Home windows -File -Recurse}
Wait-Job -Identify Information
Obtain-Job -Identify PortScan -Maintain | Choose-Object RemoteAddress,PingSucceeded,TcpTestSucceeded
Hope this was useful and also you are actually allow to implement the Wait-Job cmdlet in your PowerShell scripts.