Share via


如何使用 Managed 程式碼新增Configuration Manager內容限定詞

在Configuration Manager中,若要使用受控 SMS 提供者新增內容限定詞,請使用CoNtext 屬性,此屬性是 Dictionary 保存內容限定詞的物件。

一般而言,您會將應用程式名稱新增至 ApplicationName 內容限定詞,以及電腦名稱稱 (MachineName) 和 LocaleID (LocaleID) 。

新增Configuration Manager限定詞

  1. 設定與 SMS 提供者的連線。 如需詳細資訊,請參閱如何使用 Managed 程式碼在 Configuration Manager 中連線到 SMS 提供者

  2. 從步驟 1 取得的 WqlConnectionManager物件取得SmsNamedValuesDictionary物件。

  3. 視需要新增內容限定詞。

範例

下列 C# 範例會先將一些內容限定詞新增至 WQLConnectionManager 物件 CoNtext 字典屬性。 然後,它會在字典物件中顯示內容限定詞的清單。

注意事項

WqlConnectionManager 衍生自 ConnectionManagerBase

在此範例中 LocaleID ,內容限定詞會硬式編碼為英文 (美國) 。 如果您需要非美國地區設定安裝時,您可以從SMS_Identification Server WMI ClassLocaleID 屬性取得它。

如需呼叫範例程式碼的相關資訊,請參閱呼叫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);  
    }  
}  

範例方法具有下列參數:

參數 Type 描述
connection - WqlConnectionManager SMS 提供者的有效連線。

正在編譯程式碼

命名空間

系統

System.Collections.Generic

System.ComponentModel

Microsoft。ConfigurationManagement.ManagementProvider

Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine

組件

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

健全的程式設計

可引發的Configuration Manager例外狀況為SmsConnectionExceptionSmsQueryException。 這些可以與 SmsException一起攔截。

另請參閱

Configuration Manager內容限定詞
如何使用 Managed 程式碼連線到Configuration Manager提供者