Wondering how to handle .NET events in PowerShell?

Nrgghg,  I feel another PowerShell project coming on ...  enter PowerShell Eventing 0.5 Beta! With the magic of lightweight codegen, aka LCG, a smidgeon of reflection (well, quite a bit) and some inspiration, I managed to cough up this latest project.

While you cannot directly bind scriptblocks as eventhandlers, you can automatically route events in realtime to a background queue and deal with them in your time with a special Get-Event cmdlet. There is a sample walkthrough on the home page of the Wiki, and you can download a Sql backup script which shows progress reporting, all in script!

PS 1# Add-PSSnapin PSEventing                                                                      
PS 2# $wc = new-object system.net.webclient                                                        
PS 3# get-eventbinding -IncludeUnboundEvents | ft -auto                                            
                                                                                                                        
VariableName   EventName               TypeName  Listening                                                              
------------   ---------               --------  ---------                                                              
wc             Disposed                WebClient     False                                                              
wc             DownloadDataCompleted   WebClient     False                                                              
wc             DownloadFileCompleted   WebClient     False                                                              
wc             DownloadProgressChanged WebClient     False                                                              
wc             DownloadStringCompleted WebClient     False                                                              
wc             OpenReadCompleted       WebClient     False                                                              
wc             OpenWriteCompleted      WebClient     False                                                              
wc             UploadDataCompleted     WebClient     False                                                              
wc             UploadFileCompleted     WebClient     False                                                              
wc             UploadProgressChanged   WebClient     False                                                              
wc             UploadStringCompleted   WebClient     False                                                              
wc             UploadValuesCompleted   WebClient     False                                                              
                                                                                                                                                                                                                                        
PS 4# Connect-EventListener wc disposed -verbose                                                   
VERBOSE: Target is a WebClient                                                                                          
VERBOSE: Now listening for 'disposed' events from $wc                                                                   
PS 5# $wc.Dispose()                                                                                
PS 6# get-event | ft -auto                                                                         
                                                                                                                        
Occurred             Source      Name     Args                                                                          
--------             ------      ----     ----                                                                          
5/13/2007 8:04:20 PM variable:wc Disposed System.EventArgs                                                              
                                                                                                                                                 

Have fun!

blog comments powered by Disqus

About the author

Irish, PowerShell MVP, .NET/ASP.NET/SharePoint Developer, Budding Architect. Developer. Montrealer. Opinionated. Montreal, Quebec.

Month List

Page List