マネージ コードを使用して非同期 Configuration Manager クエリを実行する方法

Configuration Manager では、マネージド SMS プロバイダーを使用して非同期クエリを実行するには、ProcessQuery メソッドを使用します。

ProcessQuery メソッドの最初のパラメーターは、次の 2 つのイベント ハンドラーを提供する SmsBackgroundWorker クラスのインスタンスです。

非同期クエリを実行するには

  1. SMS プロバイダーへの接続をセットアップします。 詳細については、「 SMS プロバイダーの基礎」を参照してください。

  2. SmsBackgroundWorker オブジェクトを作成し、QueryProcessorObjectReady プロパティと QueryProcessorCompleted プロパティにコールバック メソッド名を設定します。

  3. 手順 1 で取得した WqlConnectionManager オブジェクトから、 QueryProcessor オブジェクト ProcessQuery メソッドを呼び出して非同期クエリを開始します。

次の例では、使用可能なすべての SMS_Collection オブジェクトに対してクエリを実行し、イベント ハンドラーで、コレクション プロパティのいくつかを Configuration Manager コンソールに書き込みます。

サンプル コードの呼び出しの詳細については、「Configuration Manager コード スニペットの呼び出し」を参照してください。

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...");
}

この例のメソッドには、次のパラメータがあります:

パラメーター 説明
connection 管理: WqlConnectionManager SMS プロバイダーへの有効な接続。

コードのコンパイル

名前空間

System

System.Collections.Generic

System.ComponentModel

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

Microsoft.ConfigurationManagement.ManagementProvider

adminui.wqlqueryengine

堅牢なプログラミング

発生する可能性がある Configuration Manager の例外は、SmsConnectionExceptionSmsQueryException です。 これらは SmsException と共にキャッチできます。

関連項目

オブジェクトの概要:Configuration Manager 遅延プロパティマネージド コードを使用して Configuration Manager オブジェクト クラス メソッドを呼び出す方法マネージド コードを使用して Configuration Manager プロバイダーに接続する方法マネージ コードを使用して Configuration Manager オブジェクトを作成する方法 マネージ コードを使用して Configuration Manager オブジェクトを変更する方法マネージド コードを使用して同期 Configuration Manager クエリを実行する方法Configuration Manager の読み取り方法マネージド コードを使用してオブジェクト化する マネージ コードを使用して遅延プロパティを読み取る方法マネージド コードを使用して Configuration Manager クエリを同期実行する方法:Configuration Manager 拡張 WMI クエリ言語:Configuration Manager 結果セットConfiguration Manager の特別なクエリクエリについて