How to Call a Configuration Manager Object Class Method by Using Managed Code
To call a SMS Provider class method, in Configuration Manager, you use the ExecuteMethod method. You populate a Dictionary object with the method's parameters, and the return value is an IResultObject object that contains the result of the method call.
Note
To call a method on an object instance, use the ExecuteMethod method on the IResultObject object instance.
To call a Configuration Manager object class method
Set up a connection to the SMS Provider. For more information, see SMS Provider fundamentals.
Create the input parameters as a Dictionary object.
Using the WqlConnectionManager object instance, call ExecuteMethod and specify the class name and input parameters.
Retrieve the method return value from the ReturnValue property in the returned IResultObject object.
Example
The following example validates a collection rule query by calling the SMS_CollectionRuleQuery class ValidateQuery class method.
For information about calling the sample code, see Calling Configuration Manager Code Snippets.
public void ValidateQueryRule(WqlConnectionManager connection, string wqlQuery)
{
try
{
Dictionary<string,object> validateQueryParameters = new Dictionary<string,object>();
// Add the sql query as the WQLQuery parameter.
validateQueryParameters.Add("WQLQuery",wqlQuery);
// Call the method
IResultObject result=connection.ExecuteMethod("SMS_CollectionRuleQuery", "ValidateQuery", validateQueryParameters);
if (result["ReturnValue"].BooleanValue == true)
{
Console.WriteLine (wqlQuery + " is a valid query");
}
else
{
Console.WriteLine (wqlQuery + " is not a valid query");
}
}
catch (SmsException ex)
{
Console.WriteLine("Failed to validate query rule: ",ex.Message);
throw;
}
}
This example method has the following parameters:
Parameter | Type | Description |
---|---|---|
connection |
- Managed: WqlConnectionManager | A valid connection to the SMS Provider. |
wqlQuery |
- Managed: IResultObject | A WQL query string. For this example, SELECT * FROM SMS_R_System is a valid query. |
Compiling the Code
Namespaces
System
System.Collections.Generic
System.ComponentModel
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
Assembly
microsoft.configurationmanagement.managementprovider
adminui.wqlqueryengine
Robust Programming
The Configuration Manager exceptions that can be raised are SmsConnectionException and SmsQueryException. These can be caught together with SmsException.
See Also
Objects overview
How to Connect to a Configuration Manager Provider 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 Perform a Synchronous Configuration Manager Query by Using Managed Code
How to Read a Configuration Manager Object by Using Managed Code