Object Eventing
In my previous article, I started a series of articles on eventing in PowerShell. With eventing, you can take action, or at least be notified, that something has happened. As IT Pros, this could include events such as files and folder creation, services starting, or processes stopping. Last time I showed you how to use an engine event to track when a PowerShell session ends. In that scenario, we use Register-EngineEvent
. Personally, I have not found much use for this type of eventing other than what I demonstrated.
The more useful tool is Register-ObjectEvent
. With this command, you can register event subscriptions for objects from the .NET Framework. This includes WMI/CIM instances, although I will discuss CIMIndication events in a future article.
Here is a very basic example of how this works. I need an object that has a defined event. I’m going to use a process.
PS C:\> $p = Get-Process notepad
PS C:\> $p | Get-Member -MemberType Event | Select-Object Name
Name
----
Disposed
ErrorDataReceived
Exited
OutputDataReceived