Search code examples
powershellremotingpowershell-remoting

Testing for Remoting in Powershell?


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.


Solution

  • 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.