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 ...

Get Pro Windows PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.