如何使用 Managed 程式碼處理 Configuration Manager 同步錯誤

若要處理同步查詢中引發的 Configuration Manager 錯誤,請攔截 SmsQueryException 例外狀況。 由於此例外狀況也會由 SMS_Exception] 攔截,因此您可以在相同的 catch 區塊中攔截它和 SmsConnectionException 例外狀況。

如果SMS_Exception中攔截的例外狀況是 SmsQueryException,您可以使用它來取得基礎 __ExtendedException 或 SMS_ExtendedException。 由於託管 SMS 提供者程式庫並未包裝這些例外狀況,因此您必須使用 System.Management 命名空間 ManagementException 物件來存取它們。

注意事項

為清楚起見,本文件中的大部分範例都只是重新擲回例外狀況。 如果您想要提供更多資訊的例外狀況資訊,可以用下列範例取代它們。

處理同步查詢錯誤

  1. 撰寫程式碼以存取簡訊提供者。

  2. 使用下列範例程式碼來攔截 SmsQueryException 和 SmsConnectionException 例外狀況。

範例

下列 C# 範例函式會嘗試開啟不存在的 SMS_Package 套件。 在異常處理常式中,程式碼會判斷所引發的錯誤類型,並顯示其資訊。

如需呼叫範例程式碼的詳細資訊,請參閱 呼叫 Configuration Manager 程式碼片段。

public void ExerciseException(WqlConnectionManager connection)
{
    try
    {

        IResultObject package = connection.GetInstance(@"SMS_Package.PackageID='UNKNOWN'");
        Console.WriteLine("Package Name: " + package["Name"].StringValue);
        Console.WriteLine("Package Description: " + package["Description"].StringValue);

    }
    catch (SmsException e)
    {
        if (e is SmsQueryException)
        {
            SmsQueryException queryException = (SmsQueryException)e;
            Console.WriteLine(queryException.Message);

            // Get either the __ExtendedStatus or SMS_ExtendedStatus object and display various properties.
            ManagementException mgmtExcept = queryException.InnerException as ManagementException;

            if (mgmtExcept != null)
            {
                if (string.Equals(mgmtExcept.ErrorInformation.ClassPath.ToString(), "SMS_ExtendedStatus", StringComparison.OrdinalIgnoreCase) == true)
                {
                    Console.WriteLine("Configuration Manager provider exception");
                }

                else if (string.Equals(mgmtExcept.ErrorInformation.ClassPath.ToString(), "__ExtendedStatus", StringComparison.OrdinalIgnoreCase) == true)
                {
                    Console.WriteLine("WMI exception");
                }
                Console.WriteLine(mgmtExcept.ErrorCode.ToString());
                Console.WriteLine(mgmtExcept.ErrorInformation["ParameterInfo"].ToString());
                Console.WriteLine(mgmtExcept.ErrorInformation["Operation"].ToString());
                Console.WriteLine(mgmtExcept.ErrorInformation["ProviderName"].ToString());
            }

        }
        if (e is SmsConnectionException)
        {
            Console.WriteLine("There was a connection error :" + ((SmsConnectionException)e).Message);
            Console.WriteLine(((SmsConnectionException)e).ErrorCode);
        }
    }
}

範例方法具有下列參數:

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

正在編譯程式碼

此 C# 範例需要:

命名空間

系統

System.Collections.Generic

System.Text

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

System.Management

System.ComponentModel

組件

Microsoft.ConfigurationManagement.ManagementProvider

adminui.wqlqueryengine

System.Management

健全的程式設計

如需錯誤處理的詳細資訊,請參閱 關於 Configuration Manager 錯誤。

另請參閱

關於錯誤如何使用 Managed 程式碼處理 Configuration Manager 非同步錯誤