如何使用托管代码执行异步Configuration Manager查询

在 Configuration Manager中,若要使用托管 SMS 提供程序执行异步查询,请使用 ProcessQuery 方法。

ProcessQuery 方法的第一个参数是 SmsBackgroundWorker 类的实例,该类提供两个事件处理程序:

执行异步查询

  1. 设置与 SMS 提供程序的连接。 有关详细信息,请参阅 SMS 提供程序基础知识

  2. 创建 SmsBackgroundWorker 对象,并使用回调方法名称填充 QueryProcessorObjectReadyQueryProcessorCompleted 属性。

  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关于查询的特殊查询