Network Connection Status Checker Script
This PowerShell script checks the status of network connections on your machine. By providing detailed information about active network interfaces, this script can help system administrators in monitoring network health, troubleshooting connectivity issues, and managing network resources more efficiently.
Step 1: Retrieve Network Adapter Information
We begin by retrieving information about all network adapters on the system. This includes their status, speeds, and types.
$networkAdapters = Get-NetAdapter | Where-Object { $_.Status -eq 'Up' }
In this line, we use the `Get-NetAdapter` cmdlet to get a list of all network adapters and filter them to include only those with a status of “Up,” indicating that they are currently active.
Step 2: Display Basic Information About Each Adapter
Next, we will iterate through the active network adapters and display key details, such as the adapter name, its interface description, and the link speed.
foreach ($adapter in $networkAdapters) { $adapterInfo = @{ 'Name' = $adapter.Name 'InterfaceDescription' = $adapter.InterfaceDescription 'LinkSpeed(Mbps)' = [math]::round($adapter.LinkSpeed / 1MB, 2) } $adapterInfo }
In this block, we create a custom hashtable to store and display the adapter’s essential attributes. We calculate the link speed in Mbps by converting from bytes to megabits.
Step 3: Check IP Configuration for Each Adapter
We will now retrieve and display the IP configuration for each active adapter, including its IP address and subnet mask.
foreach ($adapter in $networkAdapters) { $ipConfig = Get-NetIPAddress -InterfaceAlias $adapter.Name | Select-Object IPAddress, PrefixLength $ipConfig | ForEach-Object { Write-Host "Adapter: $($adapter.Name) - IP Address: $($_.IPAddress) - Subnet Mask: $($_.PrefixLength)" } }
This step uses `Get-NetIPAddress` to fetch the IP configuration details of each adapter. We select the `IPAddress` and `PrefixLength`, which represents the subnet mask, and then output the results.
Step 4: Check Network Connectivity
To ensure each adapter can reach a common external address (like Google), we will perform a simple ping test on each active network adapter.
foreach ($adapter in $networkAdapters) { $pingResult = Test-Connection -ComputerName "google.com" -Count 2 -ErrorAction SilentlyContinue if ($pingResult) { Write-Host "Adapter: $($adapter.Name) - Connectivity: Online" } else { Write-Host "Adapter: $($adapter.Name) - Connectivity: Offline" } }
In this section, we utilize `Test-Connection` to ping Google to verify connectivity. Depending on whether the ping is successful, we indicate whether the adapter is currently online or offline.
Step 5: Summarize Connectivity Status Report
Finally, we will summarize the overall connectivity status of the network adapters to present an easy-to-understand report.
Write-Host "Network Connection Status Report:" foreach ($adapter in $networkAdapters) { Write-Host "--------------------------------" Write-Host "Adapter: $($adapter.Name)" Write-Host "Status: Up" $ipConfig = Get-NetIPAddress -InterfaceAlias $adapter.Name | Select-Object IPAddress, PrefixLength foreach ($ip in $ipConfig) { Write-Host "IP Address: $($ip.IPAddress) | Subnet Mask: $($ip.PrefixLength)" } $pingResult = Test-Connection -ComputerName "google.com" -Count 1 -ErrorAction SilentlyContinue Write-Host "Connectivity: $(if ($pingResult) { 'Online' } else { 'Offline' })" }
This final block provides a consolidated connectivity status report for all active network adapters, ensuring that system administrators can review their network health quickly.
By executing this script, network administrators can gain valuable insights into the status of their network connections, enhancing their ability to manage and troubleshoot network resources effectively.