2

There are two ways suggested in the docs both of which are completely useless to determine if the remote computer is available for powershell remoting.

  1. Test-Connection is useless because it sends only ping, however, the destination may be running Intel AMT and thus respond to ping or may not be running winRM service, so this provides no useful information.

  2. Test-WSMan should test the availability of Windows RM service, but it only works, if the WinRM works, otherwise (the computer is off or WinRM is not running) it gives an error:

    Test-WSMan : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". At line:2 char:1

    • Test-WSMan -ComputerName destinationcomputer
    • + CategoryInfo : InvalidOperation: (destinationcomputer:String) [Test-WSMan], InvalidOperationException + FullyQualifiedErrorId : WsManError,Microsoft.WSMan.Management.TestWSManCommand 

So I tried enveloping Test-WSMan in try-catch commands, but it still gives the error out, it is not really caught:

 Try { Test-WSMan -ComputerName destinationcomputer } catch { write-output "not working"} 

Any idea how to do this? (i would be happy with Test-WSMan if I could get rid of the error and enforce it to return true or false)

2
  • 5
    You need to add -ErrorAction Stop to your command so it get caught in the Try / Catch. Otherwise it will go right through it. Test-WSMan -ComputerName destinationcomputer -ErrorAction Stop Commented Feb 1, 2020 at 14:36
  • 1
    @leosenko Your question makes me think you need a way to "test connectivity". However, your opening paragraph states "... determine if the remote computer is available for powershell remoting". Are you testing for connectivty or are you testing to see if the computer is available for PowerShell remoting (not exactly the same thing, right?)? Also, is the client computer and remote computer members of a domain or workgroup, or ... ? Commented Feb 1, 2020 at 15:52

1 Answer 1

2

Test $? after running test-wsman.

Test-WSMan destinationcomputer if (! $?) { 'not working' } 

You could also do it this way since a failure returns no standard output:

if (!(Test-WSMan destinationcomputer)){ 'not working' } 

See also Check if a command has run successfully

Also in powershell 7:

Test-WSMan destinationcomputer || 'not working' 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.