Wait-Event
Published: February 29, 2012
Updated: August 15, 2012
Applies To: Windows PowerShell 2.0, Windows PowerShell 3.0
Wait-Event
Syntax
Parameter Set: Default Wait-Event [[-SourceIdentifier] <String> ] [-Timeout <Int32> ] [ <CommonParameters>]
Detailed Description
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 CTRL+C.
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: by using the Action parameter of the event subscription and by waiting for an event to return and then respond with an action.
Parameters
-SourceIdentifier<String>
Waits only for events with the specified source identifier. By default, Wait-Events waits for any event.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
1 |
|
Default Value |
All events |
|
Accept Pipeline Input? |
true (ByPropertyName) |
|
Accept Wildcard Characters? |
false |
-Timeout<Int32>
Determines 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.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
-1 |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
<CommonParameters>
This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).
Inputs
The input type is the type of the objects that you can pipe to the cmdlet.
- System.String
Outputs
The output type is the type of the objects that the cmdlet emits.
- System.Management.Automation.PSEventArgs
Notes
-
Events, event subscriptions, and the event queue exist only in the current session. If you close the current session, the event queue is discarded and the event subscription is canceled.
Examples
-------------------------- EXAMPLE 1 --------------------------
This command waits for the next event that is raised.
PS C:\> wait-event
-------------------------- EXAMPLE 2 --------------------------
This command waits for the next event that is raised and that has a source identifier of "ProcessStarted".
PS C:\> wait-event -sourceIdentifier "ProcessStarted"
-------------------------- EXAMPLE 3 --------------------------
This command uses the Wait-Event cmdlet to wait for a timer event on a timer that is set for 2000 milliseconds.
PS C:\> $timer.Interval = 2000PS C:\>$timer.Autoreset = $falsePS C:\>$timer.Enabled = $true; Wait-Event Timer.Elapsed# After 2 secondsEventIdentifier : 12Sender : System.Timers.TimerSourceEventArgs : System.Timers.ElapsedEventArgsSourceArgs : {System.Timers.Timer, System.Timers.ElapsedEventArgs}SourceIdentifier : Timer.ElapsedTimeGenerated : 6/10/2008 3:24:18 PMMessageData :ForwardEvent : False
-------------------------- EXAMPLE 4 --------------------------
This command waits up to 90 seconds for the next event that is raised and that has a source identifier of "ProcessStarted". If the specified time expires, the wait ends.
PS C:\> wait-event -sourceIdentifier "ProcessStarted" -timeout 90
Related topics