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

75 lines
2.4 KiB
PowerShell

#Function to check connectivity
function Test-Network {
#Init
$timeout = 0
#Test connectivity during 30 min then timeout
Write-Log "Checking internet connection..." "Yellow"
While ($timeout -lt 1800) {
$TestNetwork = Test-NetConnection 8.8.8.8 -Port 443 -InformationLevel Quiet
if ($TestNetwork) {
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
}
}
else {
Start-Sleep 10
$timeout += 10
#Send Warning Notif if no connection for 5 min
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
}
}
}
#Send Timeout Notif if no connection for 30 min
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
}