powershellpowershell-2.0winrmwsman

Test WinRM/WSMan connectivity?


I'm trying to test if winrm works on a list of systems; however, I can't seem to catch/silence the error that appears when I attempt to connect to a system. It appears to work on one system:

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

But doesn't work on another:

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.

I've tried surrounding it in a try/catch block, but it doesn't seem to silence it. I am attempting to run a check against these systems to determine which ones have WinRM configured correctly and working; but if the script keeps outputting this text, it won't work very neatly. Is there any way to suppress this text, or is there a better way to test WinRM connectivity?


Solution

  • You could redirect the error stream to $null and evaluate $LastExitCode to detect an error:

    $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
    }