如何使用托管代码添加Configuration Manager上下文限定符

在 Configuration Manager中,若要使用托管 SMS 提供程序添加上下文限定符,请使用 Context 属性,该属性是一个Dictionary包含上下文限定符的对象。

通常,会将应用程序名称添加到 ApplicationName 上下文限定符,以及计算机名称 (MachineName) 和 LocaleID (LocaleID) 。

添加Configuration Manager上下文限定符

  1. 设置与 SMS 提供程序的连接。 有关详细信息,请参阅如何使用托管代码在 Configuration Manager 中连接到 SMS 提供程序

  2. 从步骤 1 获取的 WqlConnectionManager 对象获取 SmsNamedValuesDictionary 对象。

  3. 根据需要添加上下文限定符。

示例

下面的 C# 示例首先将许多上下文限定符添加到 WQLConnectionManager 对象 Context dictionary 属性。 然后,它会在字典对象中显示上下文限定符的列表。

注意

WqlConnectionManager 派生自 ConnectionManagerBase

在此示例中, LocaleID 上下文限定符硬编码为英语 (美国) 。 如果需要非美国区域设置。安装,可以从 SMS_Identification 服务器 WMI 类LocaleID 属性获取它。

有关调用示例代码的信息,请参阅调用Configuration Manager代码片段

public void AddContextQualifiers(WqlConnectionManager connection)  
{  
    try  
    {  
        connection.Context.Add("ApplicationName", "My application name");  
        connection.Context.Add("MachineName","Computername");  
        connection.Context.Add("LocaleID", @"MS\1033");  

        foreach (KeyValuePair<string, object> namedValue in connection.Context)  
        {  
            Console.WriteLine(namedValue.Key);  
            Console.WriteLine(namedValue.Value);  
            Console.WriteLine();  
        }  
    }  
    catch (SmsException e)  
    {  
        Console.WriteLine("Failed to add context qualifier : " + e.Message);  
    }  
}  

示例方法具有以下参数:

参数 类型 说明
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提供程序