Description:
This PowerShell script creates a simple network diagnostics tool. It allows users to perform basic network troubleshooting tasks such as pinging a host, checking DNS resolution, viewing network adapter information, and performing a traceroute. The script demonstrates working with network-related cmdlets, handling user input, and presenting network information in a readable format.
Full Script:
# Advanced Network Diagnostics Tool
# Function to display the menu
function Show-Menu {
Clear-Host
Write-Host "=== Advanced Network Diagnostics Tool ===" -ForegroundColor Cyan
Write-Host "1. Ping a host"
Write-Host "2. Resolve DNS"
Write-Host "3. View network adapter information"
Write-Host "4. Perform traceroute"
Write-Host "5. View open ports"
Write-Host "6. Check internet connectivity"
Write-Host "7. View IP configuration"
Write-Host "8. Perform a network speed test"
Write-Host "9. Flush DNS cache"
Write-Host "10. View routing table"
Write-Host "11. Exit"
}
# Function to ping a host
function Ping-Host {
$host_name = Read-Host "Enter the host name or IP address to ping"
Write-Host "`nPinging $host_name..." -ForegroundColor Yellow
$result = Test-Connection -ComputerName $host_name -Count 4 -ErrorAction SilentlyContinue
if ($result) {
$result | Format-Table @{Name="Source"; Expression={$_.PSComputerName}},
@{Name="Destination"; Expression={$_.Address}},
@{Name="IPV4Address"; Expression={$_.IPV4Address}},
ResponseTime
} else {
Write-Host "Unable to reach $host_name" -ForegroundColor Red
}
}
# Function to resolve DNS
function Resolve-DNSName {
$dns_name = Read-Host "Enter the domain name to resolve"
Write-Host "`nResolving $dns_name..." -ForegroundColor Yellow
try {
$result = Resolve-DnsName -Name $dns_name -ErrorAction Stop
$result | Format-Table Name, IPAddress
} catch {
Write-Host "Unable to resolve $dns_name. Error: $($_.Exception.Message)" -ForegroundColor Red
}
}
# Function to view network adapter information
function View-NetworkAdapters {
Write-Host "`nNetwork Adapter Information:" -ForegroundColor Yellow
Get-NetAdapter | Where-Object Status -eq "Up" | Format-Table Name, InterfaceDescription, Status, LinkSpeed
}
# Function to perform traceroute
function Perform-Traceroute {
$destination = Read-Host "Enter the destination for traceroute"
Write-Host "`nPerforming traceroute to $destination..." -ForegroundColor Yellow
Test-NetConnection -ComputerName $destination -TraceRoute |
Select-Object -ExpandProperty TraceRoute |
ForEach-Object { Write-Host $_ }
}
# Function to view open ports
function View-OpenPorts {
Write-Host "`nViewing open ports..." -ForegroundColor Yellow
Get-NetTCPConnection | Where-Object State -eq "Established" |
Format-Table LocalAddress, LocalPort, RemoteAddress, RemotePort, State
}
# Function to check internet connectivity
function Check-InternetConnectivity {
Write-Host "`nChecking internet connectivity..." -ForegroundColor Yellow
$testResult = Test-NetConnection -ComputerName "www.google.com" -InformationLevel "Detailed"
if ($testResult.TcpTestSucceeded) {
Write-Host "Internet is accessible." -ForegroundColor Green
} else {
Write-Host "Internet is not accessible." -ForegroundColor Red
}
$testResult | Format-List
}
# Function to view IP configuration
function View-IPConfiguration {
Write-Host "`nIP Configuration:" -ForegroundColor Yellow
Get-NetIPConfiguration | Format-List InterfaceAlias, IPv4Address, IPv6Address, DNSServer
}
# Function to perform a network speed test
function Perform-SpeedTest {
Write-Host "`nPerforming network speed test..." -ForegroundColor Yellow
# Note: This requires the installation of speedtest-cli
# You can install it using: winget install Ookla.Speedtest.CLI
try {
$result = speedtest --format=json --progress=no | ConvertFrom-Json
Write-Host "Download Speed: $([math]::Round($result.download.bandwidth / 125000, 2)) Mbps"
Write-Host "Upload Speed: $([math]::Round($result.upload.bandwidth / 125000, 2)) Mbps"
Write-Host "Ping: $($result.ping.latency) ms"
} catch {
Write-Host "Unable to perform speed test. Make sure speedtest-cli is installed." -ForegroundColor Red
}
}
# Function to flush DNS cache
function Flush-DNSCache {
Write-Host "`nFlushing DNS cache..." -ForegroundColor Yellow
Clear-DnsClientCache
Write-Host "DNS cache flushed successfully." -ForegroundColor Green
}
# Function to view routing table
function View-RoutingTable {
Write-Host "`nRouting Table:" -ForegroundColor Yellow
Get-NetRoute | Format-Table DestinationPrefix, NextHop, RouteMetric, ifIndex
}
# Main program loop
do {
Show-Menu
$choice = Read-Host "`nEnter your choice (1-11)"
switch ($choice) {
"1" { Ping-Host }
"2" { Resolve-DNSName }
"3" { View-NetworkAdapters }
"4" { Perform-Traceroute }
"5" { View-OpenPorts }
"6" { Check-InternetConnectivity }
"7" { View-IPConfiguration }
"8" { Perform-SpeedTest }
"9" { Flush-DNSCache }
"10" { View-RoutingTable }
"11" { Write-Host "Exiting program..." -ForegroundColor Yellow; break }
default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red }
}
if ($choice -ne "11") {
Read-Host "`nPress Enter to continue..."
}
} while ($choice -ne "11")
This advanced Network Diagnostics Tool includes:
- A comprehensive menu with 11 options
- Functions for various network diagnostic tasks:
- Pinging a host
- Resolving DNS names
- Viewing network adapter information
- Performing a traceroute
- Viewing open ports
- Checking internet connectivity
- Viewing IP configuration
- Performing a network speed test (requires speedtest-cli)
- Flushing DNS cache
- Viewing the routing table
- Use of various PowerShell cmdlets related to networking
- Error handling for network operations
- Formatting of output for better readability
This script provides a more comprehensive set of tools for network diagnostics and troubleshooting. It’s suitable for users who want to perform a wider range of network-related tasks and gain more detailed insights into their network configuration and performance.
Note: The speed test function requires the installation of speedtest-cli, which can be installed using the Windows Package Manager (winget) or downloaded from the Ookla website.