Eric Schoonover
Eric Schoonover

Reputation: 48402

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?

Upvotes: 10

Views: 5006

Answers (1)

Keith Hill
Keith Hill

Reputation: 201672

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.

Upvotes: 11

Related Questions