Hi i have a monitoring script (with a winforms gui) that is always running in the back. Unfortunately this annoys users when they try to manually shutdown the computer, cause it provokes the "this app is preventing windows shutdown"-screen.
So i need a a reliable way to automatically close the script when a shutdown was initiated.
I tried subscribing to the SessionEnding- and SessionEnded-event, but it did not work:
$sysevent = [microsoft.win32.systemevents]
Register-ObjectEvent -InputObject $sysevent -EventName "SessionEnding" -Action { Exitfunction }
UPDATE: Its like this at the moment:
$sysevent = [microsoft.win32.systemevents]
Register-ObjectEvent -InputObject $sysevent -EventName "SessionEnding" -Action { [Windows.Forms.MessageBox]::Show("Shutdown!", "", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Warning)}
Register-ObjectEvent -InputObject $sysevent -EventName "SessionEnded" -Action { [Windows.Forms.MessageBox]::Show("Shutdown!", "", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Warning)}
Register-WmiEvent -Class win32_computerShutdownEvent -Action { [Windows.Forms.MessageBox]::Show("Shutdown!", "", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Warning)}
I added a messagebox to see if one of these events would in fact fire, but maybe the code executed by the action scriptblock was faulty. No luck. None of these events kick in when i try to shutdown a windows 8.1 system. There are no errors inside the events when i read them with get-job. The state is in all of them "not started".
Any ideas why?