-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPingTestParallel.ps1
136 lines (125 loc) · 7.56 KB
/
PingTestParallel.ps1
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
#MADE FOR PS7!
#Pings in parallel. Reports to a text file and to console.
#$CSV is meant to have a field labeled "IP_Address". This can be changed as needed.
Clear-Host
#Variables
$OutLocation = #"C:\Location"
$Results = "$OutLocation\Results.txt"
$CSV = #"C:\CSVFile.csv"
#Note Variables represent thresholds in milliseconds. Reporting will display anything over the values. For example
#MinLatency = 50, will report x% of packets over 50 ms
#MedLatency = 30, will report x% of packets over 30 ms
#IdealLatency = 20, will report x% of packets over 20 ms
#Reporting in the console will create a tabed cascading list to show how much of each condition was met from highest threshold to lowest.
$MinLatency = 60
$MedLatency = 50
$IdealLatency = 30
#Create PSobject with ips parsed from CSV
$IPListCSV = Import-Csv $CSV | ForEach-Object {[PSCustomObject]@{
'IPAddress' = $_.IP_Address}
}
#Shorten IPListCSV Object
$IPList = $IPListCSV.IPAddress
#Parallel ping. Results recorded to each appropriate file
Write-Host ""
do {
$PingAmountAsk = Read-Host -Prompt "Enter number of pings to send as a test for each connection"
$PingAmount = [int]$PingAmountAsk
Clear-Host
if ($PingAmount.GetType().Name -ne "Int32") {
Write-Host Please enter a valid number.
Write-Host ""
}
} while ($PingAmount.GetType().Name -ne "Int32")
Clear-Host
Write-Host "Running Ping Test..."
Write-Host ""
Write-Host "Noticed Issues:"
Write-Host ""
$IPList | ForEach-Object -ThrottleLimit 10 -Verbose -Parallel {
$AliveTest = Test-Connection -Count 3 -Ping -IPv4 -DontFragment -TargetName $_
$IPFile = "$using:OutLocation\$_.txt"
$AliveTest | Out-File -FilePath $IPFile
if ($AliveTest.Status -notcontains "Success") {
Write-Host ===============================================================================
Write-Host -ForegroundColor DarkRed "Dead: $_ is not responding or has very high latency. Skipping."
Write-Output =============================================================================== | Out-File -FilePath $IPFile -Force -Append
Write-Output "Dead: $_ is not responding or has very high latency. Skipping." | Out-File -FilePath $IPFile -Force -Append
Write-Output =============================================================================== | Out-File -FilePath $IPFile -Force -Append
}
else {
$PingTest = Test-Connection -Count $using:PingAmount -Ping -IPv4 -DontFragment -TargetName $_
$HighTest = (($PingTest.Latency) -gt $using:MinLatency).count/$using:PingAmount -gt 0 -and (($PingTest.Latency -gt $using:MedLatency).count/$using:PingAmount -gt 0)
$MedTest = (($PingTest.Latency) -gt $using:MedLatency).count/$using:PingAmount -gt 0 -and (($PingTest.Latency -gt $using:IdealLatency).count/$using:PingAmount -gt 0)
$LowTest = (($PingTest.Latency) -gt $using:IdealLatency).count/$using:PingAmount -gt 0
$PingTest | Out-File -FilePath $IPFile -Force -Append
if ((($PingTest.Status) -match "TimedOut").count -gt 0 -or ((($PingTest.Latency) -gt $using:IdealLatency).count/$using:PingAmount) -gt 0) {
Write-Host ===============================================================================
"===============================================================================" | Out-File $IPFile -Force -Append
if ((($PingTest.Status) -match "TimedOut").count -gt 0) {
Write-Host -ForegroundColor DarkRed Dropped: $PingTest.DisplayAddress.GetValue(0) dropped ((($PingTest.Status) -match "TimedOut").count/$using:PingAmount).ToString("P") of packets
"Dropped: " + $PingTest.DisplayAddress.GetValue(0) + " dropped " + ((($PingTest.Status) -match "TimedOut").count/$using:PingAmount).ToString("P") + " of packets " | Out-File -FilePath $IPFile -Force -Append
}
}
if ($HighTest) {
Write-Host -ForegroundColor Red High Latency: $PingTest.DisplayAddress.GetValue(0) had ((($PingTest.Latency) -ge $using:MinLatency).count/$using:PingAmount).ToString("P") of packets over $using:MinLatency ms
"High Latency: " + $PingTest.DisplayAddress.GetValue(0) + " had " + ((($PingTest.Latency) -ge $using:MinLatency).count/$using:PingAmount).ToString("P") + " of packets over " + $using:MinLatency + " ms" | Out-File -FilePath $IPFile -Force -Append
}
if ($MedTest) {
Write-Host -ForegroundColor Yellow Medium Latency: $PingTest.DisplayAddress.GetValue(0) had ((($PingTest.Latency) -ge $using:MedLatency).count/$using:PingAmount).ToString("P") of packets over $using:MedLatency ms | Out-File -FilePath $IPFile -Force -Append
"Medium Latency: " + $PingTest.DisplayAddress.GetValue(0) + " had " + ((($PingTest.Latency) -ge $using:MedLatency).count/$using:PingAmount).ToString("P") + " of packets over " + $using:MedLatency + " ms" | Out-File -FilePath $IPFile -Force -Append
}
if ($LowTest) {
Write-Host -ForegroundColor White Low Latency: $PingTest.DisplayAddress.GetValue(0) had ((($PingTest.Latency) -ge $using:IdealLatency).count/$using:PingAmount).ToString("P") of packets over $using:IdealLatency ms | Out-File -FilePath $IPFile -Force -Append
"Low Latency: " + $PingTest.DisplayAddress.GetValue(0) + " had " + ((($PingTest.Latency) -ge $using:IdealLatency).count/$using:PingAmount).ToString("P") + " of packets over " + $using:IdealLatency + "ms" | Out-File -FilePath $IPFile -Force -Append
}
else {
if (($PingTest.Status) -notmatch "TimedOut") {
"===============================================================================" | Out-File -FilePath $IPFile -Force -Append
$PingTest.DisplayAddress.GetValue(0) + " had no latency based on defined thresholds" | Out-File -FilePath $IPFile -Force -Append
"===============================================================================" | Out-File -FilePath $IPFile -Force -Append
}
}
}
}
#Location and name of results
$Files = Get-ChildItem $OutLocation
#Write results to file (All results recorded)
$ResultTable = @{}
$ResultTable.IPs = @{}
foreach ($File in $Files) {
$Content = Get-Content -Path $File.FullName
$FileName = $File.BaseName
$ResultTable.IPs."$FileName" = @()
if ("$Content" -match 'TimedOut') {
if ("$Content" -match 'is not responding or has very high latency') {
$ResultTable.IPs."$FileName" = "Dead"
}
else {
$ResultTable.IPs."$FileName" = "Dropped"
}
}
elseif ("$Content" -match "over $IdealLatency") {
$ResultTable.IPs."$FileName" = "IdealLatency"
if ("$Content" -match "over $MedLatency") {
$ResultTable.IPs."$FileName" = "MedLatency"
if ("$Content" -match "over $MinLatency") {
$ResultTable.IPs."$FileName" = "HighLatency"
}
else {
$ResultTable.IPs."$FileName" = "MedLatency"
}
}
else {
$ResultTable.IPs."$FileName" = "LowLatency"
}
}
elseif ("$Content" -notmatch "Dropped: $FileName" -and "$Content" -notmatch "over $IdealLatency") {
$ResultTable.IPs."$FileName" = "OK"
}
}
$ResultTable.IPs.GetEnumerator() | Sort-Object -Property Value
$ResultTable.IPs.GetEnumerator() | Sort-Object -Property Value | Select-Object -Property Key,Value | Export-Csv -NoTypeInformation $Results
Write-Host ""
Write-Host "All results recorded to $Results"
Write-Host ""