次の方法で共有


How to Perform an Asynchronous Configuration Manager Query by Using Managed Code

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

In Microsoft System Center Configuration Manager 2007, to perform an asynchronous query by using the managed SMS Provider, you use the ProcessQuery method.

The first parameter of the ProcessQuery method is an instance of the SmsBackgroundWorker class that provides two event handlers:

  • QueryProcessObjectReady. This event handler is called for each object returned by the query. The event handler provides an IResultObject object that represents the object.

  • QueryProcessCompleted. This event handler is called when the query is completed. It also provides information about any errors that occur. For more information, see For information about error handling, see How to Handle Configuration Manager Asynchronous Errors by Using Managed Code.

The second parameter to of the ProcessQuery method is the WQL statement for the query.

To perform an asynchronous query

  1. Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.

  2. Create the SmsBackgroundWorker object and populate the QueryProcessorObjectReady and QueryProcessorCompleted properties with the callback method names.

  3. From the WqlConnectionManager object you obtain in step one, call the QueryProcessor object ProcessQuery method to start the asynchronous query.

Example

The following example queries for all available SMS_Collection objects, and in the event handler, the example writes several of the collection properties to the Configuration Manager 2007 console.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

public void QueryCollections(WqlConnectionManager connection)
{
    try
    {
        // Set up the query.
        SmsBackgroundWorker bw1 = new SmsBackgroundWorker();
        bw1.QueryProcessorObjectReady += new EventHandler<QueryProcessorObjectEventArgs>(bw1_QueryProcessorObjectReady);
        bw1.QueryProcessorCompleted += new EventHandler<RunWorkerCompletedEventArgs>(bw1_QueryProcessorCompleted);

        // Query for all collections.
        connection.QueryProcessor.ProcessQuery(bw1, "select * from SMS_Collection");
        
        // Pause while query runs.
        Console.ReadLine();
    }
    catch (SmsException ex)
    {
        Console.WriteLine("Failed to start asynchronous query: ", ex.Message);
    }
}

void bw1_QueryProcessorObjectReady(object sender, QueryProcessorObjectEventArgs e)
{
    try
    {
        // Get the collection.
        IResultObject collection = (IResultObject)e.ResultObject;

        //Display properties.
        Console.WriteLine(collection["CollectionID"].StringValue);
        Console.WriteLine(collection["Name"].StringValue);
        Console.WriteLine();
        collection.Dispose();
    }
    catch (SmsQueryException eX)
    {
        Console.WriteLine("Query Error: " + eX.Message);
    }
}

void bw1_QueryProcessorCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    Console.WriteLine("Done...");
}

This example method has the following parameters:

Parameter Type Description

connection

Managed: WqlConnectionManager

A valid connection to the SMS Provider.

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

Concepts

About Configuration Manager Objects
Configuration Manager Lazy Properties
How to Call a Configuration Manager Object Class Method by Using Managed Code
How to Connect to an SMS Provider in Configuration Manager by 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 a Synchronous Configuration Manager Query by Using Managed Code
How to Read a Configuration Manager Object by Using Managed Code
How to Read Lazy Properties by Using Managed Code
How to Use Configuration Manager Objects with Managed Code
How to Perform a Synchronous Configuration Manager Query by Using Managed Code
Configuration Manager Extended WMI Query Language
Configuration Manager Result Sets
Configuration Manager Special Queries
Configuration Manager Queries