PowerShell 2.0: One Cmdlet at a Time 96 New-Event

Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the New-Event cmdlet.

What can I do with it?

Create a custom event.

Example:

The built-in PowerShell help has a great example for New-Event. It uses New-Event to create a custom event based on a reaction to another event.

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) } }

You will notice that if you execute this function and then create a new process a new event is automatically generated.

How could I have done this in PowerShell 1.0?

PowerShell engine events are a new feature in PowerShell 2.0.

1000 things 1% better