Skip to content

Instantly share code, notes, and snippets.

@markwragg
Last active April 12, 2019 12:06
Show Gist options
  • Save markwragg/78e923e9e7a1d5aa8a6acdcc2dc6bdce to your computer and use it in GitHub Desktop.
Save markwragg/78e923e9e7a1d5aa8a6acdcc2dc6bdce to your computer and use it in GitHub Desktop.
Powershell snippets to test functionality of the test-NetConnection cmdlet.
#Performs a simple Ping test to the default destination of internetbeacon.msedge.net
Test-NetConnection
#Performs a Ping test to google.com
$Ping = Test-NetConnection -ComputerName google.com | Select *
#Performs a Traceroute test to google.com
$TraceRt = Test-NetConnection -ComputerName google.com -TraceRoute
$TraceRt | Select *
#Performs a HTTP port test to google.com
$CommonTCP = Test-NetConnection -ComputerName google.com -CommonTCPPort HTTP
$CommonTCP | Select *
#Performs a TCP 443 port test to google.com
$TCP443 = Test-NetConnection -ComputerName google.com -Port 443
$TCP443 | Select *
#Tests the list of domains for a list of port responses
$TopDomains = @("facebook.com","twitter.com","google.com","youtube.com","wordpress.org","linkedin.com","wikipedia.org","bbc.co.uk","instagram.com","yahoo.com","wragg.io")
$PortList = @(80,443)
$TopDomains | ForEach-Object {
$Domain = Test-NetConnection $_ | Select ComputerName,RemoteAddress,PingSucceeded
$PortList | ForEach-Object {
$PortResult = (Test-NetConnection $Domain.ComputerName -Port $_).TcpTestSucceeded
$Domain | Add-Member –MemberType NoteProperty –Name "TCP$_" –Value $PortResult -Force
}
$Domain
} | FT
#Performs a traceroute against the list of domains
$TopDomains | ForEach-Object {
Test-NetConnection $_ -TraceRoute
} | FT
#Funky version of ping with colour coded time results and -t option for constant ping and -d option for dots view
Function fping ($Dest, [Switch]$t, [switch]$d){
Test-NetConnection $Dest
Do {
Write-Host "Pinging $Dest ..." -NoNewline
$TimeTaken = (Measure-Command{$Test = Test-NetConnection $Dest}).Milliseconds
$TimeColor = Switch ($TimeTaken)
{
{$_ -lt 150}{"Green"; break}
{$_ -lt 350}{"Yellow"; break}
{$_ -ge 350}{"Red"; break}
default {"Gray"; break}
}
If ($Test.PingSucceeded -eq $True) {
Switch ($d)
{
$true {Write-Host ("."*($timetaken/10)) -ForegroundColor $TimeColor}
default {Write-Host " succeeded -- TimeTaken: $($TimeTaken)ms" -ForegroundColor $TimeColor}
}
}
Start-Sleep 1
} Until ($t -eq $False)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment