【问题标题】:Need help capturing error messages in PowerShell需要帮助在 PowerShell 中捕获错误消息
【发布时间】:2020-12-17 02:33:55
【问题描述】:

我正在尝试从网络上的多个服务器捕获本地管理员组中的用户。我最初是从我发现的一个执行 WMI 调用的脚本开始的,但所有这些都在网络上被阻止了。

我终于得到了以下工作,但是当我收到错误时,它给出了 Powershell 错误,我想通过简单的回答来简化它,例如“访问被拒绝”或“WinRM 无法处理请求。”等...我必须将报告交给非技术用户。

我想将 以下错误消息: 之后的句子捕获到句点。

如果有更好的方法或输出,我愿意接受建议。

感谢您的帮助

$computerlist = get-content "C:\ServerList.txt"


ForEach ($computer in $computerList)
    {
    &{$computer 
    Invoke-Command -ComputerName $computer -ScriptBlock {Get-LocalGroupMember -Group administrators} |
        Select-Object Name,PrincipalSource|Format-table -AutoSize 
    Write-Output " "} 2>&1 >>.\output.txt
    
    }

[XXXXXXX] 连接到远程服务器 XXXXXXX 失败并显示以下错误消息:WinRM 无法处理请求。 使用时出现以下错误 Kerberos 身份验证:找不到计算机 XXXXXXX。验证计算机是否存在于网络上,并且提供的名称拼写正确。更多 有关信息,请参阅 about_Remote_Troubleshooting 帮助主题。 + CategoryInfo : OpenError: (EPICSOAPT:String) [], PSRemotingTransportException + FullyQualifiedErrorId : NetworkPathNotFound,PSSessionStateBroken

【问题讨论】:

    标签: powershell error-handling


    【解决方案1】:

    尝试以下方法:

    Invoke-Command -ComputerName $computerList -ScriptBlock { 
      Get-LocalGroupMember -Group administrators | Select-Object Name, PrincipalSource 
    } 2>&1 | ForEach-Object {
      if ($_ -is [System.Management.Automation.ErrorRecord]) { # an error
        # Extract the substring of interest from the error message and prepend 
        # the computer name.
        # Write the result as a *warning*
        # Note: Warnings are sent to a different output stream,
        #       which in this case means that all warnings print *before* 
        #       Format-Table's output.
        Write-Warning ("$($_.TargetObject ): " + (($_ -split ' : ')[1] -split '\.')[0] + '.')
      }
      else { # an output object, pass it through
        $_ 
      }
    } | Format-Table -GroupBy PSComputerName -AutoSize > .\output.txt
    
    • 最好在单个调用中将计算机的列表传递给Invoke-Command,这会使这些计算机并行成为目标>.

      • 注意:目标计算机的输出保证以输入顺序到达,但您可以插入Sort-Object PSComputerName管道段以对结果进行排序按计算机名称的字母顺序。
    • 在您的方法中,2>&1 用于将错误流 (2) 合并到成功 (1) 流中。

    • ForEach-Object 脚本块-is 中,type(-inheritance) / interface test operator 用于检测输入对象中的错误 (System.Management.Automation.ErrorRecord) 实例。

      • 如果找到这样的实例,则使用-split operator 提取感兴趣的错误消息部分,并在前面加上计算机名称。

      • Write-Warning 用于以视觉显眼的方式发出错误消息;请注意,警告会发送到不同的output stream (#3),在这种情况下,这意味着所有警告Format-Table格式化的成功输出之前打印。

      • 如果您想在 之后发出警告,则必须将字符串收集到一个数组(或列表)中,然后在单独的语句中输出。

    • Format-Table-GroupBy参数用于通过.PSComputerName属性分组格式化成功输出对象,该属性由Invoke-Command隐式添加并包含源名称电脑。

      • 请注意,.PSComputerName 属性和另一个添加的属性 .RunspaceId 默认情况下包含在表输出中。您可以通过将-HideComputerName 开关添加到Invoke-Command 调用来避免包含PSComputerName;但是,RunspaceId 属性仍然包括在内。为避免这种情况,请使用Format-Table-Property 参数传递要显示的属性名称的显式列表。 (另一个更方便但更昂贵的选择是使用-HideComputerName,并在Format-Table 调用之前将Select-Object * -Exclude RunspaceId 命令插入管道。

    示例输出(假设成功检索到的对象具有属性onetwothree;请注意首先显示有关连接失败的警告的方式):

    WARNING: server2: WinRM cannot complete the operation.
    
       PSComputerName: server1
    
    one two three PSComputerName RunspaceId
    --- --- ----- -------------- ----------
      1   2     3 server1        29b0bc1e-cf7f-4d9b-b267-86deff2b7ed0
      4   5     6 server1        ab2ed6a9-3fba-4083-b13e-b43617048ecf
    
       PSComputerName: server3
    
    one two three PSComputerName RunspaceId
    --- --- ----- -------------- ----------
      7   8     9 server3        39b0bc1e-cf7f-4d9b-b267-86deff2b7ed1
    

    【讨论】:

      【解决方案2】:

      我建议将try/catch 块与-ErrorAction StopInvoke-Command cmdlet 一起使用。

      ErrorAction Stop 强制将错误作为异常抛出,该异常可以使用 try / catch 块捕获并根据需要处理错误消息。

      try { 
        Invoke-Command -ComputerName test -ScriptBlock { 
          Write-output "hello" 
        } -ErrorAction Stop 
      } 
      catch { 
        Write-Output "Error Occured: $($_)" 
      }
      
      // prints normal output: 
      Error Occured: [test] Connecting to remote server test failed with the following error message : WinRM cannot process the request. The following error occurred while using Kerberos authentication: Cannot find the computer test. Verify that the co
      mputer exists on the network and that the name provided is spelled correctly. For more information, see the about_Remote_Troubleshooting Help topic.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-17
        • 2013-03-07
        • 1970-01-01
        • 2022-08-02
        相关资源
        最近更新 更多