如何使用托管代码处理 Configuration Manager 同步错误

若要处理同步查询中引发的 Configuration Manager 错误,请捕获 SmsQueryException 异常。 由于此异常也被 SMS_Exception] 捕获,因此您可以在同一捕获块中捕获它和 SmsConnectionException 异常。

如果SMS_Exception中捕获的异常是 SmsQueryException,则可以使用它来访问基础 __ExtendedException OR SMS_ExtendedException. 由于托管 SMS 提供程序库不会包装这些异常,因此您需要使用 System.Management 命名空间 ManagementException 对象来访问它们。

注意

为清楚起见,本文档中的大多数示例只是重新引发异常。 如果需要更多信息异常信息,可以用以下示例替换它们。

处理同步查询错误

  1. 编写代码以访问 SMS 提供程序。

  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);
        }
    }
}

示例方法具有以下参数:

参数 类型 说明
connection - WqlConnectionManager 与提供程序的有效连接。

编译代码

此 C# 示例需要:

命名空间

System

System.Collections.Generic

System.Text

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

System.Management

System.ComponentModel

Assembly

Microsoft.ConfigurationManagement.ManagementProvider

adminui.wqlqueryengine

System.Management

可靠编程

有关错误处理的详细信息,请参阅关于 Configuration Manager 错误。

另请参阅

关于错误如何使用托管代码处理 Configuration Manager 异步错误