How to Perform a Synchronous Configuration Manager Query by Using Managed Code
Updated: November 1, 2013
Applies To: System Center 2012 Configuration Manager, System Center 2012 Configuration Manager SP1, System Center 2012 R2 Configuration Manager
To perform a synchronous query by using the managed SMS Provider, you use WqlConnectionManager.QueryProcessor.ExecuteQuery method.
The M:Microsoft.ConfigurationManagement.ManagementProvider.QueryProcessorBase.ExecuteQuery(System.String,System.Collections.Generic.Dictionary{System.String,System.Object}) method takes a WQL query string and optional context information for the call. An T:Microsoft.ConfigurationManagement.ManagementProvider.IResultObject is returned containing the objects found in the query.
To perform a synchronous query
Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.
Using the WqlConnectionManager object you obtain in step one, call the QueryProcessor object ExecuteQuery method to query SMS Provider and get an IResultObject containing a collection of query results.
Example
The following code example shows how to make a synchronous query for the available packages by using ExecuteQuery.
For information about calling the sample code, see Calling Configuration Manager Code Snippets.
public void QueryPackages(WqlConnectionManager connection)
{
try
{
IResultObject query = connection.QueryProcessor.ExecuteQuery("Select * from SMS_Package");
foreach (IResultObject o in query)
{
Console.WriteLine(o["Name"].StringValue);
o.Dispose();
}
}
catch (SmsException ex)
{
Console.WriteLine("Failed to query packages: " + ex.Message);
throw;
}
}
Compiling the Code
System
System.Collections.Generic
System.ComponentModel
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
microsoft.configurationmanagement.managementprovider
adminui.wqlqueryengine
Robust Programming
The Configuration Manager exceptions that can be raised are T:Microsoft.ConfigurationManagement.ManagementProvider.SmsConnectionException and T:Microsoft.ConfigurationManagement.ManagementProvider.SmsQueryException. These can be caught together with T:Microsoft.ConfigurationManagement.ManagementProvider.SmsException.
About Configuration Manager Objects
Configuration Manager Lazy Properties
How to Call a Configuration Manager Object Class Method by Using Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using Managed Code
How to Create a Configuration Manager Object by Using Managed Code
How to Modify a Configuration Manager Object by Using Managed Code
How to Perform an Asynchronous Configuration Manager Query by Using Managed Code
How to Read a Configuration Manager Object by Using Managed Code
How to Read Lazy Properties by Using Managed Code
How to Use Configuration Manager Objects with Managed Code
Configuration Manager Extended WMI Query Language
Configuration Manager Result Sets
Configuration Manager Special Queries
Configuration Manager Queries
This example method has the following parameters:
Parameter
Type
Description
connection
Managed: WqlConnectionManager
A valid connection to the SMS Provider.