-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWinNetPulse.ps1
More file actions
237 lines (163 loc) · 4.83 KB
/
Copy pathWinNetPulse.ps1
File metadata and controls
237 lines (163 loc) · 4.83 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
<#
WinNetPulse v1.3.2
New:
Magenta color when packet loss
#>
param(
[switch]$NoClear
)
if (-not $NoClear) {
Clear-Host
}
function Show-Header {
Write-Host "===============================" -ForegroundColor Cyan
Write-Host " WinNetPulse v1.3.2" -ForegroundColor Green
Write-Host "===============================" -ForegroundColor Cyan
}
function Select-Mode {
Write-Host "Select mode:" -ForegroundColor Yellow
Write-Host "1) Ping"
Write-Host "2) Tracert"
$choice = Read-Host "Choose (1-2)"
if ($choice -eq "2") { return "tracert" }
return "ping"
}
function Select-PingCount {
Write-Host "Select ping count:" -ForegroundColor Yellow
Write-Host "1) 10"
Write-Host "2) 30"
Write-Host "3) 50"
Write-Host "4) 100"
Write-Host "5) 200"
Write-Host "6) Infinite (-t)"
$choice = Read-Host "Choose (1-6)"
switch ($choice) {
"1" { return 10 }
"2" { return 30 }
"3" { return 50 }
"4" { return 100 }
"5" { return 200 }
"6" { return -1 }
default { return 10 }
}
}
function Write-ColoredLatency($time, $text) {
if ($time -lt 20) {
Write-Host $text -ForegroundColor Green
}
elseif ($time -lt 80) {
Write-Host $text -ForegroundColor Yellow
}
else {
Write-Host $text -ForegroundColor Red
}
}
function Get-TimeStamp {
return (Get-Date).ToString("yyyy/MM/dd HH:mm:ss.ff")
}
function Invoke-PingTest($target, $count) {
$min = [int]::MaxValue
$max = 0
$sum = 0
$sent = 0
$received = 0
if ($count -eq -1) {
Write-Host "Infinite ping mode. Press Q to stop." -ForegroundColor Yellow
while ($true) {
if ([console]::KeyAvailable) {
$key = [console]::ReadKey($true)
if ($key.Key -eq 'Q') { break }
}
$sent++
$reply = Test-Connection -ComputerName $target -Count 1 -ErrorAction SilentlyContinue
$ts = Get-TimeStamp
if ($reply) {
$time = $reply.ResponseTime
$ttl = $reply.TimeToLive
$received++
if ($time -lt $min) { $min = $time }
if ($time -gt $max) { $max = $time }
$sum += $time
$text = "[$sent] ${time}ms TTL=$ttl [$ts]"
Write-ColoredLatency $time $text
}
else {
Write-Host "[$sent] Request timed out. [$ts]" -ForegroundColor Magenta
}
Start-Sleep -Seconds 1
}
}
else {
for ($i=1; $i -le $count; $i++) {
$sent++
$reply = Test-Connection -ComputerName $target -Count 1 -ErrorAction SilentlyContinue
$ts = Get-TimeStamp
if ($reply) {
$time = $reply.ResponseTime
$ttl = $reply.TimeToLive
$received++
if ($time -lt $min) { $min = $time }
if ($time -gt $max) { $max = $time }
$sum += $time
$text = "[$i/$count] ${time}ms TTL=$ttl [$ts]"
Write-ColoredLatency $time $text
}
else {
Write-Host "[$i/$count] Request timed out. [$ts]" -ForegroundColor Magenta
}
Start-Sleep -Seconds 1
}
}
if ($received -gt 0) {
$avg = [math]::Round($sum / $received,2)
}
else {
$min = 0
$avg = 0
}
$lost = $sent - $received
if ($sent -gt 0) {
$lossPercent = [math]::Round(($lost / $sent) * 100,2)
}
else {
$lossPercent = 0
}
return [pscustomobject]@{
Target = $target
Sent = $sent
Received = $received
Lost = $lost
LossPercent = $lossPercent
Min = $min
Max = $max
Avg = $avg
}
}
function Run-PingMode {
$target = Read-Host "Enter IP or domain"
$count = Select-PingCount
$result = Invoke-PingTest $target $count
Write-Host "`n===== WinNetPulse Summary =====" -ForegroundColor Cyan
Write-Host "Target: $($result.Target)"
Write-Host "Packets: Sent = $($result.Sent), Received = $($result.Received), Lost = $($result.Lost) ($($result.LossPercent)%)"
Write-Host "Min = $($result.Min) ms"
Write-Host "Max = $($result.Max) ms"
Write-Host "Avg = $($result.Avg) ms"
Write-Host "Finished at: $(Get-TimeStamp)"
Write-Host "=============================" -ForegroundColor Cyan
}
function Run-TracertMode {
$target = Read-Host "Enter IP or domain"
Write-Host "`nRunning tracert..." -ForegroundColor Yellow
tracert $target
}
Show-Header
$mode = Select-Mode
if ($mode -eq "tracert") {
Run-TracertMode
}
else {
Run-PingMode
}
Write-Host "`nPress Enter to exit..." -ForegroundColor DarkGray
Read-Host | Out-Null