Register-ObjectEvent

Subscribe 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]

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-ObjectEvent will return an event job object that represents the action. -EventName string
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 "Event
Arrived" and "Stopped." To find the event name of an event, use Get-Member. -Forward 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. -InputObject psobject
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. -MessageData psobject Additional data associated with the event. The value of this parameter appears in the MessageData property of the event object. -SourceIdentifier string
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 PowerShell assigns. 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-ObjectEvent subscribes to events that are generated by .NET Framework objects on the local or 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 Get-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

Subscribe to object events on remote computers, utilising a function:

# Create sessions
PS C:\> $sess = new-pssession -computername Server01, Server02
    
# run a script for each session which adds subscriptions on the Win32_Process:
PS C:\> invoke-command -session $sess -filepath ProcessCreationEvent.ps1

# get events in the queue
PS C:\> invoke-command -session $sess { get-event }
    
    # ProcessCreationEvent.ps1
    
    function EnableProcessCreationEvent 
    { 
       $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

“A beauty is a woman you notice; a charmer is one who notices you” ~ Adlai E. Stevenson

Related:

Register-EngineEvent - Subscribe to PowerShell events
Register-WmiEvent - Subscribe to a WMI event
Get-Event - Get events in the event queue


© Copyright SS64.com 1999-2014
Some rights reserved