Created
August 20, 2020 02:46
-
-
Save Petalousa/f9531b7d2d11daccd502a644568cf7c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @author Petalousa | |
# | |
# This file pings google's dns (8.8.8.8) every second to confirm connectivity | |
# if a ping to google fails, the script will ping the router (192.168.0.1) | |
# - if it can connect, the failure to connect to google and successful connection to the router is logged | |
# - if it canot connect, the failure to connect to google and the router is logged | |
# if the script was previously unable to ping google and now is able to ping google, the success will be logged | |
# | |
$LogFile = "variableConnectionTest.log" | |
$ExternalHost = "8.8.8.8" | |
$LocalHost = "192.168.0.1" | |
$CurrState = $true | |
$PrevState = $true | |
Write-Host "Running pingTest" | |
while ($true){ | |
$PingInstance = Get-CIMInstance -ClassName Win32_PingStatus -Filter "Address='$ExternalHost' AND Timeout=1000"; | |
$Status = $PingInstance.StatusCode | |
$LocalPingInstance = $null | |
$LocalStatus = $null | |
if ($Status -eq 0){ | |
# Write-Host "$(Get-Date) pinging $ExternalHost - $($PingInstance.ResponseTime) ms" | |
$CurrState = $true | |
}else{ | |
$LocalPingInstance = Get-CIMInstance -ClassName Win32_PingStatus -Filter "Address='$LocalHost' AND Timeout=50"; | |
$LocalStatus = $LocalPingInstance.StatusCode | |
$CurrState = $false | |
} | |
# if no connection is occuring | |
if($CurrState -eq $false){ | |
if($LocalStatus -eq 0){ | |
# if the router can be pinged | |
$ErrorString = "$(Get-Date) pinging $ExternalHost - failure; pinging $LocalHost - success" | |
Write-Host $ErrorString | |
Add-Content -Path $LogFile -Value $ErrorString | |
}else{ | |
# if the router cannot be pinged | |
$ErrorString = "$(Get-Date) pinging $ExternalHost - failure; pinging $LocalHost - failure" | |
Write-Host $ErrorString | |
Add-Content -Path $LogFile -Value $ErrorString | |
} | |
}elseif($PrevState -ne $CurrState){ | |
# if the script regained connection | |
$ErrorString = "$(Get-Date) pinging $ExternalHost - success" | |
Write-Host $ErrorString | |
Add-Content -Path $LogFile -Value $ErrorString | |
} | |
# update PrevState | |
$PrevState = $CurrState | |
Start-Sleep -s 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment