【问题标题】:Powershell Invoke-Command -ErrorVariable output is incompletePowershell Invoke-Command -ErrorVariable 输出不完整
【发布时间】:2019-01-11 19:47:08
【问题描述】:

所以我一直试图从 Invoke-Command 错误中获取完整的错误消息和堆栈跟踪,但没有任何运气。

我运行了这段代码:

Invoke-command -COMPUTER "TESTCOMPUTER" -ScriptBlock {

    klist purge -li 0x3e7

    Return Get-Service

} -ErrorVariable errmsg

Write-Host "`r`nError: $errmsg"

这是我在控制台中收到的输出:

[TESTCOMPUTER] 连接到远程服务器 TESTCOMPUTER 失败并显示以下错误消息:WinRM 无法完成操作。 验证指定的计算机名称是否有效,该计算机是否可以通过网络访问,并且该计算机的防火墙例外 WinRM 服务已启用并允许从此计算机进行访问。默认情况下,公共配置文件的 WinRM 防火墙例外限制访问 同一本地子网中的远程计算机。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。 + CategoryInfo : OpenError: (TESTCOMPUTER) [], PSRemotingTransportException + FullyQualifiedErrorId : WinRMOperationTimeout,PSSessionStateBroken

错误:[TESTCOMPUTER] 连接到远程服务器 TESTCOMPUTER 失败,并显示以下错误消息:WinRM 无法完成操作。 验证指定的计算机名称是否有效,该计算机可通过网络访问,并且 WinR 的防火墙例外 M 服务已启用并允许从此计算机进行访问。默认情况下,公共配置文件的 WinRM 防火墙例外限制对远程的访问 e 同一本地子网中的计算机。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。

如您所见,第二部分是缺少堆栈跟踪的错误副本。我怎样才能把整个错误变成一个字符串?

【问题讨论】:

    标签: powershell error-handling invoke-command


    【解决方案1】:

    ErrorVariable 是 System.Management.Automation.ErrorRecord
    如果你对这个变量执行Get-Member,你可以看到它有这些方法和属性:

    Name                  MemberType     Definition
    ----                  ----------     ----------
    Equals                Method         bool Equals(System.Object obj)
    GetHashCode           Method         int GetHashCode()
    GetObjectData         Method         void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void ISeri...
    GetType               Method         type GetType()
    ToString              Method         string ToString()
    writeErrorStream      NoteProperty   bool writeErrorStream=True
    CategoryInfo          Property       System.Management.Automation.ErrorCategoryInfo CategoryInfo {get;}
    ErrorDetails          Property       System.Management.Automation.ErrorDetails ErrorDetails {get;set;}
    Exception             Property       System.Exception Exception {get;}
    FullyQualifiedErrorId Property       string FullyQualifiedErrorId {get;}
    InvocationInfo        Property       System.Management.Automation.InvocationInfo InvocationInfo {get;}
    PipelineIterationInfo Property       System.Collections.ObjectModel.ReadOnlyCollection[int] PipelineIterationInfo {get;}
    ScriptStackTrace      Property       string ScriptStackTrace {get;}
    TargetObject          Property       System.Object TargetObject {get;}
    PSMessageDetails      ScriptProperty System.Object PSMessageDetails {get=& { Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails };}
    

    如果您省略了Write-Host 并以

    结束您的代码
    $errmsg
    

    它将返回整个错误(错误颜色,即红色)

    您可以通过组合$errmsg 对象的不同属性来构造完整的错误消息,如下所示:

    $err = "`r`nError: {0}`r`n    + CategoryInfo          : {1}`r`n    + FullyQualifiedErrorId : {2}" -f $errmsg.ErrorDetails, $errmsg.CategoryInfo, $errmsg.FullyQualifiedErrorId
    Write-Host $err
    

    或使用 Here-String 以获得更好的可读性:

    $err = @"
    Error: $($errmsg.ErrorDetails)
        + CategoryInfo          : $($errmsg.CategoryInfo)
        + FullyQualifiedErrorId : $($errmsg.FullyQualifiedErrorId)
    "@ 
    Write-Host $err
    

    甚至可以添加更多感兴趣的属性,但这取决于您。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-02
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多