How can you handle events thrown by .NET object using PowerShell v2? Can someone point me to a simple code sample?
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:
$timer = New-Object Timers.Timer
$timer.Interval = 500
$timer.Start()
$job = Register-ObjectEvent -inputObject $timer -eventName Elapsed `
-sourceIdentifier Timer.Random `
-Action {$random = Get-Random -Min 0 -Max 100; $random}
Receive-Job $job
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With