The following script (which will stop IIS service when AppMgmt service is stopped) runs successfully in "PowerGUI Script Editor", but when running it from console "powershell -file path_to_script" it does not execute action when the event is fired:
function Watch-MyService()
{
$query = "SELECT * FROM __InstanceModificationEvent WITHIN 2 " +
"WHERE TargetInstance Isa 'Win32_Service' " +
"AND TargetINstance.Name = 'AppMgmt'" +
"AND TargetInstance.State = 'Stopped'"
$action =
{
Write-Host "stopping service # 2"
Stop-Service 'W3SVC' -Force
}
Write-Host "registering event"
Register-WMIEvent -query $query -SourceIdentifier "ControllerSvcEvent" -action $action
}
Watch-MyService
in console, I only see:
Registering event
but nothing else displays when stopping the 'AppMgmt' service, no output to the console, & the IIS service stays unaffected.
the console is started with admin privileges.