Skip to content

Commit c6856b7

Browse files
committed
fix Test-Network
1 parent aa6f0ca commit c6856b7

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed

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

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
function Test-Network {
44

5-
#Init
5+
# Init
66
$timeout = 0
77

88
#Test connectivity during 30 min then timeout
99
Write-ToLog "Checking internet connection..." "Yellow"
1010

1111
try {
1212
$NlaRegKey = "HKLM:\SYSTEM\CurrentControlSet\Services\NlaSvc\Parameters\Internet"
13-
$ncsiHost = Get-ItemPropertyValue -Path $NlaRegKey -Name ActiveWebProbeHost
14-
$ncsiPath = Get-ItemPropertyValue -Path $NlaRegKey -Name ActiveWebProbePath
13+
$ncsiHost = Get-ItemPropertyValue -Path $NlaRegKey -Name ActiveWebProbeHost
14+
$ncsiPath = Get-ItemPropertyValue -Path $NlaRegKey -Name ActiveWebProbePath
1515
$ncsiContent = Get-ItemPropertyValue -Path $NlaRegKey -Name ActiveWebProbeContent
1616
}
1717
catch {
@@ -20,7 +20,7 @@ function Test-Network {
2020
$ncsiContent = "Microsoft Connect Test"
2121
}
2222

23-
While ($timeout -lt 1800) {
23+
while ($timeout -lt 1800) {
2424
try {
2525
$ncsiResponse = Invoke-WebRequest -Uri "http://$($ncsiHost)/$($ncsiPath)" -UseBasicParsing -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::Chrome); # DevSkim: ignore DS137138 Insecure URL
2626
}
@@ -31,74 +31,60 @@ function Test-Network {
3131
if (($ncsiResponse) -and ($ncsiResponse.StatusCode -eq 200) -and ($ncsiResponse.content -eq $ncsiContent)) {
3232
Write-ToLog "Connected !" "Green"
3333

34-
#Check for metered connection
34+
# Check for metered connection
3535
try {
36-
Add-Type -AssemblyName 'Windows.Networking, ContentType=WindowsRuntime' -ErrorAction Stop
37-
#[void][Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime]
36+
[void][Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime]
3837
$cost = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile().GetConnectionCost()
38+
39+
$networkCostTypeName = [Windows.Networking.Connectivity.NetworkCostType]::GetName(
40+
[Windows.Networking.Connectivity.NetworkCostType],
41+
$cost.NetworkCostType
42+
)
3943
}
4044
catch {
4145
Write-ToLog "Could not evaluate metered connection status - skipping check." "Gray"
4246
return $true
4347
}
4448

45-
if ($cost.ApproachingDataLimit -or $cost.OverDataLimit -or $cost.Roaming -or $cost.BackgroundDataUsageRestricted -or ($cost.NetworkCostType -ne "Unrestricted")) {
46-
49+
if ($cost.ApproachingDataLimit -or $cost.OverDataLimit -or $cost.Roaming -or $cost.BackgroundDataUsageRestricted -or ($networkCostTypeName -ne "Unrestricted")) {
4750
Write-ToLog "Metered connection detected." "Yellow"
4851

4952
if ($WAUConfig.WAU_DoNotRunOnMetered -eq 1) {
50-
5153
Write-ToLog "WAU is configured to bypass update checking on metered connection"
5254
return $false
53-
5455
}
5556
else {
56-
5757
Write-ToLog "WAU is configured to force update checking on metered connection"
5858
return $true
59-
6059
}
61-
6260
}
6361
else {
64-
6562
return $true
66-
6763
}
68-
6964
}
7065
else {
71-
7266
Start-Sleep 10
7367
$timeout += 10
7468

75-
#Send Warning Notif if no connection for 5 min
7669
if ($timeout -eq 300) {
77-
#Log
7870
Write-ToLog "Notify 'No connection' sent." "Yellow"
7971

80-
#Notif
8172
$Title = $NotifLocale.local.outputs.output[0].title
8273
$Message = $NotifLocale.local.outputs.output[0].message
8374
$MessageType = "warning"
8475
$Balise = "Connection"
8576
Start-NotifTask -Title $Title -Message $Message -MessageType $MessageType -Balise $Balise
8677
}
87-
8878
}
89-
9079
}
9180

92-
#Send Timeout Notif if no connection for 30 min
9381
Write-ToLog "Timeout. No internet connection !" "Red"
9482

95-
#Notif
9683
$Title = $NotifLocale.local.outputs.output[1].title
9784
$Message = $NotifLocale.local.outputs.output[1].message
9885
$MessageType = "error"
9986
$Balise = "Connection"
10087
Start-NotifTask -Title $Title -Message $Message -MessageType $MessageType -Balise $Balise
10188

10289
return $false
103-
104-
}
90+
}

0 commit comments

Comments
 (0)