Compartilhar via


How to Read a WMI Object by Using System.Management

Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2

To read a Configuration Manager client Windows Management Instrumentation (WMI) object, in Microsoft System Center Configuration Manager 2007, you use a ManagementObject object to read the WMI object.

To read a WMI object

  1. 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.

  2. Create a ManagementObject object.

  3. Create a ManagementPath object with the ManagementScope path you obtain from step one.

  4. Assign the ManagementPath object to the ManagementObject path property.

  5. Call the ManagementObject object Get method to get the object from the WMI provider.

  6. 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

Concepts

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
How to Read a WMI Object by Using System.Management