1

I have a script that retrieves certain event log entries from a list of computers. I'm using a foreach loop over the list of computers with Get-EventLog -Computername $c... to get the events. I use Test-Connection MyComputer -Quiet to make sure each computer is up. I can't figure out how to test programmatically if Remoting is enabled. I've tried

if (!(Test-WSMan MyComputer))

that still throws an error trying to connect to Remoting to test if Remoting is enabled. Seems like a Catch-22. You can only test if Remoting is enabled by connecting to Remoting and if it's not enabled you get an error.

I also tried wrapping it in a Try...Catch but that didn't catch the error.

Is there a way to check from within a script whether or not a computer has Remoting available?

Note: I'm running PowerShell 4.0 everywhere PowerShell is installed.

2
  • 3
    -ErrorAction SilentlyContinue Commented Nov 17, 2015 at 12:02
  • You should add that as an Answer so I can mark it. This is much simpler. Thanks!! Commented Nov 17, 2015 at 15:59

2 Answers 2

2

If all you want is to suppress error message, then -ErrorAction SilentlyContinue should en enough for you:

if (!(Test-WSMan MyComputer -ErrorAction SilentlyContinue)) 

Or you can use -ErrorAction Ignore, if you does not want to add error to $Error automatic variable.

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

Comments

1

You Can use this Function:

Function Test-PSRemoting { Param($Computer) $ErrorActionPreference = "Stop" Try { if ((Invoke-Command -ComputerName $Computer -ScriptBlock {1}) -eq "1") { return $true } else { return $false } } Catch { return $false } } 

Use it like this:

Test-PSRemoting ComputerName True 

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.