In VBScript, WMI can execute asynchronous queries by using ExecQueryAsync and WbemScripting.SWbemSink, for example:
Sub Main()
Set sink = WScript.CreateObject("wbemscripting.swbemsink","sink_")
Set wmi = GetObject("winmgmts://./root/cimv2")
wmi.ExecQueryAsync sink, "select * from Win32_Process"
Do
wscript.sleep 1000
Loop
End Sub
Sub sink_OnObjectReady(process, octx)
MsgBox process.Name
End Sub
Main()
The functions (sink_OnObjectReady
and sink_OnCompleted
) are callbacks which are passed implicitly to sink
.
But in PowerShell, I cannot create callbacks and pass them to sink
.
$sink = New-Object -ComObject wbemscripting.swbemsink
Here, $sink
only has one method Cancel()
and it doesn't have OnObjectReady
, etc.
I still believe that PowerShell can do this because it can invoke .NET and COM easily.
See also: https://learn.microsoft.com/en-us/windows/win32/wmisdk/making-an-asynchronous-call-with-vbscript
Thanks