-
Notifications
You must be signed in to change notification settings - Fork 6
/
checkInternetContinuous.ps1
93 lines (81 loc) · 7.74 KB
/
checkInternetContinuous.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
Write-Host " " -ForegroundColor White -BackgroundColor Blue
Write-Host " Anton's continuous internet connection checker " -ForegroundColor White -BackgroundColor Blue
Write-Host " " -ForegroundColor White -BackgroundColor Blue
Write-Host ""
Write-Host " Started at" (get-date).ToString('T')
Write-Host ""
$stop = 0
$attempt = 1
$stopwatch = [system.diagnostics.stopwatch]::StartNew()
$lastAttemptTimestamp = $stopwatch.Elapsed.TotalSeconds
$currentTimestamp = $stopwatch.Elapsed.TotalSeconds
$secondsConnected = 0
$secondsDisconnected = 0
$connectedSince = 0
$connectedSinceMs = 0
$disconnectedSince = 0
$disconnectedSinceMs = 0
$previousStatus = "None"
$cursorResetPosition = $host.UI.RawUI.CursorPosition
$cursorCurrentPosition = $host.UI.RawUI.CursorPosition
$cursorExitPosition = $host.UI.RawUI.CursorPosition
do {
### Start ping
$host.UI.RawUI.CursorPosition = $cursorResetPosition
Write-Host " Pinging google.com... " -NoNewLine -ForegroundColor Yellow
$cursorCurrentPosition = $host.UI.RawUI.CursorPosition
$host.UI.RawUI.CursorPosition = $cursorExitPosition
$ping = test-connection -comp www.google.com -Quiet
$host.UI.RawUI.CursorPosition = $cursorCurrentPosition
### Update result
$currentTimestamp = $stopwatch.Elapsed.TotalSeconds
if ($ping) {
if ($previousStatus -ne "Connected") {
$connectedSince = (get-date)
$connectedSinceMs = $currentTimestamp
}
$secondsConnected += $currentTimestamp - $lastAttemptTimestamp
Write-Host "[ Connected ]" -NoNewLine -ForegroundColor Black -BackgroundColor Green
Write-Host " since " -NoNewLine -ForegroundColor Yellow
$roundedConnectedSince = [math]::Round($currentTimestamp - $connectedSinceMs)
Write-Host $connectedSince.ToString('T') "($roundedConnectedSince s ago)" -NoNewLine -ForegroundColor Black -BackgroundColor Yellow
Write-Host " "
$previousStatus = "Connected"
} else {
if ($previousStatus -ne "Disconnected") {
$disconnectedSince = (get-date)
$disconnectedSinceMs = $currentTimestamp
}
$secondsDisconnected += $currentTimestamp - $lastAttemptTimestamp
Write-Host "[ Disconnected ]" -NoNewLine -ForegroundColor Black -BackgroundColor Red
Write-Host " since " -NoNewLine -ForegroundColor Yellow
$roundedConnectedSince = [math]::Round($currentTimestamp - $disconnectedSinceMs)
Write-Host $disconnectedSince.ToString('T') "($roundedConnectedSince s ago)" -NoNewLine -ForegroundColor Black -BackgroundColor Yellow
Write-Host " "
$previousStatus = "Disconnected"
}
### Display status bar
$statusBarLength = 10
$connectedPercentage = ($secondsConnected / ($secondsConnected + $secondsDisconnected)) * 100
$connectedPercentageLength = ($connectedPercentage / 100) * $statusBarLength
$disconnectedPercentageLength = $statusBarLength - $connectedPercentageLength
Write-Host ""
Write-Host " Uptime: [" -NoNewLine
for ($i = 0; $i -lt $connectedPercentageLength; $i++) {
Write-Host " " -NoNewLine -BackgroundColor Green
}
Write-Host "|" -NoNewLine -BackgroundColor Yellow -ForegroundColor White
for ($i = 0; $i -lt $disconnectedPercentageLength; $i++) {
Write-Host " " -NoNewLine -BackgroundColor Red
}
$roundedConnectedPercentage = [math]::Round($connectedPercentage)
Write-Host "] $roundedConnectedPercentage%"
$roundedSecondsConnected = [math]::Round($secondsConnected)
$roundedSecondsDisconnected = [math]::Round($secondsDisconnected)
Write-Host " Connected for" $roundedSecondsConnected "s, disconnected for" $roundedSecondsDisconnected "s"
### Reset
Write-Host "" -NoNewLine
$lastAttemptTimestamp = $currentTimestamp
$attempt++
$cursorExitPosition = $host.UI.RawUI.CursorPosition
} until ($stop -eq 1)