OSD icon indicating copy to clipboard operation
OSD copied to clipboard

Fix: Add Error handling for Invoke-WebRequest Header check

Open KatsuhiroWatanabe opened this issue 1 month ago • 0 comments

Pull Request: Fix: Add error handling for Invoke-WebRequest HEAD check

Change Summary

Added error handling around the HTTP HEAD request in Save-WebFile.ps1 to prevent execution when the source URL returns an error (e.g., 404 Not Found).

Code Added

-$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
+}

Impact

  • Only affects Save-WebFile.ps1.
  • No other logic modified.
  • Prevents downloading error HTML when the URL is invalid.

Test Command

Save-WebFile -SourceUrl "https://download.lenovo.com/pccbbs/mobiles/tp_x9-15_gen1_mt21q6-21q7_w11_24h2_202511notFound.exe" `
-DestinationName "notFound.exe" `
-DestinationDirectory C:\Temp\ `
-Verbose -Overwrite

KatsuhiroWatanabe avatar Dec 01 '25 07:12 KatsuhiroWatanabe