techthoughts2
12/28/2015 - 3:54 PM

Test WinRM Connection

Verifies if remote WinRM Powershell commands can be run against another device by verifying WinRM connectivity to the specified computer name over both HTTP and HTTPS. Returns a customer PSObject containing boolean value for both HTTP and HTTPS results.

<#
.Synopsis
   Tests WinRm connectivity to specified computername via HTTP and HTTPS
.DESCRIPTION
   Evaluates both HTTP and HTTPS connectivity over WinRM to specified computername. Returns PSObject with Boolean value for HTTP and HTTPS based on results.
.PARAMETER ComputerName
   Hostname of device you wish to test WinRM connection to
.EXAMPLE
   Test-WinRM -ComputerName HYP1
   
   Tests HTTP and HTTPS WinRM connectivity to HYP1 and returns two boolean values in PSObject
.EXAMPLE
   Test-WinRM -ComputerName HYP1 -Verbose
   
   Tests HTTP and HTTPS WinRM connectivity to HYP1 and returns two boolean values in PSObject. Verbose output is displayed.
.OUTPUTS
   PSObject containing boolean values for HTTP and HTTPS WinRM results
.NOTES
   Author: Jake Morrison
   http://techthoughts.info

   Only hostnames are supported by this simple WinRM check. IPs are not supported.
   The default timeout is static set to 8 seconds and can be adjusted by changing the OperTimeout value.
#>
function Test-WinRM {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true,
            Position = 0)]
        [string]$ComputerName
    )
    [bool]$evalWinRMHTTP = $true #assume the best
    [bool]$evalWinRMHTTPS = $true #assume the best
    $Object = New-Object PSObject  

    $so = New-PSSessionOption -MaxConnectionRetryCount 2 -OpenTimeout 8000

    Write-Verbose -Message "Evaluating WinRM over HTTP..."
    $eval = $null
    $eval = New-PSSession -ComputerName $ComputerName -SessionOption $so -ErrorAction SilentlyContinue
    if (!($eval)) {
        $evalWinRMHTTP = $false
        Write-Verbose -Message "HTTP: $evalWinRMHTTP"
    }
    Write-Verbose -Message "Evaluating WinRM over HTTPS..."
    $eval = $null
    $eval = New-PSSession -ComputerName $ComputerName -SessionOption $so -UseSSL -ErrorAction SilentlyContinue
    if (!($eval)) {
        $evalWinRMHTTPS = $false
        Write-Verbose -Message "HTTPS: $evalWinRMHTTPS"
    }
    $Object | add-member Noteproperty HTTP $evalWinRMHTTP
    $Object | add-member Noteproperty HTTPS $evalWinRMHTTPS
    return $Object
}