Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling events with PowerShell?

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

like image 300
Eric Schoonover Avatar asked Sep 25 '09 17:09

Eric Schoonover


1 Answers

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.

like image 134
Keith Hill Avatar answered Oct 16 '22 20:10

Keith Hill