我正在尝试测试winrm是否在系统列表上工作;但是,当我尝试连接到系统时,似乎无法捕捉/消除出现的错误。它似乎可以在一个系统上运行:

PS C:\Users\Egr> winrm id -r:system1
IdentifyResponse
    ProtocolVersion = http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
    ProductVendor = Microsoft Corporation
    ProductVersion = OS: x.x.xxxx SP: x.x Stack: x.x

但不适用于另一个:
PS C:\Users\Egr> winrm id -r:system2
WSManFault
    Message = WinRM cannot process the request. The following error occured while using Kerberos authentication: The net
work path was not found.
 Possible causes are:
  -The user name or password specified are invalid.
  -Kerberos is used when no authentication method and no user name are specified.
  -Kerberos accepts domain user names, but not local user names.
  -The Service Principal Name (SPN) for the remote computer name and port does not exist.
  -The client and remote computers are in different domains and there is no trust between the two domains.
 After checking for the above issues, try the following:
  -Check the Event Viewer for events related to authentication.
  -Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or use
 HTTPS transport.
 Note that computers in the TrustedHosts list might not be authenticated.
   -For more information about WinRM configuration, run the following command: winrm help config.

Error number:  -2147024843 0x80070035
The network path was not found.

我已经尝试将其围绕在try / catch块中,但是似乎并没有使其静音。我试图对这些系统进行检查,以确定哪些系统已正确配置了WinRM并且可以正常运行;但是如果脚本继续输出此文本,它将无法很好地工作。有什么方法可以禁止显示此文本,或者有更好的方法来测试WinRM连接?

最佳答案

您可以将redirect the error stream转换为$null并评估$LastExitCode来检测错误:

$rhost = 'system2'

winrm id -r:$rhost 2>$null
if ($LastExitCode -eq 0) {
  Write-Host "$rhost OK" -ForegroundColor green
} else {
  Write-Host "$rhost unavailable" -ForegroundColor red
}

08-26 14:16