Share via


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

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

如果在SMS_Exception中攔截到的例外狀況是 SmsQueryException,您可以使用它來取得基礎 __ExtendedExceptionSMS_ExtendedException 。 因為受控 SMS 提供者程式庫不會包裝這些例外狀況,所以您必須使用 System.Management 命名空間 ManagementException 物件來存取它們。

注意事項

為了清楚起見,本檔中的大部分範例只會重新擲回例外狀況。 如果您想要更多資訊的例外狀況資訊,您可以將它們取代為下列範例。

若要處理同步查詢錯誤

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

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

範例

下列 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非同步錯誤