测试WinRM / WSMan连接?

时间:2013-08-12 18:53:55

标签: powershell powershell-v2.0 winrm wsman

我正在尝试测试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连接?

1 个答案:

答案 0 :(得分:1)

您可以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
}