我希望异步接收WMI事件并使用SWbemSink对象处理它们。 Microsoft instructs调用IWbemServices.ExecNotificationQueryAsync传递一个SWbemSink实例。当事件发生时,它将传递给SWbemSink对象的OnObjectReady方法,以便客户端可以处理它。下面是如何在js中执行此操作的示例。我们怎样才能用Python做到这一点?我知道如何使用win32com.client与WMI进行交互,但是如何实现Python脚本以便以事件驱动的方式执行?
//Connects to default namespace (HKEY_LOCAL_MACHINE\Software\Microsoft\WBEM\Scripting\Default Namespace).
var wbemServices = GetObject("WinMgmts:{impersonationLevel=impersonate,(security)}");
//Create an event sink object.
var sink = WScript.CreateObject("WbemScripting.SWbemSink", "SINK_");
//Register an event query describing which events the client would like
//to receive.
wbemServices.ExecNotificationQueryAsync(
sink, "Select * From __InstanceOperationEvent within 0.5 Where TargetInstance ISA 'Win32_Process' and TargetInstance.Caption='explorer.exe'");
//Show a msg box to allow user to stop receiving events.
var wshShell = WScript.CreateObject("WScript.Shell");
wshShell.Popup("Click OK to stop listening for events...");
sink.Cancel();
//SWbemSink function called every time an event is ready.
function SINK_OnObjectReady(wbemObject, asyncContext) {
WScript.Echo("Event received: " + wbemObject.GetObjectText_());
}