Edit

Share via


How to Perform an Asynchronous Configuration Manager Query by Using WMI

In Configuration Manager, you perform a synchronous query for Configuration Manager objects by calling the SWbemServices object ExecQueryAsync method and by implementing a sink method to handle query results.

To handle each returned object, create an objWbemSink.OnObjectReady event subroutine. To be notified when the query is completed, create a objWbemSink.OnCompleted event subroutine.

Note

Lazy properties are not returned in asynchronous queries. For more information, see How to Read Lazy Properties by Using WMI.

To perform an asynchronous query

  1. Set up a connection to the SMS Provider. For more information, see How to Connect to an SMS Provider in Configuration Manager by Using WMI.

  2. Create an OnObjectReady subroutine to handle objects by the query.

  3. Create an OnCompleted subroutine to handle query completion.

  4. Using the SWbemServices object you obtain from step one, use ExecQueryAsync object to query Configuration Manager objects asynchronously.

Example

The following VBScript code example asynchronously queries for all SMS_Collection objects.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

Dim bdone
Sub QueryCollection(connection)

    Dim sink
    bdone = False

    Set sink = WScript.CreateObject("wbemscripting.swbemsink","sink_")

    ' Query for all collections.
    connection.ExecQueryAsync sink, "select * from SMS_Collection"

    ' Wait until all instances are returned.
    While Not bdone
        wscript.sleep 1000
    Wend
 End Sub

' The sink subroutine to handle the OnObjectReady
' event. This is called as each object returns.
Sub sink_OnObjectReady(collection, octx)
    WScript.Echo "CollectionID: " + collection.CollectionID
    WScript.Echo "Name: " + collection.Name
    Wscript.Echo
End Sub

' The sink subroutine to handle the OnCompleted event.
' This is called when all the objects are returned.
' The oErr parameter obtains an SWbemLastError object,
' if available from the provider.
Sub sink_OnCompleted(HResult, oErr, oCtx)
    WScript.Echo "All collections returned"
    bdone = true
End Sub

This example method has the following parameters:

Parameter Type Description
connection SWbemServices A valid connection to the SMS Provider.

See Also

Windows Management Instrumentation Objects overview How to Call a Configuration Manager Object Class Method by Using WMI How to Connect to an SMS Provider in Configuration Manager by Using WMI How to Create a Configuration Manager Object by Using WMI How to Delete a Configuration Manager Object by Using WMI How to Modify a Configuration Manager Object by Using WMI How to Perform a Synchronous Configuration Manager Query by Using WMI How to Read a Configuration Manager Object by Using WMI How to Read Lazy Properties by Using WMI Configuration Manager Extended WMI Query Language Configuration Manager Result Sets Configuration Manager Special Queries About queries