如何使用 WMI 处理Configuration Manager同步错误

在 Configuration Manager 中,通过在发生错误时检查SWbemLastError对象来处理同步错误。 当错误对象 Number 属性为非零时发生错误。

注意

在 VBScript 中,应声明要继续运行脚本(如果发生错误)。 否则,脚本将在出现错误情况时结束。 为此,请在脚本中使用 On Error Resume Next 声明。

示例

以下 VBScript 示例显示对象 SWbemLastError 中提供的最新错误信息。 可以使用以下代码来尝试获取无效SMS_Package包来对其进行测试。

有关调用示例代码的信息,请参阅调用Configuration Manager代码片段


Sub ExerciseError(connection)

    On Error Resume next

    Dim packages
    Dim package

    ' Run the query.
    Set package = connection.Get("SMS_Package.PackageID='UNKNOWN'")

    If Err.Number<>0 Then
        Call DisplayLastError
    End If

End Sub

Sub DisplayLastError
    Dim ExtendedStatus

    ' Get the error object.
    Set ExtendedStatus = CreateObject("WbemScripting.SWBEMLastError")

    ' Determine the type of error.
    If ExtendedStatus.Path_.Class = "__ExtendedStatus" Then
        WScript.Echo "WMI Error: "& ExtendedStatus.Description
    ElseIf ExtendedStatus.Path_.Class = "SMS_ExtendedStatus" Then
        WScript.Echo "Provider Error: "& ExtendedStatus.Description
        WScript.Echo "Code: " & ExtendedStatus.ErrorCode
    End If
End Sub

另请参阅

关于错误
WMI SDK