Register-EngineEvent

Subscribe to events that are generated by the PowerShell engine and by New-Event

Syntax
      Register-EngineEvent [-SourceIdentifier] string 
         [[-Action] scriptblock] [-Forward] [-MessageData psobject]
            [-SupportEvent] [CommonParameters]

Key:
   -Action scriptblock
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. Register-EngineEvent will return an event job object that represents the action. -Forward Send events for this subscription to the session on the local computer. Use this parameter when registering for events on a remote computer or in a remote session. -MessageData psobject Additional data associated with the event. The value of this parameter appears in the MessageData property of the event object. -SourceIdentifier string
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 SourceIdentifier property of the subscriber object and of all event objects associated with this subscription. -SupportEvent Hide 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 -SupportEvent, use the Force parameter of Get-EventSubscriber and Unregister-Event. CommonParameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable, -OutBuffer -OutVariable.

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

Subscribing allows you to forward the events and/or specify an action to respond to the events.

To cancel a subscription, use Unregister-Event

Examples

Create a PSSession on two remote computers, then run run the Register-EngineEvent on each of them, forwading events back to the local session:

PS C:\> $sess = new-pssession -computername Server64, Server65

PS C:\> invoke-command -session $sess { Register-EngineEvent -sourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -forward }


Get the event subscriber of the Timer.Random event.

PS C:\> $demojobs = register-engineEvent -sourceIdentifier PowerShell.ProcessCreated -action { $processName | add-content processLog.txt }

C:\PS> get-event

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

C:\PS> unregister-event PowerShell.ProcessCreated

“History is the version of past events that people have decided to agree upon” ~ Napoleon Bonaparte

Related:

Register-ObjectEvent - Subscribe to .NET events
Register-WmiEvent - Subscribe to a WMI event
Get-Event - Get events in the event queue


© Copyright SS64.com 1999-2014
Some rights reserved