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

80 lines
2.5 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-10-25 15:18:58 +00:00
2022-03-22 13:39:01 +00:00
#Init
2022-03-14 13:55:02 +00:00
$timeout = 0
2022-10-25 15:18:58 +00:00
# Workaround for ARM64 (Access Denied / Win32 internal Server error)
$ProgressPreference = 'SilentlyContinue'
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-10-21 11:31:35 +00:00
$URLtoTest = "https://raw.githubusercontent.com/Romanitho/Winget-AutoUpdate/main/LICENSE"
2022-10-24 09:51:26 +00:00
$URLcontent = ((Invoke-WebRequest -URI $URLtoTest -UseBasicParsing).content)
2022-10-25 15:18:58 +00:00
2022-10-21 11:31:35 +00:00
if ($URLcontent -like "*MIT License*") {
2022-10-25 15:18:58 +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()
2022-10-25 15:18:58 +00:00
2022-10-18 13:23:39 +00:00
if ($cost.ApproachingDataLimit -or $cost.OverDataLimit -or $cost.Roaming -or $cost.BackgroundDataUsageRestricted -or ($cost.NetworkCostType -ne "Unrestricted")) {
2022-10-25 15:18:58 +00:00
Write-Log "Metered connection detected." "Yellow"
2022-10-25 15:18:58 +00:00
if ($WAUConfig.WAU_DoNotRunOnMetered -eq 1) {
2022-10-25 15:18:58 +00:00
Write-Log "WAU is configured to bypass update checking on metered connection"
return $false
2022-10-25 15:18:58 +00:00
}
2022-10-18 13:23:39 +00:00
else {
2022-10-25 15:18:58 +00:00
Write-Log "WAU is configured to force update checking on metered connection"
return $true
2022-10-25 15:18:58 +00:00
}
2022-10-25 15:18:58 +00:00
}
2022-10-18 13:23:39 +00:00
else {
return $true
}
2022-10-25 15:18:58 +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-10-25 15:18:58 +00:00
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-10-25 15:18:58 +00:00
2022-03-14 13:55:02 +00:00
}
2022-10-25 15:18:58 +00:00
2022-03-14 13:55:02 +00:00
}
2022-10-25 15:18:58 +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-10-24 09:51:26 +00:00
}