Register-ObjectEvent looks for a object instance in the required parameter InputObject. What is the syntax for an object’s static (Shared) event?
UPDATE: Correct syntax for TimeChanged:
$systemEvents = [Microsoft.Win32.SystemEvents]
$timeChanged = Register-ObjectEvent -InputObject $systemEvents
-EventName 'TimeChanged' -Action { Write-Host "Time changed" }
Unfortunately, the SystemEvents will not be signaled in PowerShell ISE. Here’s a sample using an object’s staic event that works everywhere:
$networkInformation = [System.Net.NetworkInformation.NetworkChange];
$networkAddressChanged = Register-ObjectEvent -InputObject $networkInformation
-EventName 'NetworkAddressChanged'
-Action { Write-Host "NetworkAddressChanged event signaled" }
If you assign a static type to a variable, you can subscribe to static events.
For example:
To find any static events a type may have, you can use Get-Member with the -Static switch
EDIT:
I did notice when trying to access [Microsoft.Win32.SystemEvents] events, that I needed to be running in an elevated prompt (on Vista and above) in order to access the messages.