Testing for Remoting Enabled in a script?

Tagged: 

This topic contains 2 replies, has 1 voice, and was last updated by Profile photo of Richard Schaefer Richard Schaefer 1 year, 8 months ago.

  • Author
    Posts
  • #32018
    Profile photo of Richard Schaefer
    Richard Schaefer
    Participant

    Is there any way to test if Remoting is enabled within a script? I'm looping over a collection of computers to get event log entries from each computer. I'm testing if the computer is available using
    Test-Connection $c -quiet
    But I can't seem to find a similar method that will tell me if Remoting is running before I try to use Get-EventLog $c. I've tried using Test-WSMan but even wrapping that in a Try/Catch block doesn't prevent the command from throwing an error.

  • #32021
    Profile photo of Richard Schaefer
    Richard Schaefer
    Participant

    Someone in StackOverflow posted 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:
    Test-PSRemoting MyComputer
    True

  • #32063
    Profile photo of Richard Schaefer
    Richard Schaefer
    Participant

    Someone also suggested adding "-ErrorAction SilentlyContinue" to the Test-WSMan command. Works great. Much more elegant than including another function in my module.

You must be logged in to reply to this topic.