Monitors WMI instance modification events for Win32_Service using an asynchronous event query.

Visual Basic
Edit|Remove
strComputer = "."
Set SINK = WScript.CreateObject("WbemScripting.SWbemSink","SINK_")
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.ExecNotificationQueryAsync SINK, _
 "SELECT * FROM __InstanceModificationEvent WITHIN 1 " & _
 "WHERE TargetInstance ISA 'Win32_Service'"
WScript.Echo "Waiting for service to be modified ..."
Do
   WScript.Sleep 1000
Loop

Sub SINK_OnObjectReady(objLatestEvent, objAsyncContext)

Wscript.Echo VbCrLf & "Service Name: " & objLatestEvent.TargetInstance.Name
Wscript.Echo "Status: " & objLatestEvent.TargetInstance.State
Wscript.Echo "Startup Type: " & objLatestEvent.TargetInstance.StartMode
Wscript.Echo "Time: " & Now

End Sub