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

78 lines
2.7 KiB
PowerShell
Raw Normal View History

# Function to check the connectivity
2023-09-15 14:38:54 +00:00
function Test-Network
{
#Init
2023-09-15 14:38:54 +00:00
$timeout = 0
2023-09-15 14:38:54 +00:00
# Test connectivity during 30 min then timeout
Write-ToLog -LogMsg 'Checking internet connection...' -LogColor 'Yellow'
2023-09-15 14:38:54 +00:00
while ($timeout -lt 1800)
{
$URLtoTest = 'https://raw.githubusercontent.com/Romanitho/Winget-AutoUpdate/main/LICENSE'
$URLcontent = ((Invoke-WebRequest -Uri $URLtoTest -UseBasicParsing).content)
2023-09-15 14:38:54 +00:00
if ($URLcontent -like '*MIT License*')
{
Write-ToLog -LogMsg 'Connected !' -LogColor 'Green'
2023-09-15 14:38:54 +00:00
# Check for metered connection
$null = (Add-Type -AssemblyName Windows.Networking)
$null = [Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime]
$cost = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile().GetConnectionCost()
2023-09-15 14:38:54 +00:00
if ($cost.ApproachingDataLimit -or $cost.OverDataLimit -or $cost.Roaming -or $cost.BackgroundDataUsageRestricted -or ($cost.NetworkCostType -ne 'Unrestricted'))
{
Write-ToLog -LogMsg 'Metered connection detected.' -LogColor 'Yellow'
2023-09-15 14:38:54 +00:00
if ($WAUConfig.WAU_DoNotRunOnMetered -eq 1)
{
Write-ToLog -LogMsg 'WAU is configured to bypass update checking on metered connection'
return $false
}
2023-09-15 14:38:54 +00:00
else
{
Write-ToLog -LogMsg 'WAU is configured to force update checking on metered connection'
return $true
2022-03-14 13:55:02 +00:00
}
}
2023-09-15 14:38:54 +00:00
else
{
return $true
}
}
2023-09-15 14:38:54 +00:00
else
{
Start-Sleep -Seconds 10
$timeout += 10
2023-09-15 14:38:54 +00:00
# Send Warning Notif if no connection for 5 min
2023-09-15 14:38:54 +00:00
if ($timeout -eq 300)
{
# Log
Write-ToLog -LogMsg "Notify 'No connection' sent." -LogColor 'Yellow'
2023-09-15 14:38:54 +00:00
# Notif
$Title = $NotifLocale.local.outputs.output[0].title
$Message = $NotifLocale.local.outputs.output[0].message
$MessageType = 'warning'
$Balise = 'Connection'
Start-NotifTask -Title $Title -Message $Message -MessageType $MessageType -Balise $Balise
}
}
}
2023-09-15 14:38:54 +00:00
# Send Timeout Notif if no connection for 30 min
Write-ToLog -LogMsg 'Timeout. No internet connection !' -LogColor 'Red'
2023-09-15 14:38:54 +00:00
# Notif
$Title = $NotifLocale.local.outputs.output[1].title
$Message = $NotifLocale.local.outputs.output[1].message
$MessageType = 'error'
$Balise = 'Connection'
Start-NotifTask -Title $Title -Message $Message -MessageType $MessageType -Balise $Balise
2023-09-15 14:38:54 +00:00
return $false
2022-10-24 09:51:26 +00:00
}