Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Building a Unified Ping and Port Connectivity Report with PowerShell

In this article, I will explore two PowerShell scripts that test network connectivity (Ping) and port availability (Port Query) on multiple computers. I will discuss the scripts, their differences, and how to use them effectively. First Script is used for Synchronous Network Testing. The first script uses a simple foreach loop to iterate over a list of computers and test their network connectivity (ICMP) and port availability.

$computers =  "ad001", "psdomain02", "192.168.34.39","server1"
foreach ($com in $computers)
{
    $ping = Test-Connection -computername $com -Quiet -Count 1
    $portcheck = Test-NetConnection -Port 445 -ComputerName $com -WarningAction SilentlyContinue
    $overall =  "Ping = $ping | Port = $($portcheck.TcpTestSucceeded)"
    [pscustomobject]@{
        ServerName = $com
        Success = $overall
        PingSucceeded = $ping
    }
}

In both the script is uses the Test-Connection cmdlet to ping each computer and the Test-NetConnection cmdlet to test port 445 connectivity. The results are stored in a custom object with properties for the server name, overall success, and ping success.

Advanced Version of this Article: High-Performance Network Auditing: Custom Ping and TCP Port Testing in PowerShell .Net Objects and Parallel Ping and Port Testing in PowerShell Using Runspaces

Second script is Asynchronous Network Testing which uses PowerShell jobs to test network connectivity and port availability asynchronously.

$computers = "ad001", "psdomain02", "192.168.34.39", "server1"
$jobs = foreach ($com in $computers) {
    Start-Job -ArgumentList $com -ScriptBlock {
        param($ComputerName)
        $ping = Test-Connection -ComputerName $ComputerName -Quiet -Count 1
        $portcheck = Test-NetConnection -Port 445 -ComputerName $ComputerName -WarningAction SilentlyContinue
        [pscustomobject]@{
            ServerName     = $ComputerName
            PingSucceeded = $ping
            PortSucceeded = $portcheck.TcpTestSucceeded
            Overall       = "Ping = $ping | Port = $($portcheck.TcpTestSucceeded)"
        }
    }
}

# Wait for all jobs to finish
Wait-Job $jobs

# Collect results
$results = Receive-Job $jobs | Select-Object ServerName, PingSucceeded, PortSucceeded, Overall

# Cleanup
Remove-Job $jobs

$results

This script starts a job for each computer in the list, which tests network connectivity and port availability. The results are collected using the Receive-Job cmdlet and stored in a variable. 

PowerShell console window displaying a table of server connectivity results, showing True or False status for Ping and Port 445 for various hostnames and IP addresses

Download this script ping_port_scanner_powershell.ps1 or it is also available on github.com.

Key differences
The main difference between the two scripts is the way they handle the testing process. The first script tests each computer synchronously, one after the other, while the second script uses jobs to test multiple computers asynchronously. This makes the second script much faster for large lists of computers.

I had an question when I shared this script with few people. What will happen if there are errors in Test-Connection and Test-NetConnection? Just to note when using -Quiet parameter with Test-Connection and running Test-NetConnection does not generate any error hence you don't require try catch block.

This script is created on the PowerShell version 5.1 (Install and Import threadjob module), If you want more faster way use PowerShell v7 and use Start-ThreadJob instead of Start-Job command.

In conclusion, both scripts provide a useful way to test network connectivity (ping / ICMP) and port availability (telnet / port query) on multiple computers. The choice of script depends on the specific requirements of your environment. By using PowerShell jobs, you can significantly speed up the testing process for large lists of computers.

Useful Articles
How to Install and Use Microsoft PowerShell on Linux
Configure PowerShell remoting between Windows and Linux
Get-PSRepository WARNING Unable to find module repositories
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send
Creating an internal PowerShell module repository
How to sign PowerShell ps1 scripts
PowerShell Convert MAC address to Link-local address IPv6
PowerShell fix repair The trust relationship between this workstation and the primary domain failed
Resovled issue with PowerShell - Trust relationship Rejoin computers in domain without restart
PowerShell Invoke-WebRequest The request was aborted Could not create SSL TLS secure channel
PowerShell Invoke-WebRequest The underlying connection was closed: Could not establish trust relationship for the SSL TLS secure channel.

Go Back

Comment

Protected by Mathcha

Blog Search

Page Views

1 4 6 7 6 3 6 9

Archive

Follow me on Blogarama