Chapter 17. Working with .NET and events

 

This chapter covers

  • .NET and PowerShell
  • Real-time events

I love it when a plan comes together!

Col. John “Hannibal” Smith, The A-Team

The good news is that PowerShell is .NET -based and works with .NET objects. The not-quite-so-good news is that not all of .NET is immediately available when you open PowerShell. Some .NET functionality is available through cmdlets—for the rest you need to access the .NET classes in your code. PowerShell doesn’t load the entire .NET framework, so you’ll need to load assemblies before you can use them. Once an assembly is loaded, you have access to the rich .NET functionality, including creating graphical applications in PowerShell.

Note

PowerShell v6 uses .NET core which has further restrictions as described in the appendix.

Windows is an event-based system. You can use PowerShell to access events from a number of sources. Your scripts can then either display information about the event or take action based on the event.

We’ll start with .NET before moving on to events.

17.1. .NET and PowerShell

The original PowerShell concept was to have cmdlets for every task, but that goal wasn’t achievable in the time frame available for the release of PowerShell v1. Instead, the team made the decision to make it easier to work directly with the .NET Framework. That way, although it might not be as easy to do everything the way the team wanted, at least it would be possible.

17.2. Real-time events

17.3. Summary