Skip to content

Conversation

@KatsuhiroWatanabe
Copy link
Contributor

@KatsuhiroWatanabe KatsuhiroWatanabe commented Dec 1, 2025

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 KatsuhiroWatanabe changed the title Fix: Add Error handling on Invoke-WebRequest Header check Fix: Add Error handling for Invoke-WebRequest Header check Dec 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant