How to Read a WMI Object by Using System.Management
To read a Configuration Manager client Windows Management Instrumentation (WMI) object, in Configuration Manager, you use a ManagementObject
object to read the WMI object.
To read a WMI object
Set up a connection to the Configuration Manager client WMI namespace. For more information, see How to Connect to the Configuration Manager Client WMI Namespace by Using System.Management.
Create a
ManagementObject
object.Create a
ManagementPath
object with theManagementScope
path you obtain from step one.Assign the
ManagementPath
object to theManagementObject
path property.Call the
ManagementObject
object Get method to get the object from the WMI provider.Use the
ManagementObject
object to read the WMI provider object properties.
Example
The following C# code example gets the Configuration Manager client WMI object SMS_Client object and displays its properties.
For information about calling the sample code, see How to Call a WMI Class Method by Using System.Management.
void ReadObject(ManagementScope scope)
{
try // Gets an instance of a CCM_InstalledComponent.
{
// Get the object.
ManagementObject obj = new ManagementObject();
ManagementPath path = new ManagementPath(scope.Path + ":CCM_InstalledComponent.Name='SMSClient'");
obj.Path = path;
obj.Get();
// Display a single property.
Console.WriteLine(obj["DisplayName"].ToString());
// Display all properties.
foreach (PropertyData property in obj.Properties)
{
Console.WriteLine(property.Name + " " + property.Value);
}
}
catch (ManagementException e)
{
Console.WriteLine("Failed to get component: " + e.Message);
throw;
}
}
This example method has the following parameters:
Parameter | Type | Description |
---|---|---|
scope |
- ManagementScope |
The client management scope. The namespace should be root\ccm. |
Compiling the Code
Namespaces
System
System.Management
Assembly
System.Management
Robust Programming
The exception that can be raised is System.Management.ManagementException.
See Also
About Configuration Manager WMI Programming
How to Call a WMI Class Method by Using System.Management
How to Connect to the Configuration Manager Client WMI Namespace by Using System.Management
How to Perform an Asynchronous Query by Using System.Management
How to Perform a Synchronous Query by Using System.Management