1

This cmdlet from client allows me to check to see if remoting with Powershell is enabled on server. In example below, results say it is:

PS C:\WINDOWS\system32> Test-WsMan xxx.104.50.xxx wsmid : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd ProductVendor : Microsoft Corporation ProductVersion : OS: 0.0.0 SP: 0.0 Stack: 3.0 

However, I don't know how to get a true/false back from running Test-WsMan. In my automation script I want to check if remoting is enabled, and if not, stop the script and exit.

I hope my question makes sense and I hope someone is a lot smarter than me.

3
  • In PowerShell, a return value of 0 or $null or $false is a false return; a return that is not false is true. Commented Mar 29, 2017 at 18:32
  • @Jeff good info, but there was no return value. Are you saying that if I said : if(Test-WsMan xxx.104.50.xxx = 0) {[put exit code here]}? It doesn't work for me, returning "Test-WSMan : A positional parameter cannot be found that accepts argument '='." Commented Mar 29, 2017 at 21:03
  • No, a null return is equal to false, and if you want to do an explicit comparison in PowerShell, you use -eq, not =. However, you can do the implicit test, just like in C-family languages: if (Test-WSMan <params...>) {<actions...>} if you want to do something if the Test-WSMan succeeds, or if (!(Test-WSMan <params...>)) {<actions...>} if it fails. Commented Mar 30, 2017 at 11:36

1 Answer 1

3

From MSDN

If the tested computer is running the service, the cmdlet displays the WS-Management identity schema, the protocol version, the product vendor, and the product version of the tested service.

MSDN does not explicitly state what it does when it is not running the service but I am sure you could just treat this as a truthy falsy. If nothing is returned it is not working. PowerShell is very good at evaluating expressions as booleans.

if(test-wsman $computer -ErrorAction SilentlyContinue){ # Remoting is enabled } else { # Something is wrong. } 

-ErrorAction SilentlyContinue would cover any reason for failure like if the name was wrong or otherwise not contactable. It would be up to you to determine what to do if remoting is "not working".

Sign up to request clarification or add additional context in comments.

5 Comments

Get-Process : A positional parameter cannot be found that accepts argument 'if'
@JustJohn If that is the error you are getting it is how you are implementing my code since I do not use Get-Process in my small snippet. Is there a newline or semi colon after a get-process call?
Not sure where that came from but it was there. It seems to work, now that I run it again.
@JustJohn Something must still be wrong with the implementation since one of those conditions would have to fire. You replaced $computer with the IP you were testing? Can you temporarily edit what you are trying into your question so I can see it.
It works so not sure what happened. I put "throw 'remoting is not enabled' " in your example and it works like expected and also gives notice to whoever was running script. I will accept your answer and thank you very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.