0

I wonder if Powershell Invoke-WebRequest will throw client side errors on a download request, such as disk full?

For instance:

 # Download recording
        try {
            $ProgressPreference = 'SilentlyContinue'
            $r = Invoke-WebRequest -Uri $jwtURL -OutFile $outfile
            $a = [int]$r.StatusCode
        }
        catch {
            $a = [int]$_.Exception.Response.StatusCode
        }

Would the code above get only http status code, or also write errors on the local file system?

Riccardo
  • 2,054
  • 6
  • 33
  • 51

1 Answers1

1

Moving from the comments to here as this is too long for comments

Remember there are error types. Terminating and non-terminating. Not every error is terminating so, you have to force that as well.

See this article: Powershell Try Catch Tutorial & Guide

so, stuff like this example:

$url = 'https://stackoverflow.com'
$req = [system.Net.WebRequest]::Create($url)

try {$res = $req.GetResponse()} 
catch [System.Net.WebException] 
{$res = $_.Exception.Response}
$res.StatusCode
postanote
  • 15,138
  • 2
  • 14
  • 25