CmdID / PowerShell / Cmdlets / Wait
Wait-Event
Waits until a particular event is raised before continuing to run. PowerShell reference
From the PowerShell reference 2 parameters
What it does
Waits until a particular event is raised before continuing to run. The Wait-Event cmdlet suspends execution of a script or function until a particular event is raised. Execution resumes when the event is detected. To cancel the wait, press <kbd>CTRL</kbd>+<kbd>C</kbd>. This feature provides an alternative to polling for an event. It also allows you to determine the response to an event in two different ways: - using the Action parameter of the event subscription - waiting for an event to return and then respond with an action
Wait-Event [[-SourceIdentifier] <String>] [-Timeout <Int32>] [<CommonParameters>]
Wait for the next event
Wait-EventIdentifyWait for an event with a specified source identifier
Wait-Event -SourceIdentifier "ProcessStarted"IdentifyWait for a timer elapsed event
$Timer = New-Object Timers.Timer
$objectEventArgs = @{
InputObject = $Timer
EventName = 'Elapsed'
SourceIdentifier = 'Timer.Elapsed'
}
Register-ObjectEvent @objectEventArgs
$Timer.Interval = 2000
$Timer.AutoReset = $false
$Timer.Enabled = $true
Wait-Event Timer.ElapsedIdentifyWait for an event after a specified timeout
Wait-Event -SourceIdentifier "ProcessStarted" -Timeout 90IdentifyIts page in the reference: Wait-Event.
Parameters
-SourceIdentifierSpecifies the source identifier that this cmdlet waits for events. By default, Wait-Event waits for any event.-TimeoutSpecifies the maximum time, in seconds, that Wait-Event waits for the event to occur. The default, -1, waits indefinitely. The timing starts when you submit the Wait-Event command.
If the specified time is exceeded, the wait ends and the command prompt returns, even if the event has not been raised. No error message is displayed.