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.
|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
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.
Create an OnObjectReady subroutine to handle objects by the query.
Create an OnCompleted subroutine to handle query completion.
Using the SWbemServices object you obtain from step one, use ExecQueryAsync object to query System Center 2012 R2 Configuration Manager objects asynchronously.
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.
|Visual Basic Script||Copy Code|
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:
A valid connection to the SMS Provider.
TasksHow 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
ConceptsConfiguration Manager Objects Overview
How to Use Configuration Manager Objects with WMI
Configuration Manager Extended WMI Query Language
Configuration Manager Result Sets
Configuration Manager Special Queries
Configuration Manager Queries