views:

159

answers:

1

How can you handle events thrown by .NET object using PowerShell v2? Can someone point me to a simple code sample?

+4  A: 

Look at the docs on the Register-ObjectEvent cmdlet. Be sure to use the -full parameter. It has some good examples of usage including this one:

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}

You might also want to check out this PowerShell Eventing QuickStart blog post. Note that some of the cmdlet names have changed e.g. Get/Remove-PsEvent is now just Get/Remove-Event.

Keith Hill