| title | Read an Object by Using Managed Code |
|---|---|
| description | The GetInstance method takes a string that identifies a specific object instance and returns an IResultObject that is used to access the object. |
| ms.date | 09/20/2016 |
| ms.subservice | sdk |
| ms.topic | how-to |
| ms.collection | tier3 |
To read a Configuration Manager object instance by using the managed SMS Provider, use WqlConnectionManager.GetInstance. The GetInstance method takes a string that identifies a specific object instance and returns an IResultObject object that is used to access the object.
The following example function shows the name and description for a supplied package identifier.
-
Set up a connection to the SMS Provider. For more information, see How to Connect to an SMS Provider in Configuration Manager by Using Managed Code.
-
Call WqlConnectionManager class GetInstance method to get the IResultObject object for the object you want.
-
Display the properties of the IResultObject.
The following code example shows how to read a Configuration Manager object.
For information about calling the sample code, see Calling Configuration Manager Code Snippets.
public void DisplayPackageName(WqlConnectionManager connection, string packageID)
{
try
{
// Get the package.
IResultObject package = connection.GetInstance(@"SMS_Package.PackageID='" + packageID + "'");
Console.WriteLine("Package Name: " + package["Name"].StringValue);
Console.WriteLine("Package Description: " + package["Description"].StringValue);
}
catch (SmsException ex)
{
Console.WriteLine("Failed to get package. Error: " + ex.Message);
throw;
}
}
This example method has the following parameters:
| Parameter | Type | Description |
|---|---|---|
Connection |
- Managed: WqlConnectionManager |
- A valid connection to the SMS Provider. |
PackageID |
- Managed: String |
A valid package identifier. Obtained from the SMS_Package class PackageID property. |
System
System.Collections.Generic
System.ComponentModel
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
microsoft.configurationmanagement.managementprovider
adminui.wqlqueryengine
The Configuration Manager exceptions that can be raised are SmsConnectionException and SmsQueryException. These can be caught together with SmsException.
Objects overview Configuration Manager Lazy Properties How to Call a Configuration Manager Object Class Method by Using Managed Code 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 Lazy Properties by Using Managed Code