How to Perform an Asynchronous Configuration Manager Query by Using WMI

 

Updated: November 1, 2013

Applies To: System Center 2012 Configuration Manager, System Center 2012 Configuration Manager SP1, System Center 2012 R2 Configuration Manager

In System Center 2012 R2 Configuration Manager, you perform an 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.

System_CAPS_noteNote

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 System Center 2012 R2 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.

Show: