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

75 lines
2.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"
2022-06-10 08:26:41 +00:00
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-06-10 08:26:41 +00:00
if ($TestNetwork) {
2022-04-13 16:50:06 +00:00
2022-03-14 13:55:02 +00:00
Write-Log "Connected !" "Green"
#Check for metered connection
[void][Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime]
$cost = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile().GetConnectionCost()
if ($cost.ApproachingDataLimit -or $cost.OverDataLimit -or $cost.Roaming -or $cost.BackgroundDataUsageRestricted -or ($cost.NetworkCostType -ne "Unrestricted")){
Write-Log "Metered connection detected." "Yellow"
if ($WAUConfig.WAU_DoNotRunOnMetered -eq 1) {
Write-Log "WAU is configured to bypass update checking on metered connection"
return $false
}
else{
Write-Log "WAU is configured to force update checking on metered connection"
return $true
}
}
else{
return $true
}
2022-04-13 16:50:06 +00:00
2022-03-14 13:55:02 +00:00
}
2022-06-10 08:26:41 +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-06-10 08:26:41 +00:00
if ($timeout -eq 300) {
2022-03-14 13:55:02 +00:00
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
}