-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetcheck.ps1
More file actions
294 lines (252 loc) · 13 KB
/
netcheck.ps1
File metadata and controls
294 lines (252 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# MIT License
#
# Copyright © 2022 Steve Sinchak
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
##############################################################
### NetCheck Version .1 ###
### Public Repo: https://github.com/stevesinchak/NetCheck/ ###
##############################################################
# Script Variables
$CurrentPath=$PSScriptRoot
$RemoteHostTest="google.com"
$RemoteHostTestQty=10
$RemoteHostTestDelay=1 # Measured in Seconds
$LocalGatewayTestQty=10
$LocalGatewayTestDelay=1 # Measured in Seconds
$WriteLogToFile=$true
$LogFilePath="$CurrentPath\log.txt"
$SpeedTestDownloadURL="https://install.speedtest.net/app/cli/ookla-speedtest-1.1.1-win64.zip"
# Minimum Acceptable Criteria
$SpeedTestDownloadMinimum=50 # Measured in Mbps
$SpeedTestUploadMinimum=5 # Measured in Mbps
$AcceptableLocalPacketLoss=0 # Measured in %
$AcceptableLocalLatency=5 # Measured in milliseconds
$AcceptableLocalJitter=5 # Measured in milliseconds
$AcceptableRemotePacketLoss=0 # Measured in %
$AcceptableRemoteLatency=35 # Measured in milliseconds
$AcceptableRemoteJitter=10 # Measured in milliseconds
# Do not modify - Internal variable to tracks if any minimum acceptable criteria is not met
$HealthyConnection=$true
###########################################################################################################
### Logging helper function (Log-Item "message" "Green") ###
### Parameters: ###
### $message = Mandatory, what you want displayed on screen and in file if file logging is enabled ###
### $Color = Defaults to "White", but a PowerShell color can be passed to alter text display on screen ###
###########################################################################################################
function Log-Item {
param (
[Parameter(Mandatory)]
[string]$Message,
$Color = "White"
)
$DateTime=Get-Date
if ($Message -eq '-')
{
$Message="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
Write-Host "$Message" -ForegroundColor Blue
}
else {
Write-Host "$DateTime - " -ForegroundColor "White" -NoNewLine
Write-Host "$Message" -ForegroundColor $Color
}
# Optional write log to file
if ($WriteLogToFile)
{
"$DateTime - $Message" | Out-File -FilePath $LogFilePath -Append
}
}
#########################
### Main Script Start ###
#########################
Log-Item "-"
Log-Item "Starting up NetCheck" "Yellow"
Log-Item "-"
# Get local client IP
$LocalIP=Get-Netipaddress -AddressFamily IPv4 -InterfaceAlias Ethernet* | Select-Object -ExpandProperty "IPAddress"
# Get default gateway
$LocalGatewayIP=Get-NetRoute -DestinationPrefix "0.0.0.0/0" | Select-Object -ExpandProperty "NextHop"
Log-Item "Client Local IP address: $LocalIP"
Log-Item "Client Local Gateway: $LocalGatewayIP"
Log-Item "-"
################################################################
### Test client to local router to find local network issues ###
################################################################
$TestDuration=$LocalGatewayTestQty*$LocalGatewayTestDelay
Log-Item "Testing client to local router connection (this test will take about $TestDuration seconds)..." "Yellow"
Log-Item "[PC]<-----Local Network----->[Router]" "Cyan"
$LocalLatencyTestResult=Test-Connection $LocalGatewayIP -Count $LocalGatewayTestQty -Delay $LocalGatewayTestDelay
$LocalLatencyTestResultCalculations=$LocalLatencyTestResult | Measure-Object -Property ResponseTime -Minimum -Maximum -Average
$LocalLatencyAverage=$LocalLatencyTestResultCalculations | Select-Object -ExpandProperty "Average"
$LocalLatencyMinimum=$LocalLatencyTestResultCalculations | Select-Object -ExpandProperty "Minimum"
$LocalLatencyMaximum=$LocalLatencyTestResultCalculations | Select-Object -ExpandProperty "Maximum"
$LocalPacketLossPercentage=100-(($LocalLatencyTestResult.ResponseTime.Count/$LocalGatewayTestQty)*100)
# Calculating jitter (deviation between pings) for additional quality measure
$LatencyDifferenceTotal=0
for ($x=0;$x -lt $LocalLatencyTestResult.ResponseTime.Count-1;$x++)
{
$L1=$LocalLatencyTestResult[$x].ResponseTime
$L2=$LocalLatencyTestResult[$x+1].ResponseTime
$CalculatedDifference=0
if (($null -ne $L1) -or ($null -ne $L2))
{
$CalculatedDifference=[Math]::Abs($L1-$L2)
}
$LatencyDifferenceTotal += $CalculatedDifference
}
$LocalJitter=$LatencyDifferenceTotal/$LocalLatencyTestResult.ResponseTime.Count
Log-Item "Local Average Latency: $LocalLatencyAverage ms"
Log-Item "Local Minimum Latency: $LocalLatencyMinimum ms"
Log-Item "Local Maximum Latency: $LocalLatencyMaximum ms"
Log-Item "Local Packet Loss: $LocalPacketLossPercentage %"
Log-Item "Local Jitter: $LocalJitter ms"
# Check for maximum threshold violations
if ($LocalLatencyAverage -gt $AcceptableLocalLatency) {
Log-Item "LOCAL NETWORK ISSUE - Average Latency is too high indicating poor network condition!" "Red"
$HealthyConnection=$false
}
if ($LocalLatencyMinimum -gt $AcceptableLocalLatency) {
Log-Item "LOCAL NETWORK ISSUE - Minimum Latency is too high indicating poor network condition!" "Red"
$HealthyConnection=$false
}
if ($LocalLatencyMaximum -gt $AcceptableLocalLatency) {
Log-Item "LOCAL NETWORK ISSUE - Maximum Latency is too high indicating poor network condition!" "Red"
$HealthyConnection=$false
}
if ($LocalPacketLossPercentage -gt $AcceptableLocalPacketLoss) {
Log-Item "LOCAL NETWORK ISSUE - Packet loss is detected indicating poor network condition!" "Red"
$HealthyConnection=$false
}
if ($LocalJitter -gt $AcceptableLocalJitter) { Log-Item "LOCAL NETWORK ISSUE - Network jitter is too high indicating poor network condition!" "Red"
$HealthyConnection=$false
}
Log-Item "-"
##############################################################################
### Test client to local router and out to the Internet to find ISP issues ###
##############################################################################
$TestDuration=$RemoteHostTestQty*$RemoteHostTestDelay
Log-Item "Testing client to local router and out to the remote Internet host (this test will take about $TestDuration seconds)..." "Yellow"
Log-Item "[PC]<-----Local Network----->[Router]<-----Internet Network----->[Remote Host]" "Cyan"
$RemoteLatencyTestResult=Test-Connection $RemoteHostTest -Count $RemoteHostTestQty -Delay $RemoteHostTestDelay
$RemoteLatencyTestResultCalculations=$RemoteLatencyTestResult | Measure-Object -Property ResponseTime -Minimum -Maximum -Average
$RemoteLatencyAverage=$RemoteLatencyTestResultCalculations | Select-Object -ExpandProperty "Average"
$RemoteLatencyMinimum=$RemoteLatencyTestResultCalculations | Select-Object -ExpandProperty "Minimum"
$RemoteLatencyMaximum=$RemoteLatencyTestResultCalculations | Select-Object -ExpandProperty "Maximum"
$RemotePacketLossPercentage=100-(($RemoteLatencyTestResult.ResponseTime.Count/$RemoteHostTestQty)*100)
# Calculating jitter (deviation between pings) for additional quality measure
$LatencyDifferenceTotal=0
for ($x=0;$x -lt $RemoteLatencyTestResult.ResponseTime.Count-1;$x++)
{
$L1=$RemoteLatencyTestResult[$x].ResponseTime
$L2=$RemoteLatencyTestResult[$x+1].ResponseTime
$CalculatedDifference=0
if (($null -ne $L1) -or ($null -ne $L2))
{
$CalculatedDifference=[Math]::Abs($L1-$L2)
}
$LatencyDifferenceTotal += $CalculatedDifference
}
$RemoteJitter=$LatencyDifferenceTotal/$RemoteLatencyTestResult.ResponseTime.Count
Log-Item "Remote Average Latency: $RemoteLatencyAverage ms"
Log-Item "Remote Minimum Latency: $RemoteLatencyMinimum ms"
Log-Item "Remote Maximum Latency: $RemoteLatencyMaximum ms"
Log-Item "Remote Packet Loss: $RemotePacketLossPercentage %"
Log-Item "Remote Jitter: $RemoteJitter ms"
# Check for maximum threshold violations
if ($RemoteLatencyAverage -gt $AcceptableRemoteLatency) {
Log-Item "INTERNET CONNECTION ISSUE - Average Latency is too high indicating poor network condition!" "Red"
$HealthyConnection=$false
}
if ($RemoteLatencyMinimum -gt $AcceptableRemoteLatency) {
Log-Item "INTERNET CONNECTION ISSUE - Minimum Latency is too high indicating poor network condition!" "Red"
$HealthyConnection=$false
}
if ($RemoteLatencyMaximum -gt $AcceptableRemoteLatency) {
Log-Item "INTERNET CONNECTION ISSUE - Maximum Latency is too high indicating poor network condition!" "Red"
$HealthyConnection=$false
}
if ($RemotePacketLossPercentage -gt $AcceptableRemotePacketLoss) {
Log-Item "INTERNET CONNECTION ISSUE - Packet loss is detected indicating poor network condition!" "Red"
$HealthyConnection=$false
}
if ($RemoteJitter -gt $AcceptableRemoteJitter) {
Log-Item "INTERNET CONNECTION ISSUE - Network jitter is too high indicating poor network condition!" "Red"
$HealthyConnection=$false
}
Log-Item "-"
#####################################################################################
### Grab trace route to remote test host for advanced ISP routing troubleshooting ###
#####################################################################################
Log-Item "Running trace route to remote test host... (this can take more than 30 seconds)" "Yellow"
$TraceRoute = &TRACERT.EXE $RemoteHostTest
foreach($Route in $TraceRoute)
{
Log-Item "$Route "
}
######################################################################
### Run Internet speed test and check against minimum requirements ###
######################################################################
Log-Item "-"
Log-Item "Running Speedtest.net Bandwidth Test... (this usually takes around 30 seconds)" "Yellow"
# Download and extract SpeedTest.Net CLI Tool if it does not already exist in PWD
if (-Not (Test-Path -Path $CurrentPath\speedtest.exe -PathType Leaf))
{
Log-Item "Downloading and extracting Speedtest.net CLI tool because it does not already exist"
Invoke-WebRequest -Uri $SpeedTestDownloadURL -OutFile $CurrentPath\speedtest_temp.zip
Expand-Archive -Path speedtest_temp.zip -DestinationPath $CurrentPath
Remove-Item $CurrentPath\speedtest_temp.zip # Clean up temp download zip
}
# Run speedtest cli and export results in json. Then convert output to powershell object so we can analyze
$SpeedTestResults = &$CurrentPath\speedtest.exe --accept-license --format=json-pretty | ConvertFrom-Json
Log-Item "Calculating Results..." "Cyan"
$ISPName=$SpeedTestResults.isp
$ISPDownload=[Math]::round((($SpeedTestResults.download | Select-Object -ExpandProperty "bandwidth")/1000000)*8,2)
$ISPUpload=[Math]::round((($SpeedTestResults.upload | Select-Object -ExpandProperty "bandwidth")/1000000)*8,2)
$ISPResultURL=$SpeedTestResults.result | Select-Object -ExpandProperty "url"
Log-Item "ISP: $ISPName"
Log-Item "Download Speed: $ISPDownload Mbps"
Log-Item "Upload Speed: $ISPUpload Mbps"
Log-Item "Speed Test Review URL: $ISPResultURL"
Log-Item "-"
# Check Download and upload speed meet minimum requirements
if ($ISPDownload -gt $SpeedTestDownloadMinimum)
{
Log-Item "Internet download speed meets requirements (minimum speed of $SpeedTestDownloadMinimum Mbps)" "Green"
} else {
Log-Item "INTERNET DOWNLOAD SPEED DOES NOT MEET REQUIREMENT OF $SpeedTestDownloadMinimum Mbps!" "Red"
$HealthyConnection=$false
}
if ($ISPUpload -gt $SpeedTestUploadMinimum)
{
Log-Item "Internet upload speed meets requirements (minimum speed of $SpeedTestUploadMinimum Mbps)" "Green"
} else {
Log-Item "INTERNET UPLOAD SPEED DOES NOT MEET REQUIREMENT OF $SpeedTestUploadMinimum Mbps!" "Red"
$HealthyConnection=$false
}
############################################################
### Check if there were any acceptable criteria failures ###
############################################################
Log-Item "-"
if ($HealthyConnection)
{
Log-Item "Connection Health Checks have passed successfully!" "Green"
}
else {
Log-Item "Health checks have FAILED - Network issues are present, review log file for details!" "Red"
}
Log-Item "-"
################
### All Done ###
################
Log-Item "NetCheck Shutting Down" "Yellow"