O'Reilly logo

Pro Windows PowerShell by Hristo Deshev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

22.7. Using Script Blocks As Event Handlers

The previous example showed something nice, but it also hinted about something that is not so pleasant: it is very difficult to deal with different events and distinguish among them. The best way to handle this problem is to use the helper eventhandler.ps1 script that ships with PSEventing and its utility functions:

  • Add-EventHandler($variable, $eventName, $script) will attach a script block as a handler for the given event.

  • Remove-EventHandler($variable, $eventName) lets us detach a previously attached event handler.

  • Do-Events($onlyOnce) will wait for events and process their event handlers one or more times.

The eventhandler.ps1 file is a part of the official PSEventing release files, but it is not ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required