This PowerShell script allows users to select from a list of popular DNS servers and perform customizable ping tests to verify network connectivity. It supports both continuous and fixed-count pings, configurable intervals, optional logging, and displays summary statistics for comprehensive network diagnostics.
Click to view script…
<#
.SYNOPSIS
DNS Ping Test Utility
.DESCRIPTION
This script allows the user to choose from a list of popular DNS servers (e.g., Google, Cloudflare, OpenDNS, etc.)
and then performs either continuous or fixed-count ping tests to verify network connectivity.
Users can configure the interval between pings and optionally log the results to a file for further analysis.
.NOTES
Press Ctrl+C to exit continuous ping mode.
#>
# Define a list of DNS providers.
$dnsProviders = @(
[PSCustomObject]@{Name="Google DNS"; IP="8.8.8.8"},
[PSCustomObject]@{Name="Cloudflare DNS"; IP="1.1.1.1"},
[PSCustomObject]@{Name="OpenDNS"; IP="208.67.222.222"},
[PSCustomObject]@{Name="Quad9 DNS"; IP="9.9.9.9"},
[PSCustomObject]@{Name="Level3 DNS"; IP="4.2.2.2"}
)
# Display the DNS server options.
Write-Host "Select a DNS server to ping:" -ForegroundColor Cyan
for ($i = 0; $i -lt $dnsProviders.Count; $i++) {
Write-Host ("{0}. {1} ({2})" -f ($i + 1), $dnsProviders[$i].Name, $dnsProviders[$i].IP)
}
# Prompt the user to choose a DNS server.
[int]$choice = Read-Host "Enter the number corresponding to your choice"
if ($choice -lt 1 -or $choice -gt $dnsProviders.Count) {
Write-Error "Invalid selection. Exiting."
exit
}
$selectedDNS = $dnsProviders[$choice - 1]
Write-Host "You selected: $($selectedDNS.Name) ($($selectedDNS.IP))" -ForegroundColor Green
# Ask if the user wants a fixed number of pings or continuous ping.
$pingCountInput = Read-Host "Enter number of pings (or press Enter for continuous ping)"
if ($pingCountInput -match '^\d+$') {
$pingCount = [int]$pingCountInput
$isContinuous = $false
} else {
$isContinuous = $true
}
# Ask for the interval between pings in seconds.
$intervalInput = Read-Host "Enter interval in seconds between pings (default is 1 second)"
if ([string]::IsNullOrWhiteSpace($intervalInput)) {
$interval = 1
} else {
$interval = [double]$intervalInput
}
# Ask if the user wants to log the results to a file.
$logChoice = Read-Host "Do you want to log results to a file? (Y/N)"
$logEnabled = $false
if ($logChoice -match '^[Yy]') {
$logEnabled = $true
$logFile = Read-Host "Enter log file path (or press Enter for default 'DNSPingLog.txt')"
if ([string]::IsNullOrWhiteSpace($logFile)) {
$logFile = "DNSPingLog.txt"
}
Write-Host "Logging enabled. Results will be saved to $logFile" -ForegroundColor Green
}
# Initialize an array to store successful ping response times if a fixed ping count is specified.
if (-not $isContinuous) {
$results = @()
}
Write-Host "Starting ping test to $($selectedDNS.IP)..." -ForegroundColor Cyan
# Function to log output if logging is enabled.
function Log-Output {
param (
[string]$message
)
if ($logEnabled) {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $logFile -Value "[$timestamp] $message"
}
}
# Run the ping test.
if ($isContinuous) {
Write-Host "Press Ctrl+C to stop continuous ping."
while ($true) {
try {
$pingResult = Test-Connection -ComputerName $selectedDNS.IP -Count 1 -ErrorAction SilentlyContinue
$timestamp = Get-Date -Format "HH:mm:ss"
if ($pingResult) {
$replyTime = $pingResult.ResponseTime
$output = "[$timestamp] Ping $($i)/$($pingCount): Reply from $($selectedDNS.IP): time = $replyTime ms"
} else {
$output = "[$timestamp] Ping $($i)/$($pingCount): Request timed out for $($selectedDNS.IP)."
}
Write-Host $output
Log-Output -message $output
}
catch {
Write-Host "An error occurred: $_"
Log-Output -message "Error: $_"
}
Start-Sleep -Seconds $interval
}
} else {
for ($i = 1; $i -le $pingCount; $i++) {
try {
$pingResult = Test-Connection -ComputerName $selectedDNS.IP -Count 1 -ErrorAction SilentlyContinue
$timestamp = Get-Date -Format "HH:mm:ss"
if ($pingResult) {
$replyTime = $pingResult.ResponseTime
$output = "[$timestamp] Ping $($i)/$($pingCount): Reply from $($selectedDNS.IP): time = $replyTime ms"
$results += $replyTime
} else {
$output = "[$timestamp] Ping $($i)/$($pingCount): Request timed out for $($selectedDNS.IP)."
}
Write-Host $output
Log-Output -message $output
}
catch {
Write-Host "An error occurred: $_"
Log-Output -message "Error: $_"
}
Start-Sleep -Seconds $interval
}
# Provide summary statistics for the fixed-count ping test.
if ($results.Count -gt 0) {
$avgTime = [Math]::Round(($results | Measure-Object -Average).Average, 2)
Write-Host "`nPing test completed. Average response time: $avgTime ms" -ForegroundColor Green
Log-Output -message "Ping test completed. Average response time: $avgTime ms"
} else {
Write-Host "`nPing test completed. No successful pings." -ForegroundColor Yellow
Log-Output -message "Ping test completed. No successful pings."
}
}