提前过滤掉 __InstanceModificationEvent
代码运行查看cpu占用基本为0,不用在意这个吧
再说19楼说了,即使过滤掉,还是要对结果进行 case 或 Switch
非要过滤掉,vbs可以变通一下,当然代码非但没简洁反而更复杂了- s1 = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk' AND TargetInstance.DriveType = 2"
- s2 = "SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk' AND TargetInstance.DriveType = 2"
- Set oWshShell = CreateObject("WScript.Shell")
- Set oWMI = GetObject("Winmgmts:\\.\Root\Cimv2")
- Set oSWbemSink = WScript.CreateObject("WbemScripting.SWbemSink", "Sink_")
- Set o1 = CreateObject("WbemScripting.SWbemNamedValueSet")
- o1.Add "类型", "插入"
- Set o2 = CreateObject("WbemScripting.SWbemNamedValueSet")
- o2.Add "类型", "拔出"
- oWMI.ExecNotificationQueryAsync oSWbemSink, s1,,,, o1
- oWMI.ExecNotificationQueryAsync oSWbemSink, s2,,,, o2
- Do
- WScript.Sleep 100
- Loop
- Sub Sink_OnObjectReady(objWbemObject, objWbemAsyncContext)
- If objWbemAsyncContext.Item("类型") = "插入" Then msgbox "插入"
- If objWbemAsyncContext.Item("类型") = "拔出" Then msgbox "拔出"
- End Sub
复制代码
|