PowerShell Tip of the Week: Quick connection test on remote machines

Quick Test-NetConnection

I’m often asked how to quickly test connectivity on remote machines. In this short article from PowerShell tip of the week series, I wanted to share such script with you. I hope that it can be useful for some of you.

Quick connection test

Basically, the script is based on Test-NetConnection command. I running it inside single Invoke-Command script block. The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. By using a single Invoke-Command command, you can run commands on multiple computers.

test connection table

Final script:

Test-NetConnetion with -InformationLevel set to Quiet. This just returns only True or False result:

    Try{
        $Array      = @()
        $Servers    = Get-Content "c:\users\$env:username\desktop\servers.txt"
        $PortNumber = '80'
        $Array = Invoke-Command -cn $Servers {param($PortNumber)
 
                    $Destinations = "test1.powershellbros.com","test2.powershellbros.com"  
                               
                    $Object = New-Object PSCustomObject
                    $Object | Add-Member -MemberType NoteProperty -Name "Servername" -Value $env:computername
                    $Object | Add-Member -MemberType NoteProperty -Name "Port" -Value $PortNumber
 
                    Foreach ($Item in $Destinations){
                        $Result = Test-NetConnection -Port $PortNumber -cn $Item -InformationLevel Quiet
                        $Object | Add-Member -Type NoteProperty -Name  "Destination" -Value $Item -Force
                        $Object | Add-Member -Type NoteProperty -Name  "Connection" -Value $Result -Force
                        $Object 
                    } 
                      
                          
        } -ArgumentList $PortNumber -ErrorAction stop | select * -ExcludeProperty runspaceid, pscomputername,PSShowComputerName
    }
    Catch [System.Exception]{
        Write-host "Error" -backgroundcolor red -foregroundcolor yellow
        $_.Exception.Message
    }
 
    $Array | Out-GridView -Title "Results" 
    $Array | Export-Csv $env:userprofile\desktop\results.csv -Force -NoTypeInformation 

Or a more detailed version with Ping Time, Ping and TCP values:

 
    Try{
        $Array      = @()
        $Servers    = Get-Content "c:\users\$env:username\desktop\servers.txt"
        $PortNumber = '80'
        $Array = Invoke-Command -cn $Servers {param($PortNumber)
 
                    $Destinations = "test1.powershellbros.com","test2.powershellbros.com" 
                               
                    $Object = New-Object PSCustomObject
                    $Object | Add-Member -MemberType NoteProperty -Name "Servername" -Value $env:computername
                    $Object | Add-Member -MemberType NoteProperty -Name "Port" -Value $PortNumber
 
                    Foreach ($Item in $Destinations){
                        $Result = Test-NetConnection -Port $PortNumber -cn $Item
                        $Delay = $Result.'PingReplyDetails'.Roundtriptime | % {("$_"  + " ms")}
                        $Object | Add-Member -Type NoteProperty -Name  "Destination" -Value $Item -Force
                        $Object | Add-Member -Type NoteProperty -Name  "Ping Time" -Value $Delay -Force
                        $Object | Add-Member -Type NoteProperty -Name  "Ping" -Value $Result.PingSucceeded -Force
                        $Object | Add-Member -Type NoteProperty -Name  "TCP" -Value $Result.TcpTestSucceeded -Force
                        $Object 
                    } 
                      
                          
        } -ArgumentList $PortNumber -ErrorAction stop | select * -ExcludeProperty runspaceid, pscomputername,PSShowComputerName
    }
    Catch [System.Exception]{
        Write-host "Error" -backgroundcolor red -foregroundcolor yellow
        $_.Exception.Message
    }
 
    $Array | Out-GridView -Title "Results" 
    $Array | Export-Csv $env:userprofile\desktop\results.csv -Force -NoTypeInformation 

2 thoughts on “PowerShell Tip of the Week: Quick connection test on remote machines

  1. hello pawel

    first, great job! For my part, I would say that there is just one thing missing: transform the code into an advanced function to make the code easily maintainable and reusable.

    I did it, for my part, if you’re interested I could post here. Let me know

    1. Hi Oliv,
      Thanks 🙂 Yes it can be transformed into function – I prefer to share simple codes first. Feel free to share here your scripts.
      Kind regards

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.