wingetautoupdate/Winget-AutoUpdate/functions/Test-Network.ps1

48 lines
1.4 KiB
PowerShell
Raw Normal View History

2022-04-13 16:50:06 +00:00
#Function to check connectivity
2022-03-14 13:55:02 +00:00
function Test-Network {
2022-04-13 16:50:06 +00:00
2022-03-22 13:39:01 +00:00
#Init
2022-03-14 13:55:02 +00:00
$timeout = 0
2022-03-22 13:39:01 +00:00
#Test connectivity during 30 min then timeout
2022-03-14 13:55:02 +00:00
Write-Log "Checking internet connection..." "Yellow"
While ($timeout -lt 1800){
2022-04-13 16:50:06 +00:00
2022-03-22 13:39:01 +00:00
$TestNetwork = Test-NetConnection 8.8.8.8 -Port 443 -InformationLevel Quiet
2022-04-13 16:50:06 +00:00
2022-03-22 13:39:01 +00:00
if ($TestNetwork){
2022-04-13 16:50:06 +00:00
2022-03-14 13:55:02 +00:00
Write-Log "Connected !" "Green"
return $true
2022-04-13 16:50:06 +00:00
2022-03-14 13:55:02 +00:00
}
2022-03-22 13:39:01 +00:00
else{
2022-04-13 16:50:06 +00:00
2022-03-14 13:55:02 +00:00
Start-Sleep 10
$timeout += 10
2022-03-22 13:39:01 +00:00
#Send Warning Notif if no connection for 5 min
2022-03-14 13:55:02 +00:00
if ($timeout -eq 300){
Write-Log "Notify 'No connection' sent." "Yellow"
$Title = $NotifLocale.local.outputs.output[0].title
$Message = $NotifLocale.local.outputs.output[0].message
$MessageType = "warning"
$Balise = "connection"
Start-NotifTask $Title $Message $MessageType $Balise
}
2022-04-13 16:50:06 +00:00
2022-03-14 13:55:02 +00:00
}
2022-04-13 16:50:06 +00:00
2022-03-14 13:55:02 +00:00
}
2022-03-22 13:39:01 +00:00
#Send Timeout Notif if no connection for 30 min
2022-03-14 13:55:02 +00:00
Write-Log "Timeout. No internet connection !" "Red"
$Title = $NotifLocale.local.outputs.output[1].title
$Message = $NotifLocale.local.outputs.output[1].message
$MessageType = "error"
$Balise = "connection"
Start-NotifTask $Title $Message $MessageType $Balise
return $false
2022-04-13 16:50:06 +00:00
2022-03-14 13:55:02 +00:00
}