Tag Archives: SourceIdentifier

Wait-Event

NAME
    Wait-Event

SYNOPSIS
    Waits until a particular event is raised before continuing to run.

SYNTAX
    Wait-Event [[-SourceIdentifier] <string>] [-Timeout <int>] [<CommonParameters>]

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.

        Required?                    false
        Position?                    1
        Default value                All events
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Timeout <int>
        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.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.String

OUTPUTS
    System.String

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.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Wait-Event

    Description
    ———–
    This command waits for the next event that is raised.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Wait-Event -SourceIdentifier “ProcessStarted”

    Description
    ———–
    This command waits for the next event that is raised and that has a source identifier of “ProcessStarted”.

    ————————– EXAMPLE 3 ————————–

    C:\PS>$timer.Interval = 2000
    C:\PS> $timer.Autoreset = $false
    C:\PS> $timer.Enabled = $true; Wait-Event Timer.Elapsed

    # After 2 seconds

    EventIdentifier : 12
    Sender         : System.Timers.Timer
    SourceEventArgs : System.Timers.ElapsedEventArgs
    SourceArgs     : {System.Timers.Timer, System.Timers.ElapsedEventArgs}
    SourceIdentifier : Timer.Elapsed
    TimeGenerated    : 6/10/2008 3:24:18 PM
    MessageData     :
    ForwardEvent     : False

    Description
    ———–
    This command uses the Wait-Event cmdlet to wait for a timer event on a timer that is set for 2000 milliseconds.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Wait-Event -SourceIdentifier “ProcessStarted” -Timeout 90

    Description
    ———–
    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.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135276
    Register-ObjectEvent
    Register-EngineEvent
    Register-WmiEvent
    Unregister-Event
    Get-Event
    New-Event
    Remove-Event
    Wait-Event
    Get-EventSubscriber

Unregister-Event

NAME
    Unregister-Event

SYNOPSIS
    Cancels an event subscription.

SYNTAX
    Unregister-Event [-SubscriptionId] <int> [-Force] [-Confirm] [-WhatIf] [<CommonParameters>]

    Unregister-Event [-SourceIdentifier] <string> [-Force] [-Confirm] [-WhatIf] [<CommonParameters>]

DESCRIPTION
    The Unregister-Event cmdlet cancels an event subscription that was created by using the Register-EngineEvent, Register-ObjectEvent, or Register-WmiEvent cmdlet.

    When an event subscription is canceled, the event subscriber is deleted from the session and the subscribed events are no longer added to the event queue. When you cancel a subscription to an event created by using the New-Event cmdlet, the new event is also deleted from the session.

    Unregister-Event does not delete events from the event queue. To delete events, use the Remove-Event cmdlet.

PARAMETERS
    -Force [<SwitchParameter>]
        Cancels all event subscriptions, including subscriptions that were hidden by using the SupportEvent parameter of Register-ObjectEvent, Register-WmiEvent, and Register-EngineEvent.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SourceIdentifier <string>
        Cancels event subscriptions that have the specified source identifier.

        A SourceIdentifier or SubscriptionId parameter must be included in every command.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -SubscriptionId <int>
        Cancels event subscriptions that have the specified subscription identifier.

        A SourceIdentifier or SubscriptionId parameter must be included in every command.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Confirm [<SwitchParameter>]
        Prompts you for confirmation before executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -WhatIf [<SwitchParameter>]
        Describes what would happen if you executed the command without actually executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.Management.Automation.PSEventSubscriber
        You can pipe the output from Get-EventSubscriber to Unregister-Event.

OUTPUTS
    None
        This cmdlet does not return any output.

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.

        Unregister-Event cannot delete events created by using the New-Event cmdlet unless you have subscribed to the event by using the Register-EngineEvent cmdlet. To delete a custom event from the session, you must remove it programmatically or close the session.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Unregister-Event -SourceIdentifier ProcessStarted

    Description
    ———–
    This command cancels the event subscription that has a source identifier of “ProcessStarted”.

    To find the source identifier of an event, use the Get-Event cmdlet. To find the source identifier of an event subscription, use the Get-EventSubscriber cmdlet.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Unregister-Event -subscriptionId 2

    Description
    ———–
    This command cancels the event subscription that has a subscription identifier of 2.

    To find the subscription identifier of an event subscription, use the Get-EventSubscriber cmdlet.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Get-Eventsubscriber -Force | Unregister-Event -Force

    Description
    ———–
    This command cancels all event subscriptions in the session.

    The command uses the Get-EventSubscriber cmdlet to get all event subscriber objects in the session, including the subscribers that are hidden by using the SupportEvent parameter of the event registration cmdlets.

    It uses a pipeline operator (|) to send the subscriber objects to Unregister-Event, which deletes them from the session. To complete the task, the Force parameter is also required on Unregister-Event.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135269
    Register-ObjectEvent
    Register-EngineEvent
    Register-WmiEvent
    Unregister-Event
    Get-Event
    New-Event
    Remove-Event
    Wait-Event
    Get-EventSubscriber

Register-ObjectEvent

NAME
    Register-ObjectEvent

SYNOPSIS
    Subscribes to the events that are generated by a Microsoft .NET Framework object.

SYNTAX
    Register-ObjectEvent [-InputObject] <psobject> [-EventName] <string> [[-SourceIdentifier] <string>] [[-Action] <scriptblock>] [-Forward] [-MessageData <psobject>] [-SupportEvent] [<CommonParameters>]

DESCRIPTION
    The Register-ObjectEvent cmdlet subscribes to events that are generated by .NET Framework objects on the local computer or on a remote computer.

    When the subscribed event is raised, it is added to the event queue in your session. To get events in the event queue, use the Get-Event cmdlet.

    You can use the parameters of Register-ObjectEvent to specify property values of the events that can help you to identify the event in the queue. You can also use the Action parameter to specify actions to take when a subscribed event is raised and the Forward parameter to send remote events to the event queue in the local session.

    When you subscribe to an event, an event subcriber is added to your session. To get the event subscribers in the session, use the Get-EventSubscriber cmdlet. To cancel the subscription, use the Unregister-Event cmdlet, which deletes the event subscriber from the session.

PARAMETERS
    -Action <scriptblock>
        Specifies commands to handle the events. The commands in the Action run when an event is raised, instead of sending the event to the event queue. Enclose the commands in braces ( { } ) to create a script block.

        The value of the Action parameter can include the $Event, $EventSubscriber, $Sender, $SourceEventArgs, and $SourceArgs automatic Variables, which provide information about the event to the Action script block. For more information, see about_Automatic_Variables.

        When you specify an action, Register-ObjectEvent returns an event job object that represents that action. You can use the Job cmdlets to manage the event job.

        Required?                    false
        Position?                    102
        Default value                None.
        Accept pipeline input?     false
        Accept wildcard characters? false

    -EventName <string>
        Specifies the event to which you are subscribing. Enter the event name. This parameter is required.

        The value of this parameter is not a name that you select for the event subscription. It is the name of an event that the .NET Framework object exposes. For example, the ManagementEventWatcher class has events named “EventArrived” and “Stopped.” To find the event name of an event, use the Get-Member cmdlet.

        Required?                    true
        Position?                    2
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Forward [<SwitchParameter>]
        Sends events for this subscription to a remote session. Use this parameter when you are registering for events on a remote computer or in a remote session.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?     false
        Accept wildcard characters? false

    -InputObject <psobject>
        Specifies the .NET Framework object that generates the events. Enter a Variable that contains the object, or type a command or expression that gets the object. This parameter is required.

        Required?                    true
        Position?                    1
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -MessageData <psobject>
        Specifies any additional data to be associated with this event subscription. The value of this parameter appears in the MessageData property of all events associated with this subscription.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SourceIdentifier <string>
        Specifies a name that you select for the subscription. The name that you select must be unique in the current session. The default value is the GUID that Windows PowerShell assigns.

        The value of this parameter appears in the value of the SourceIdentifier property of the subcriber object and of all event objects associated with this subscription.

        Required?                    false
        Position?                    101
        Default value                GUID
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SupportEvent [<SwitchParameter>]
        Hides the event subscription. Use this parameter when the current subscription is part of a more complex event registration mechanism and it should not be discovered independently.

        To view or cancel a subscription that was created with the SupportEvent parameter, use the Force parameter of the Get-EventSubscriber and Unregister-Event cmdlets.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe objects to Register-ObjectEvent.

OUTPUTS
    None
        This cmdlet does not generate any output.

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.

    ————————– EXAMPLE 1 ————————–

    C:\PS>$query = New-Object System.Management.WqlEventQuery “__InstanceCreationEvent”, (New-Object TimeSpan 0,0,1), “TargetInstance isa ‘Win32_Process'”

    C:\PS> $processWatcher = New-Object System.Management.ManagementEventWatcher $query

    C:\PS> Register-ObjectEvent -InputObject $processWatcher -EventName “EventArrived”

    Description
    ———–
    This example subscribes to events generated when a new process starts.

    The command uses the ManagementEventWatcher object to get EventArrived events. A query object specifies that the events are instance creation events for the Win32_Process class.

    ————————– EXAMPLE 2 ————————–

    C:\PS>$query = New-Object System.Management.WqlEventQuery “__InstanceCreationEvent”, (New-Object TimeSpan 0,0,1), “TargetInstance isa ‘Win32_Process'”

    C:\PS> $processWatcher = New-Object System.Management.ManagementEventWatcher $query

    C:\PS> $action = { New-Event “PowerShell.ProcessCreated” -Sender $sender -EventArguments $SourceEventArgs.NewEvent.TargetInstance }

    C:\PS> Register-ObjectEvent -InputObject $processWatcher -EventName “EventArrived” -Action $action

    Id    Name            State     HasMoreData     Location             Command
    —    —-            —–     ———–     ——–             ——-
    2     422cfe5a-65e… Running    True                                 New-Event “PowerShe…

    Description
    ———–
    This example shows how to specify an action to respond to an event. When you specify an action, events that are raised are not added to the event queue. Instead, the action responds to the event.

    In this example, when an instance creation event is raised indicating that a new process is started, a new ProcessCreated event is raised.

    The action uses the $Sender and $SourceEventArgs automatic Variables which are populated only for event actions.

    The Register-ObjectEvent command returns a job object that represents the action, which runs as a background job. You can use the Job cmdlets, such as Get-Job and Receive-Job, to manage the background job.

    For more information, see about_jobs.

    ————————– EXAMPLE 3 ————————–

    C:\PS>$s = New-PSSession -computername Server01, Server02

    C:\PS> Invoke-Command -session $s -filepath ProcessCreationEvent.ps1

    C:\PS> Invoke-Command -session $s { Get-Event }

    # ProcessCreationEvent.ps1

    Function Enable-ProcessCreationEvent
    {
     $query = New-Object System.Management.WqlEventQuery “__InstanceCreationEvent”, `
         (New-Object TimeSpan 0,0,1), `
         “TargetInstance isa ‘Win32_Process'”
     $processWatcher = New-Object System.Management.ManagementEventWatcher $query

     $identifier = “WMI.ProcessCreated”
     Register-ObjectEvent -input $processWatcher -EventName “EventArrived” `
         -SourceIdentifier $identifier -MessageData “Test” -Forward
     }
    }

    EnableProcessCreationEvent

    Description
    ———–
    This example shows how to subscribe to object events on remote computers.

    The first command creates PSSessions on two remote computers and saves them in the $s Variable.

    The second command uses the FilePath parameter of the Invoke-Command cmdlet to run the ProcessCreationEvent.ps1 script in the each of the PSSessions in $s.

    The script includes a Register-ObjectEvent command that subscribes to instance creation events on the Win32_Process object through the ManagementEventWatcher object and its EventArrived event.

    ————————– EXAMPLE 4 ————————–

    C:\PS>$timer = New-Object Timers.Timer

    C:\PS> $timer.Interval = 500

    C:\PS> $job = Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Timer.Random -Action {$random = Get-Random -Min 0 -Max 100}

    C:\PS> $job.gettype().fullname
    System.Management.Automation.PSEventJob

    C:\PS> $job | Format-List -property *

    State         : Running
    Module        : __DynamicModule_6b5cbe82-d634-41d1-ae5e-ad7fe8d57fe0
    StatusMessage :
    HasMoreData : True
    Location     :
    Command     : $random = Get-Random -Min 0 -Max 100
    JobStateInfo : Running
    Finished     : System.Threading.ManualResetEvent
    InstanceId    : 88944290-133d-4b44-8752-f901bd8012e2
    Id            : 1
    Name         : Timer.Random
    ChildJobs     : {}
    …

    C:\PS> $timer.Enabled = $true

    C:\PS> & $job.module {$random}
    60

    C:\PS> & $job.module {$random}
    47

    Description
    ———–
    This example shows how to use the dynamic module in the PSEventJob object that is created when you include an Action in a event registration.

    The first command uses the New-Object cmdlet to create a timer object. The second command sets the interval of the timer to 500 (milliseconds).

    The third command uses the Register-ObjectEvent cmdlet to register the Elapsed event of the timer object. The command includes an action that handles the event. Whenever the timer interval elapses, an event is raised and the commands in the action run. In this case, the Get-Random cmdlet generates a random number between 0 and 100 and saves it in the $random Variable.

    When you use an Action parameter in a Register-ObjectEvent command, the command returns a PSEventJob object that represents the action. The command saves the job object in the $job Variable.

    The PSEventJob object that the Register-ObjectEvent cmdlet returns is also available in the Action property of the event subscriber. For more information, see Get-EventSubscriber.

    The fourth command shows that the $job Variable contains a PSEventJob object. The fifth command uses the Format-List cmdlet to display all of the properties of the PSEventJob object in a list.

    The PSEventJob has a Module property that contains a dynamic script module that implements the action.

    The sixth command enables the timer.

    The remaining commands use the call operator (&) to invoke the command in the module and display the value of the $random Variable. You can use the call operator to invoke any command in a module, including commands that are not exported. In this case, the commands show the random number that is being generated when the Elapsed event occurs.

    For more information about modules, see about_modules.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135244
    Register-EngineEvent
    Register-WmiEvent
    Unregister-Event
    Get-Event
    New-Event
    Remove-Event
    Wait-Event

Register-WmiEvent

NAME
    Register-WmiEvent

SYNOPSIS
    Subscribes to a Windows Management Instrumentation (WMI) event.

SYNTAX
    Register-WmiEvent [-Class] <string> [[-SourceIdentifier] <string>] [[-Action] <scriptblock>] [-ComputerName <string>] [-Credential <PSCredential>] [-Forward] [-MessageData <psobject>] [-Namespace <string>] [-SupportEvent] [-Timeout <Int64>] [<CommonParameters>]

    Register-WmiEvent [-Query] <string> [[-SourceIdentifier] <string>] [[-Action] <scriptblock>] [-ComputerName <string>] [-Credential <PSCredential>] [-Forward] [-MessageData <psobject>] [-Namespace <string>] [-SupportEvent] [-Timeout <Int64>] [<CommonParameters>]

DESCRIPTION
    The Register-WmiEvent cmdlet subscribes to WMI events on the local computer or on a remote computer.

    When the subscribed WMI event is raised, it is added to the event queue in your local session even if the event occurs on a remote computer. To get events in the event queue, use the Get-Event cmdlet.

    You can use the parameters of Register-WmiEvent to subscribe to events on remote computers and to specify the property values of the events that can help you to identify the event in the queue. You can also use the Action parameter to specify actions to take when a subscribed event is raised.

    When you subscribe to an event, an event subscriber is added to your session. To get the event subscribers in the session, use the Get-EventSubscriber cmdlet. To cancel the subscription, use the Unregister-Event cmdlet, which deletes the event subscriber from the session.

PARAMETERS
    -Action <scriptblock>
        Specifies commands that handle the events. The commands in the Action parameter run when an event is raised instead of sending the event to the event queue. Enclose the commands in braces ( { } ) to create a script block.

        The value of the Action parameter can include the $Event, $EventSubscriber, $Sender, $SourceEventArgs, and $SourceArgs automatic Variables, which provide information about the event to the Action script block. For more information, see about_Automatic_Variables.

        When you specify an action, Register-WmiEvent returns an event job object that represents that action. You can use the cmdlets that contain the Job noun (the Job cmdlets) to manage the event job.

        Required?                    false
        Position?                    102
        Default value                The event is added to the event queue.
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Class <string>
        Specifies the event to which you are subscribing. Enter the WMI class that generates the events. A Class or Query parameter is required in every command.

        Required?                    true
        Position?                    1
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ComputerName <string>
        Specifies a remote computer. The default is the local computer. Enter a NetBIOS name, an IP address, or a fully qualified domain name.

        Required?                    false
        Position?                    named
        Default value                Local computer
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Credential <PSCredential>
        Specifies a user account that has permission to perform this action. Type a user name, such as “User01” or “Domain01\User01”. Or, enter a PSCredential object, such as one from the Get-Credential cmdlet. If you type a user name, you will be prompted for a password.

        Required?                    false
        Position?                    named
        Default value                The credentials of the current user
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Forward [<SwitchParameter>]
        Sends events for this subscription to the session on the local computer. Use this parameter when you are registering for events on a remote computer or in a remote session.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -MessageData <psobject>
        Specifies any additional data to be associated with this event subscription. The value of this parameter appears in the MessageData property of all events associated with this subscription.

        Required?                    false
        Position?                    named
        Default value                None. The MessageData property is NULL.
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Namespace <string>
        Specifies the namespace of the WMI class.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Query <string>
        Specifies a query in WMI Query Language (WQL) that identifies the WMI event class, such as “select * from __InstanceDeletionEvent”.

        Required?                    true
        Position?                    1
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SourceIdentifier <string>
        Specifies a name that you select for the subscription. The name that you select must be unique in the current session. The default value is the GUID that Windows PowerShell assigns.

        The value of this parameter appears in the value of the SourceIdentifier property of the subscriber object and of all event objects associated with this subscription.

        Required?                    false
        Position?                    101
        Default value                GUID
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SupportEvent [<SwitchParameter>]
        Hides the event subscription. Use this parameter when the current subscription is part of a more complex event registration mechanism and it should not be discovered independently.

        To view or cancel a subscription that was created with the SupportEvent parameter, use the Force parameter of the Get-EventSubscriber and Unregister-Event cmdlets.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Timeout <Int64>
        Determines how long Windows PowerShell waits for this command to complete.

        The default value, 0 (zero), means that there is no time-out, and it causes Windows PowerShell to wait indefinitely.

        Required?                    false
        Position?                    named
        Default value                0
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe objects to Register-WmiEvent.

OUTPUTS
    None
        This cmdlet does not generate any output.

NOTES

        To use this cmdlet in Windows Vista or a later version of Windows, start Windows PowerShell with the “Run as administrator” option.

        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.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Register-WmiEvent -Class ‘Win32_ProcessStartTrace’ -SourceIdentifier “ProcessStarted”

    Description
    ———–
    This command subscribes to the events generated by the Win32_ProcessStartTrace class. This class raises an event whenever a process starts.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Register-WmiEvent -query “select * from __instancecreationevent within 5 where targetinstance isa ‘win32_process'” -SourceIdentifier “WMIProcess” -MessageData “Test 01” -Timeout 500

    Description
    ———–
    This command uses a query to subscribe to Win32_process instance creation events.

    ————————– EXAMPLE 3 ————————–

    C:\PS>$action = { Get-History | where { $_.commandline -like “*Start-Process*” } | Export-Clixml “commandHistory.clixml” }

    C:\PS> Register-WmiEvent -Class ‘Win32_ProcessStartTrace’ -SourceIdentifier “ProcessStarted” -Action $action

    Id    Name            State     HasMoreData Location Command
    —    —-            —–     ———– ——– ——-
    1     ProcessStarted NotStarted False                 Get-History | where {…

    Description
    ———–
    This example shows how to use an action to respond to an event. In this case, when a process starts, any Start-Process commands in the current session are written to an XML file.

    When you use the Action parameter, Register-WmiEvent returns a background job that represents the event action. You can use the Job cmdlets, such as Get-Job and Receive-Job, to manage the event job.

    For more information, see about_jobs.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Register-WmiEvent -Class ‘Win32_ProcessStartTrace’ -SourceIdentifier “Start” -ComputerName Server01

    C:\PS> Get-Event -SourceIdentifier “Start”

    Description
    ———–
    This example registers for events on the Server01 remote computer.

    WMI returns the events to the local computer and stores them in the event queue in the current session. To retrieve the events, run a local Get-Event command.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135245
    Register-ObjectEvent
    Register-EngineEvent
    Unregister-Event
    Get-Event
    New-Event
    Remove-Event
    Wait-Event

Remove-Event

NAME
    Remove-Event

SYNOPSIS
    Deletes events from the event queue.

SYNTAX
    Remove-Event [-EventIdentifier] <int> [-Confirm] [-WhatIf] [<CommonParameters>]

    Remove-Event [-SourceIdentifier] <string> [-Confirm] [-WhatIf] [<CommonParameters>]

DESCRIPTION
    The Remove-Event cmdlet deletes events from the event queue in the current session.

    This cmdlet deletes only the events currently in the queue. To cancel event registrations or unsubscribe, use the Unregister-Event cmdlet.

PARAMETERS
    -EventIdentifier <int>
        Deletes only the event with the specified event identifier. An EventIdentifier or SourceIdentifier parameter is required in every command.

        Required?                    true
        Position?                    1
        Default value                None
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -SourceIdentifier <string>
        Deletes only the events with the specified source identifier. Wildcards are not permitted. An EventIdentifier or SourceIdentifier parameter is required in every command.

        Required?                    true
        Position?                    1
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Confirm [<SwitchParameter>]
        Prompts you for confirmation before executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -WhatIf [<SwitchParameter>]
        Describes what would happen if you executed the command without actually executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.Management.Automation.PSEventArgs
        You can pipe events from Get-Event to Remove-Event.

OUTPUTS
    None
        The cmdlet does not generate any output.

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.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Remove-Event -SourceIdentifier “ProcessStarted”

    Description
    ———–
    This command deletes events with a source identifier of “Process Started” from the event queue.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Remove-Event -eventIdentifier 30

    Description
    ———–
    This command deletes the event with an event ID of 30 from the event queue.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Get-Event | Remove-Event

    Description
    ———–
    This command deletes all events from the event queue.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135247
    Register-ObjectEvent
    Register-EngineEvent
    Register-WmiEvent
    Unregister-Event
    Get-Event
    New-Event
    Remove-Event
    Wait-Event

Register-EngineEvent

NAME
    Register-EngineEvent

SYNOPSIS
    Subscribes to events that are generated by the Windows PowerShell engine and by the New-Event cmdlet.

SYNTAX
    Register-EngineEvent [-SourceIdentifier] <string> [[-Action] <scriptblock>] [-Forward] [-MessageData <psobject>] [-SupportEvent] [<CommonParameters>]

DESCRIPTION
    The Register-EngineEvent cmdlet subscribes to events that are generated by the Windows PowerShell engine and the New-Event cmdlet. Use the SourceIdentifier parameter to specify the event.

    You can use this cmdlet to subscribe to the “Exiting” engine event and events generated by the New-Event cmdlet. These events are automatically added to your event queue in your session without subscribing. However, subscribing lets you forward the events, specify an action to respond to the events, and cancel the subscription.

    When the subscribed event is raised, it is added to the event queue in your session. To get events in the event queue, use the Get-Event cmdlet.

    When you subscribe to a event, an event subcriber is added to your session. To get the event subscribers in the session, use the Get-EventSubscriber cmdlet. To cancel the subscription, use the Unregister-Event cmdlet, which deletes the event subscriber from the session.

PARAMETERS
    -Action <scriptblock>
        Specifies commands to handle the events. The commands in the Action run when an event is raised, instead of sending the event to the event queue. Enclose the commands in braces ( { } ) to create a script block.

        The value of the Action parameter can include the $Event, $EventSubscriber, $Sender, $SourceEventArgs, and $SourceArgs automatic Variables, which provide information about the event to the Action script block. For more information, see about_Automatic_Variables.

        When you specify an action, Register-EngineEvent returns an event job object that represents that action. You can use the Job cmdlets to manage the event job.

        Required?                    false
        Position?                    102
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Forward [<SwitchParameter>]
        Sends events for this subscription to the session on the local computer. Use this parameter when you are registering for events on a remote computer or in a remote session.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?     false
        Accept wildcard characters? false

    -MessageData <psobject>
        Specifies additional data associated with the event. The value of this parameter appears in the MessageData property of the event object.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SourceIdentifier <string>
        Specifies the source identifier of the event to which you are subscribing. The source identifier must be unique in the current session. This parameter is required.

        The value of this parameter appears in the value of the SourceIdentifier property of the subscriber object and of all event objects associated with this subscription.

        Required?                    true
        Position?                    101
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SupportEvent [<SwitchParameter>]
        Hides the event subscription. Use this parameter when the current subscription is part of a more complex event registration mechanism and it should not be discovered independently.

        To view or cancel a subscription that was created with the SupportEvent parameter, use the Force parameter of the Get-EventSubscriber and Unregister-Event cmdlets.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe input to Register-EngineEvent.

OUTPUTS
    None or System.Management.Automation.PSEventJob
        If you use the Action parameter, Register-EngineEvent returns a System.Management.Automation.PSEventJob object. Otherwise, it does not generate any output.

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.

    ————————– EXAMPLE 1 ————————–

    C:\PS>$s = New-PSSession -computername Server01, Server02

    C:\PS> Invoke-Command -session $s { Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -Forward }

    Description
    ———–
    This command registers for a Windows PowerShell engine event on two remote computers.

    The first command creates a PSSession on each of the remote computers.

    The second command uses the Invoke-Command cmdlet to run the Register-EngineEvent command in the remote sessions.

    The Register-EngineEvent command uses the SourceIdentifier parameter to identify the event. It uses the Forward parameter to forward the events from the remote session to the local session.

    ————————– EXAMPLE 2 ————————–

    C:\PS>$j = Register-EngineEvent -SourceIdentifier PowerShell.ProcessCreated -Action { $processName | Add-Content processLog.txt }

    C:\PS> Get-Event

    C:\PS> $results = $j | Receive-Job

    C:\PS> Unregister-Event PowerShell.ProcessCreated

    Description
    ———–
    This command shows how to use the Job cmdlets to manage the event job object that Register-EngineEvent returns when you use the Action parameter.

    An event job is managed just like any other Windows PowerShell job. For more information, see about_jobs. In this example, the Receive-Job cmdlet is used to get the results of the job.

    To delete the job from the session, use Remove-Job. To cancel your event subscription, use the Unregister-Event cmdlet. To delete the events in the event queue, use Remove-Event.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135243
    Register-ObjectEvent
    Register-WmiEvent
    Unregister-Event
    Get-Event
    New-Event
    Remove-Event
    Wait-Event
    Get-Job
    Receive-Job
    Remove-Job
    Wait-Job

New-Event

NAME
    New-Event

SYNOPSIS
    Creates a new event.

SYNTAX
    New-Event [-SourceIdentifier] <string> [[-Sender] <psobject>] [[-EventArguments] <PSObject[]>] [[-MessageData] <psobject>] [<CommonParameters>]

DESCRIPTION
    The New-Event cmdlet creates a new custom event.

    You can use custom events to notify users about state changes in your program and any change that your program can detect, including hardware or system conditions, application status, disk status, network status, or the completion of a background job.

    Custom events are automatically added to the event queue in your session whenever they are raised; you do not need to subscribe to them. However, if you want to forward an event to the local session or specify an action to respond to the event, use the Register-EngineEvent cmdlet to subscribe to the custom event.

    When you subscribe to a custom event, the event subscriber is added to your session. If you cancel the event subscription by using the Unregister-Event cmdlet, the event subscriber and custom event are deleted from the session. If you do not subscribe to the custom event, to delete the event, you must change the program conditions or close the Windows PowerShell session.

PARAMETERS
    -EventArguments <PSObject[]>
        Specifies an object that contains options for the event.

        Required?                    false
        Position?                    3
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -MessageData <psobject>
        Specifies additional data associated with the event. The value of this parameter appears in the MessageData property of the event object.

        Required?                    false
        Position?                    4
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Sender <psobject>
        Specifies the object that raises the event. The default is the Windows PowerShell engine.

        Required?                    false
        Position?                    2
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SourceIdentifier <string>
        Specifies a name for the new event. This parameter is required, and it must be unique in the session.

        The value of this parameter appears in the SourceIdentifier property of the events.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe input to this cmdlet.

OUTPUTS
    System.Management.Automation.PSEventArgs

NOTES

        The new custom event, the event subscription, 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.

    ————————– EXAMPLE 1 ————————–

    C:\PS>New-Event -SourceIdentifier Timer -Sender windows.timer -MessageData “Test”

    Description
    ———–
    This command creates a new event in the Windows PowerShell event queue. It uses a Windows.Timer object to send the event.

    ————————– EXAMPLE 2 ————————–

    C:\PS>function Enable-ProcessCreationEvent
    {
     $query = New-Object System.Management.WqlEventQuery “__InstanceCreationEvent”, (New-Object TimeSpan 0,0,1), “TargetInstance isa ‘Win32_Process'”
     $processWatcher = New-Object System.Management.ManagementEventWatcher $query
     $identifier = “WMI.ProcessCreated”

     Register-ObjectEvent $processWatcher “EventArrived” -SupportEvent $identifier -Action {
         [void] (New-Event -sourceID “PowerShell.ProcessCreated” -Sender $args[0] -EventArguments $args[1].SourceEventArgs.NewEvent.TargetInstance)
     }
    }

    Description
    ———–
    This sample Function uses the New-Event cmdlet to raise an event in response to another event. The command uses the Register-ObjectEvent cmdlet to subscribe to the Windows Management Instrumentation (WMI) event that is raised when a new process is created. The command uses the Action parameter of the cmdlet to call the New-Event cmdlet, which creates the new event.

    Because the events that New-Event raises are automatically added to the Windows PowerShell event queue, you do not need to register for that event.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135234
    Register-ObjectEvent
    Register-EngineEvent
    Register-WmiEvent
    Unregister-Event
    Get-Event
    Remove-Event
    Wait-Event

Get-Event

NAME
    Get-Event

SYNOPSIS
    Gets the events in the event queue.

SYNTAX
    Get-Event [-EventIdentifier] <int> [<CommonParameters>]

    Get-Event [[-SourceIdentifier] <string>] [<CommonParameters>]

DESCRIPTION
    The Get-Event cmdlet gets events in the Windows PowerShell event queue for the current session. You can get all events or use the EventIdentifier or SourceIdentifier parameters to specify the events.

    When an event occurs, it is added to the event queue. The event queue includes events for which you have registered, events created by using the New-Event cmdlet, and the event that is raised when Windows PowerShell exits. You can use Get-Event or Wait-Event to get the events.

    This cmdlet does not get events from the Event Viewer logs. To get those events, use Get-WinEvent or Get-EventLog.

PARAMETERS
    -EventIdentifier <int>
        Gets only the events with the specified event identifier.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -SourceIdentifier <string>
        Gets only events with the specified source identifier. The default is all events in the event queue. Wildcards are not permitted.

        Required?                    false
        Position?                    1
        Default value                All events
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe input to this cmdlet.

OUTPUTS
    System.Management.Automation.PSEventArgs
        Get-Event returns a PSEventArgs object for each event. To see a description of this object, type “Get-Help Get-Event -full” and see the Notes section of the help topic.

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.

        The Get-Event cmdlet returns a PSEventArgs object (System.Management.Automation.PSEventArgs) with the following properties.

        — ComputerName: The name of the computer on which the event occurred. This property value is populated only when the event is forwarded from a remote computer.

        — RunspaceId: A GUID that uniquely identifies the session in which the event occurred. This property value is populated only when the event is forwarded from a remote computer.

        — EventIdentifier: An integer (Int32) that uniquely identifies the event notification in the current session.

        — Sender: The object that generated the event. In the value of the Action parameter, the $Sender automatic Variable contains the sender object.

        — SourceEventArgs: The first parameter that derives from EventArgs, if it exists. For example, in a timer elapsed event in which the signature has the form “Object sender, Timers.ElapsedEventArgs e”, the SourceEventArgs property would contain the Timers.ElapsedEventArgs. In the value of the Action parameter, the $SourceEventArgs automatic Variable contains this value.

        — SourceArgs: All parameters of the original event signature. For a standard event signature, $args[0] represents the sender, and $args[1] represents the SourceEventArgs. In the value of the Action parameter, the $SourceArgs automatic Variable contains this value.

        — SourceIdentifier: A string that identifies the event subscription. In the value of the Action parameter, the SourceIdentifier property of the $Event automatic Variable contains this value.

        — TimeGenerated: A DateTime object that represents the time at which the event was generated. In the value of the Action parameter, the TimeGenerated property of the $Event automatic Variable contains this value.

        –MessageData: Data associated with the event subscription. Users specify this data when they register an event. In the value of the Action parameter, the MessageData property of the $Event automatic Variable contains this value.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-Event

    Description
    ———–
    This command gets all events in the event queue.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Get-Event -SourceIdentifier “PowerShell.ProcessCreated”

    Description
    ———–
    This command gets events in which the value of the SourceIdentifier property is “PowerShell.ProcessCreated”.

    ————————– EXAMPLE 3 ————————–

    C:\PS>$events = Get-Event

    C:\PS> $events[0] | Format-List -property *

    ComputerName     :
    RunspaceId     : c2153740-256d-46c0-a57c-b805917d1b7b
    EventIdentifier : 1
    Sender         : System.Management.ManagementEventWatcher
    SourceEventArgs : System.Management.EventArrivedEventArgs
    SourceArgs     : {System.Management.ManagementEventWatcher, System.Management.EventArrivedEventArgs}
    SourceIdentifier : ProcessStarted
    TimeGenerated    : 11/13/2008 12:09:32 PM
    MessageData     :

    C:\PS> Get-Event | where {$_.TimeGenerated -ge “11/13/2008 12:15:00 PM”}

    ComputerName     :
    RunspaceId     : c2153740-256d-46c0-a57c-b8059325d1a0
    EventIdentifier : 1
    Sender         : System.Management.ManagementEventWatcher
    SourceEventArgs : System.Management.EventArrivedEventArgs
    SourceArgs     : {System.Management.ManagementEventWatcher, System.Management.EventArrivedEventArgs}
    SourceIdentifier : ProcessStarted
    TimeGenerated    : 11/13/2008 12:15:00 PM
    MessageData     :

    Description
    ———–
    This example shows how to get events by using properties other than SourceIdentifier.

    The first command gets all events in the event queue and saves them in the $events Variable.

    The second command uses array notation to get the first (0-index) event in the array in the $events Variable. The command uses a pipeline operator (|) to send the event to the Format-List command, which displays all properties of the event in a list. This allows you to examine the properties of the event object.

    The third command shows how to use the Where-Object cmdlet to get an event
    based on the time that it was generated.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Get-Event -eventIdentifier 2

    Description
    ———–
    This command gets the event with an event identifier of 2.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113453
    Register-ObjectEvent
    Register-EngineEvent
    Register-WmiEvent
    Unregister-Event
    New-Event
    Remove-Event
    Wait-Event

Get-EventSubscriber

NAME
    Get-EventSubscriber

SYNOPSIS
    Gets the event subscribers in the current session.

SYNTAX
    Get-EventSubscriber [-SubscriptionId] <int> [[-Force]] [<CommonParameters>]

    Get-EventSubscriber [[-SourceIdentifier] <string>] [[-Force]] [<CommonParameters>]

DESCRIPTION
    The Get-EventSubscriber cmdlet gets the event subscribers in the current session.

    When you subscribe to an event by using a Register event cmdlet, an event subscriber is added to your Windows PowerShell session, and the events to which you subscribed are added to your event queue whenever they are raised. To cancel an event subscription, delete the event subscriber by using the Unregister-Event cmdlet.

PARAMETERS
    -Force [<SwitchParameter>]
        Gets all event subscribers, including subscribers for events that are hidden by using the SupportEvent parameter of Register-ObjectEvent, Register-WmiEvent, and Register-EngineEvent.

        Required?                    false
        Position?                    2
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SourceIdentifier <string>
        Gets only the event subscribers with the specified SourceIdentifier property value. By default, Get-EventSubscriber gets all event subscribers in the session. Wildcards are not permitted. This parameter is case-sensitive.

        Required?                    false
        Position?                    1
        Default value                All event subscribers
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -SubscriptionId <int>
        Gets only the specified subscription identifier. By default, Get-EventSubscriber gets all event subscribers in the session.

        Required?                    true
        Position?                    1
        Default value                None
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe input to this cmdlet.

OUTPUTS
    System.Management.Automation.PSEventSubscriber
        Get-EventSubscriber returns an object that represents each event subscriber.

NOTES

        The New-Event cmdlet, which creates a custom event, does not generate a subscriber. Therefore, the Get-EventSubscriber cmdlet will not find a subscriber object for these events. However, if you use the Register-EngineEvent cmdlet to subscribe to a custom event (in order to forward the event or to specify an action), Get-EventSubscriber will find the subscriber that Register-EngineEvent generates.

        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.

    ————————– EXAMPLE 1 ————————–

    C:\PS>$timer = New-Object Timers.Timer

    C:\PS> $timer | Get-Member -Type Event

    C:\PS> Register-ObjectEvent -inputObject $timer -EventName Elapsed -SourceIdentifier Timer.Elapsed

    C:\PS> Get-EventSubscriber

    C:\PS> $timer = New-Object Timers.Timer

    C:\PS> $timer | Get-Member -Type Event

     TypeName: System.Timers.Timer

    Name     MemberType Definition
    —-     ———- ———-
    Disposed Event     System.EventHandler Disposed(System.Object, System.EventArgs)
    Elapsed Event     System.Timers.ElapsedEventHandler Elapsed(System.Object, System.Timers.ElapsedEventArgs)

    C:\PS> Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Timer.Elapsed

    C:\PS> Get-EventSubscriber

    SubscriptionId : 4
    SourceObject     : System.Timers.Timer
    EventName        : Elapsed
    SourceIdentifier : Timer.Elapsed
    Action         :
    HandlerDelegate :
    SupportEvent     : False
    ForwardEvent     : False

    Description
    ———–
    This example uses a Get-EventSubscriber command to get the event subscriber for a timer event.

    The first command uses the New-Object cmdlet to create an instance of a timer object. It saves the new timer object in the $timer Variable.

    The second command uses the Get-Member cmdlet to display the events that are available for timer objects. The command uses the Type parameter of the Get-Member cmdlet with a value of Event.

    The third command uses the Register-ObjectEvent cmdlet to register for the Elapsed event on the timer object.

    The fourth command uses the Get-EventSubscriber cmdlet to get the event subscriber for the Elapsed event.

    ————————– EXAMPLE 2 ————————–

    C:\PS>$timer = New-Object Timers.Timer

    C:\PS> $timer.Interval = 500

    C:\PS> Register-ObjectEvent -inputObject $timer -eventName Elapsed -SourceIdentifier Timer.Random -Action { $random = Get-Random -Min 0 -Max 100 }

    Id Name         State     HasMoreData Location Command
    — —-         —–     ———– ——– ——-
    3 Timer.Random NotStarted False                 $random = Get-Random

    C:\PS> $timer.Enabled = $true

    C:\PS> $subscriber = Get-EventSubcriber -sourceIdentifer Timer.Random

    C:\PS> ($subscriber.action).gettype().fullname
    PSEventJob

    C:\PS> $subscriber.action | Format-List -property *

    State         : Running
    Module        : __DynamicModule_6b5cbe82-d634-41d1-ae5e-ad7fe8d57fe0
    StatusMessage :
    HasMoreData : True
    Location     :
    Command     : $random = Get-Random -Min 0 -Max 100
    JobStateInfo : Running
    Finished     : System.Threading.ManualResetEvent
    InstanceId    : 88944290-133d-4b44-8752-f901bd8012e2
    Id            : 1
    Name         : Timer.Random
    ChildJobs     : {}
    …

    C:\PS> & $subscriber.action.module {$random}
    96

    C:\PS> & $subscriber.action.module {$random}
    23

    Description
    ———–
    This example shows how to use the dynamic module in the PSEventJob object in the Action property of the event subscriber.

    The first command uses the New-Object cmdlet to create a timer object. The second command sets the interval of the timer to 500 (milliseconds).

    The third command uses the Register-ObjectEvent cmdlet to register the Elapsed event of the timer object. The command includes an action that handles the event. Whenever the timer interval elapses, an event is raised and the commands in the action run. In this case, the Get-Random cmdlet generates a random number between 0 and 100 and saves it in the $random Variable. The source identifier of the event is Timer.Random.

    When you use an Action parameter in a Register-ObjectEvent command, the command returns a PSEventJob object that represents the action.

    The fourth command enables the timer.

    The fifth command uses the Get-EventSubscriber cmdlet to get the event subscriber of the Timer.Random event. It saves the event subscriber object in the $subscriber Variable.

    The sixth command shows that the Action property of the event subscriber object contains a PSEventJob object. In fact, it contains the same PSEventJob object that the Register-ObjectEvent command returned.

    The seventh command uses the Format-List cmdlet to display all of the properties of the PSEventJob object in the Action property in a list. The result reveal that the PSEventJob object has a Module property that contains a dynamic script module that implements the action.

    The remaining commands use the call operator (&) to invoke the command in the module and display the value of the $random Variable. You can use the call operator to invoke any command in a module, including commands that are not exported. In this case, the commands show the random number that is being generated when the Elapsed event occurs.

    For more information about modules, see about_modules.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135155
    Register-ObjectEvent
    Register-EngineEvent
    Register-WmiEvent
    Unregister-Event
    Get-Event
    New-Event
    Remove-Event
    Wait-Event