共用方式為


如何使用 Managed 程式代碼呼叫 Configuration Manager 物件類別方法

若要呼叫 SMS 提供者類別方法,請在 Configuration Manager 中使用 ExecuteMethod 方法。 您會使用方法的參數填入 Dictionary 物件,而傳回值是包含方法呼叫結果的 IResultObject 物件。

注意事項

若要在對象實例上呼叫方法,請在 IResultObject 物件實例上使用 ExecuteMethod 方法。

呼叫 Configuration Manager 物件類別方法

  1. 設定與SMS提供者的連線。 如需詳細資訊,請 參閱SMS提供者基本概念

  2. 將輸入參數建立為 Dictionary 物件。

  3. 使用 WqlConnectionManager 物件實例,呼叫 ExecuteMethod 並指定類別名稱和輸入參數。

  4. 從傳回之 IResultObject 物件中的 ReturnValue 屬性擷取方法傳回值。

範例

下列範例會呼叫 SMS_CollectionRuleQuery 類別 ValidateQuery 類別方法來驗證集合規則查詢。

如需呼叫範例程式代碼的相關信息,請參閱呼叫 Configuration Manager 代碼段

public void ValidateQueryRule(WqlConnectionManager connection, string wqlQuery)
{
    try
    {
        Dictionary<string,object> validateQueryParameters = new Dictionary<string,object>();

        // Add the sql query as the WQLQuery parameter.
        validateQueryParameters.Add("WQLQuery",wqlQuery);

        // Call the method
        IResultObject result=connection.ExecuteMethod("SMS_CollectionRuleQuery", "ValidateQuery", validateQueryParameters);

        if (result["ReturnValue"].BooleanValue == true)
        {
            Console.WriteLine (wqlQuery + " is a valid query");
        }
        else
        {
            Console.WriteLine (wqlQuery + " is not a valid query");
        }
     }
     catch (SmsException ex)
     {
           Console.WriteLine("Failed to validate query rule: ",ex.Message);
           throw;
     }
}

此範例方法具有下列參數:

參數 Type 描述
connection - 受控: WqlConnectionManager SMS 提供者的有效連線。
wqlQuery - Managed: IResultObject WQL 查詢字串。 在此範例中, SELECT * FROM SMS_R_System 是有效的查詢。

正在編譯程式碼

命名空間

系統

System.Collections.Generic

System.ComponentModel

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

組件

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

健全的程式設計

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

另請參閱

物件概觀如何使用 Managed 程式代碼連接到 Configuration Manager 提供者如何使用 Managed 程式代碼建立 Configuration Manager 物件如何使用 Managed 程式代碼修改 Configuration Manager 物件如何執行異步作Configuration Manager 使用 Managed 程式代碼查詢如何使用 Managed 程式代碼執行同步 Configuration Manager 查詢如何使用 Managed 程式代碼讀取 Configuration Manager 物件