共用方式為


How to Perform a Synchronous Query 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 synchronously query the Microsoft System Center Configuration Manager 2007 client Windows Management Instrumentation (WMI), you use a ManagementObjectSearcher object.

To read a lazy property from a Configuration Manager 2007 object that is returned in a query, you get the object instance, which in turn retrieves any lazy object properties from the SMS Provider.

To perform a synchronous query

  1. Set up a connection to the Configuration Manager 2007 client WMI namespace. For more information, see How to Connect to the Configuration Manager Client WMI Namespace by Using System.Management.

  2. Create a ManagementObjectSearcher collection, and specify a WQL query.

  3. Iterate through the ManagementObjectSearcher collection to view the ManagementObject for each WMI object that is returned by the query.

Example

The following C# code example queries for the single SMS_Client object that is on a Configuration Manager 2007 client.

For information about calling the sample code, see How to Call a WMI Class Method by Using System.Management.

public void QueryObjects(ManagementScope scope)
{
    try
    {
        ManagementObjectSearcher s = new ManagementObjectSearcher
            ((scope), new WqlObjectQuery("SELECT * FROM sms_client"));

        foreach (ManagementObject o in s.Get())
        {
            // There is only one instance of SMS_Client, so this should enumerate only once.
            Console.WriteLine("Client version: " + o["ClientVersion"].ToString());
        }
    }
    catch (System.Management.ManagementException e)
    {
        Console.WriteLine("Failed to make query: ", e.Message);
        throw;
    }
}

This example method has the following parameters:

Parameter Type Description

connection

WqlConnectionManager

A valid connection to the SMS Provider.

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