1

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

Charles
  • 11
  • 2
  • A [Google search](https://www.google.com/search?q=powershell+wmi+asynchronous) turned up - after a little checking of other returns - this [Svendsen Tech](https://www.powershelladmin.com/wiki/Get-wmiobject_wrapper.php) solution to your question. – Jeff Zeitlin Jun 09 '22 at 11:22
  • @JeffZeitlin This is not what I am looking for. This script is designed to "retrieve and collect data from a (potentially large) list of computers". I wanted to execute async WMI query on local computer. – Charles Jun 12 '22 at 01:04
  • A list of computers can consist of a single computer, and that computer can be the local computer. VBScript and PowerShell, while they have similar capabilities, represent different philosophies for handling .NET objects, and trying to translate VBScript to PowerShell rarely works well, in my experience. – Jeff Zeitlin Jun 13 '22 at 12:19

0 Answers0