Skip to content

Commit 1185896

Browse files
authored
Update Test-Latency.ps1 PS7 Support
Added support for Powershell 7
1 parent 4969be6 commit 1185896

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

Scripts/Test-Latency.ps1

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Write-Host "GENERATING LATENCY REPORT..." -ForegroundColor Yellow
22
Write-Host "--------------------------------" -ForegroundColor Yellow
33

4+
# Save powershell version in a variable
5+
$psVersion = $PSVersionTable.PSVersion.Major
6+
47
# Function to determine latency status
58
function GetLatencyStatus {
69
param($Latency)
@@ -30,7 +33,11 @@ function HandleConnectionError {
3033
# Latency measurement to Google
3134
Write-Host "Google:" -ForegroundColor Yellow
3235
try {
33-
$LatencyGoogle = Test-Connection 8.8.8.8 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty ResponseTime
36+
if ($psVersion -ge 7) {
37+
$LatencyGoogle = Test-Connection 8.8.8.8 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty Latency
38+
} else {
39+
$LatencyGoogle = Test-Connection 8.8.8.8 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty ResponseTime
40+
}
3441
$LatencyGoogleAverage = ($LatencyGoogle | Measure-Object -Average).Average
3542
$StatusGoogle = GetLatencyStatus $LatencyGoogleAverage
3643
Write-Host "Average Latency: $LatencyGoogleAverage ms ($($StatusGoogle.Status))" -ForegroundColor $StatusGoogle.Color
@@ -42,7 +49,11 @@ Write-Host ""
4249
# Latency measurement to Cloudflare DNS
4350
Write-Host "Cloudflare DNS:" -ForegroundColor Yellow
4451
try {
45-
$LatencyCloudflare = Test-Connection 1.1.1.1 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty ResponseTime
52+
if ($psVersion -ge 7) {
53+
$LatencyCloudflare = Test-Connection 1.1.1.1 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty Latency
54+
} else {
55+
$LatencyCloudflare = Test-Connection 1.1.1.1 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty ResponseTime
56+
}
4657
$LatencyCloudflareAverage = ($LatencyCloudflare | Measure-Object -Average).Average
4758
$StatusCloudflare = GetLatencyStatus $LatencyCloudflareAverage
4859
Write-Host "Average Latency: $LatencyCloudflareAverage ms ($($StatusCloudflare.Status))" -ForegroundColor $StatusCloudflare.Color
@@ -54,7 +65,11 @@ Write-Host ""
5465
# Latency measurement to OpenDNS
5566
Write-Host "OpenDNS:" -ForegroundColor Yellow
5667
try {
57-
$LatencyOpenDNS = Test-Connection 208.67.222.222 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty ResponseTime
68+
if ($psVersion -ge 7) {
69+
$LatencyOpenDNS = Test-Connection 208.67.222.222 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty Latency
70+
} else {
71+
$LatencyOpenDNS = Test-Connection 208.67.222.222 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty ResponseTime
72+
}
5873
$LatencyOpenDNSAverage = ($LatencyOpenDNS | Measure-Object -Average).Average
5974
$StatusOpenDNS = GetLatencyStatus $LatencyOpenDNSAverage
6075
Write-Host "Average Latency: $LatencyOpenDNSAverage ms ($($StatusOpenDNS.Status))" -ForegroundColor $StatusOpenDNS.Color
@@ -66,7 +81,11 @@ Write-Host ""
6681
# Latency measurement to Verizon DNS
6782
Write-Host "Verizon DNS:" -ForegroundColor Yellow
6883
try {
69-
$LatencyVerizon = Test-Connection 151.197.0.38 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty ResponseTime
84+
if ($psVersion -ge 7) {
85+
$LatencyVerizon = Test-Connection 151.197.0.38 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty Latency
86+
} else {
87+
$LatencyVerizon = Test-Connection 151.197.0.38 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty ResponseTime
88+
}
7089
$LatencyVerizonAverage = ($LatencyVerizon | Measure-Object -Average).Average
7190
$StatusVerizon = GetLatencyStatus $LatencyVerizonAverage
7291
Write-Host "Average Latency: $LatencyVerizonAverage ms ($($StatusVerizon.Status))" -ForegroundColor $StatusVerizon.Color
@@ -78,7 +97,12 @@ Write-Host ""
7897
# Latency measurement to Amazon AWS
7998
Write-Host "Amazon AWS:" -ForegroundColor Yellow
8099
try {
81-
$LatencyAWS = Test-Connection 18.216.200.189 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty ResponseTime
100+
if ($psVersion -ge 7) {
101+
$LatencyAWS = Test-Connection 18.216.200.189 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty Latency
102+
103+
} else {
104+
$LatencyAWS = Test-Connection 18.216.200.189 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty ResponseTime
105+
}
82106
$LatencyAWSAverage = ($LatencyAWS | Measure-Object -Average).Average
83107
$StatusAWS = GetLatencyStatus $LatencyAWSAverage
84108
Write-Host "Average Latency: $LatencyAWSAverage ms ($($StatusAWS.Status))" -ForegroundColor $StatusAWS.Color
@@ -90,7 +114,11 @@ Write-Host ""
90114
# Latency measurement to Microsoft Azure
91115
Write-Host "Microsoft Azure:" -ForegroundColor Yellow
92116
try {
93-
$LatencyAzure = Test-Connection 18.216.200.189 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty ResponseTime
117+
if ($psVersion -ge 7) {
118+
$LatencyAzure = Test-Connection 18.216.200.189 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty Latency
119+
} else {
120+
$LatencyAzure = Test-Connection 18.216.200.189 -Count 5 -ErrorAction Stop | Select-Object -ExpandProperty ResponseTime
121+
}
94122
$LatencyAzureAverage = ($LatencyAzure | Measure-Object -Average).Average
95123
$StatusAzure = GetLatencyStatus $LatencyAzureAverage
96124
Write-Host "Average Latency: $LatencyAzureAverage ms ($($StatusAzure.Status))" -ForegroundColor $StatusAzure.Color

0 commit comments

Comments
 (0)