From 478735eedf714d474cf0e6cd3b7c3ba521a737ea Mon Sep 17 00:00:00 2001 From: Andreas Brett Date: Sun, 12 Nov 2023 20:13:16 +0100 Subject: [PATCH] catch IWR exceptions --- Winget-AutoUpdate/functions/Test-Network.ps1 | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Winget-AutoUpdate/functions/Test-Network.ps1 b/Winget-AutoUpdate/functions/Test-Network.ps1 index 2284b20..10e3dc1 100644 --- a/Winget-AutoUpdate/functions/Test-Network.ps1 +++ b/Winget-AutoUpdate/functions/Test-Network.ps1 @@ -17,11 +17,15 @@ function Test-Network { $ncsiHost = "www.msftconnecttest.com" $ncsiPath = "connecttest.txt" $ncsiContent = "Microsoft Connect Test" - } finally { - $ncsiResponse = Invoke-WebRequest -Uri "http://$($ncsiHost)/$($ncsiPath)" -UseBasicParsing } - if (($ncsiResponse.StatusCode -eq 200) -and ($ncsiResponse.content -eq $ncsiContent)) { + try { + $ncsiResponse = Invoke-WebRequest -Uri "http://$($ncsiHost)/$($ncsiPath)" -UseBasicParsing + } catch { + $ncsiResponse = $false + } + + if (($ncsiResponse) -and ($ncsiResponse.StatusCode -eq 200) -and ($ncsiResponse.content -eq $ncsiContent)) { Write-ToLog "Connected !" "Green" #Check for metered connection