From f934becad76fadea5055d9071c5a2b83796aed6d Mon Sep 17 00:00:00 2001 From: Katsuhiro Watanabe Date: Mon, 1 Dec 2025 15:45:08 +0900 Subject: [PATCH] Fix: Add Error handling on Invoke-WebRequest Header check --- Public/Functions/Other/Save-WebFile.ps1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Public/Functions/Other/Save-WebFile.ps1 b/Public/Functions/Other/Save-WebFile.ps1 index f074693a9..5941f4092 100644 --- a/Public/Functions/Other/Save-WebFile.ps1 +++ b/Public/Functions/Other/Save-WebFile.ps1 @@ -117,7 +117,13 @@ function Save-WebFile { Write-Verbose "Destination: $DestinationFullName" Write-Verbose 'Requesing HTTP HEAD to get Content-Length and Accept-Ranges header' - $remote = Invoke-WebRequest -UseBasicParsing -Method Head -Uri $SourceUrl + try { + $remote = Invoke-WebRequest -UseBasicParsing -Method Head -Uri $SourceUrl + } + catch { + Write-Warning "$_" # Error Example: Response status code does not indicate success: 404 (Not Found). + Return $null + } $remoteLength = [Int64]($remote.Headers.'Content-Length' | Select-Object -First 1) $remoteAcceptsRanges = ($remote.Headers.'Accept-Ranges' | Select-Object -First 1) -eq 'bytes'