CmdID / PowerShell / Cmdlets / Wait
Wait-Job
Waits until one or all of the PowerShell jobs running in the session are in a terminating state. PowerShell reference
From the PowerShell reference
9 parameters
Alias wjb
What it does
Waits until one or all of the PowerShell jobs running in the session are in a terminating state. The Wait-Job cmdlet waits for a job to be in a terminating state before continuing execution. The terminating states are: - Completed - Failed - Stopped - Suspended - Disconnected You can wait until a specified job, or all jobs are in a terminating state. You can also set a maximum wait time for the job using the Timeout parameter, or use the Force parameter to wait for a job in the Suspended or Disconnected states. When the commands in the job are complete, Wait-Job returns a job object and continues execution. You can use the Wait-Job cmdlet to wait for jobs started by using the Start-Job cmdlet or the AsJob parameter of the Invoke-Command cmdlet. For more information about jobs, see about_Jobs. Starting in Windows PowerShell 3.0, the Wait-Job cmdlet also waits for custom job types, such as workflow jobs and instances of scheduled jobs. To enable Wait-Job to wait for jobs of a particular type, import the module that supports the custom job type into the session before you run the Get-Job cmdlet, either by using the Import-Module cmdlet or by using or getting a cmdlet in the module. For information about a particular custom job type, see the documentation of the custom job type feature.
Wait-Job [-Any] [-Timeout <Int32>] [-Force] [-Id] <Int32[]> [<CommonParameters>]Wait-Job [-Job] <Job[]> [-Any] [-Timeout <Int32>] [-Force] [<CommonParameters>]Wait-Job [-Any] [-Timeout <Int32>] [-Force] [-Name] <String[]> [<CommonParameters>]Wait-Job [-Any] [-Timeout <Int32>] [-Force] [-InstanceId] <Guid[]> [<CommonParameters>]Wait-Job [-Any] [-Timeout <Int32>] [-Force] [-State] <JobState> [<CommonParameters>]Wait-Job [-Any] [-Timeout <Int32>] [-Force] [-Filter] <Hashtable> [<CommonParameters>]
Wait for all jobs
Get-Job | Wait-JobIdentifyWait for jobs started on remote computers by using Start-Job
$s = New-PSSession Server01, Server02, Server03
Invoke-Command -Session $s -ScriptBlock {Start-Job -Name Date1 -ScriptBlock {Get-Date}}
$done = Invoke-Command -Session $s -Command {Wait-Job -Name Date1}
$done.CountIdentifyDetermine when the first job finishes
$s = New-PSSession -ComputerName (Get-Content -Path .\Machines.txt)
$c = 'Get-EventLog -LogName System | Where-Object {$PSItem.EntryType -eq "error" --and $PSItem.Source -eq "LSASRV"} | Out-File -FilePath Errors.txt'
Invoke-Command -Session $s -ScriptBlock {Start-Job -ScriptBlock {$Using:c}
Invoke-Command -Session $s -ScriptBlock {Wait-Job -Any}IdentifyTrips 1 Sigma rule: mediumEventLog Query Requests By Builtin Utilities by Ali Alwashali, Nasreddine Bencherchali (Nextron Systems)
Set a wait time for jobs on remote computers
PS> $s = New-PSSession -ComputerName Server01, Server02, Server03
PS> $jobs = Invoke-Command -Session $s -ScriptBlock {Start-Job -ScriptBlock {Get-Date}}
PS> $done = Invoke-Command -Session $s -ScriptBlock {Wait-Job -Timeout 30}
PS>IdentifyWait until one of several jobs finishes
Wait-Job -Id 1,2,5 -AnyIdentifyWait for a period, then allow job to continue in background
Wait-Job -Name "DailyLog" -Timeout 120IdentifyWait for a job by name
Wait-Job -Name "Job3"IdentifyWait for jobs on local computer started with Start-Job
$j = Start-Job -ScriptBlock {Get-ChildItem -Filter *.ps1| Where-Object {$PSItem.LastWriteTime -gt ((Get-Date) - (New-TimeSpan -Days 7))}}
$j | Wait-JobIdentifyWait for jobs started on remote computers by using Invoke-Command
$s = New-PSSession -ComputerName Server01, Server02, Server03
$j = Invoke-Command -Session $s -ScriptBlock {Get-Process} -AsJob
$j | Wait-JobIdentifyWait for a job that has an ID
Get-JobIdentifyIts page in the reference: Wait-Job.
Parameters
-AnyIndicates that this cmdlet returns the job object and continues execution when any job finishes. By default, Wait-Job waits until all of the specified jobs are complete before it displays the prompt.-FilterSpecifies a hash table of conditions. This cmdlet waits for jobs that satisfy all of the conditions in the hash table. Enter a hash table where the keys are job properties and the values are job property values.
This parameter works only on custom job types, such as workflow jobs and scheduled jobs. It does not work on standard jobs, such as those created by using the Start-Job cmdlet. For information about support for this parameter, see the help topic for the job type.
This parameter was introduced in Windows PowerShell 3.0.-ForceIndicates that this cmdlet continues to wait for jobs in the Suspended or Disconnected state. By default, Wait-Job returns, or ends the wait, when jobs are in one of the following states:
- Completed
- Failed
- Stopped
- Suspended
- Disconnected
This parameter was introduced in Windows PowerShell 3.0.-IdSpecifies an array of IDs of jobs for which this cmdlet waits.
The ID is an integer that uniquely identifies the job in the current session. It is easier to remember and type than the instance ID, but it is unique only in the current session. You can type one or more IDs, separated by commas. To find the ID of a job, type Get-Job.-InstanceIdSpecifies an array of instance IDs of jobs for which this cmdlet waits. The default is all jobs.
An instance ID is a GUID that uniquely identifies the job on the computer. To find the instance ID of a job, use Get-Job.-JobSpecifies the jobs for which this cmdlet waits. Enter a variable that contains the job objects or a
command that gets the job objects. You can also use a pipeline operator to send job objects to the
Wait-Job cmdlet. By default, Wait-Job waits for all jobs created in the current session.-NameSpecifies friendly names of jobs for which this cmdlet waits.-StateSpecifies a job state. This cmdlet waits only for jobs in the specified state. The acceptable values for this parameter are:
- NotStarted
- Running
- Completed
- Failed
- Stopped
- Blocked
- Suspended
- Disconnected
- Suspending
- Stopping
For more information about job states, see JobState Enumeration.-TimeoutSpecifies the maximum wait time for each job, in seconds. The default value, -1, indicates that the cmdlet waits until the job finishes. The timing starts when you submit the Wait-Job command, not the Start-Job command.
If this time is exceeded, the wait ends and execution continues, even if the job is still running. The command does not display any error message.