4

I am using Powershell's Invoke-WebRequest (this also applies to Invoke-RestMethod) to make a call to an ElasticSearch cluster. This specific command often returns an error result (409 conflict).

Powershell sees the error state, throws an error and does not pass anything useful through the pipeline, and it spews the body of the response onto the console:

enter image description here

However, a) even though it's got an error code, I don't really care that it's errored (only that it's returned), and b) I want to be able to access the body of that response so that I can actually inspect the data contained within.

Is there any way to have PowerShell suppress the error output (-ErrorAction does not work) and pass something useful along the pipeline?

As a note, I actually want to invoke this with Invoke-RestMethod but as it and Invoke-WebRequest are essentially the same command, I've generalised this question to Invoke-WebRequest

Mark Henderson
  • 68,316
  • 31
  • 175
  • 255
  • I don't think you can do that with `Invoke-WebRequest`, you have to go deeper and use `System.Net.WebRequest` directly, I think I have some code somewhere where I do exactly what you need, is that an option? – Peter Hahndorf Aug 08 '16 at 16:59
  • Totally an option, if you have something you can share. I don't want you to bother writing something just for this though. – Mark Henderson Aug 08 '16 at 17:00
  • Beginning with PowerShell 7 Microsoft has added a `-SkipHttpErrorCheck` parameter to `Invoke-WebRequest`. Ref: [Invoke-WebRequest # -SkipHttpErrorCheck](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.2) – AlwaysLearning Feb 22 '22 at 05:53

1 Answers1

5

Last year I wrote a script to diagnose common IIS setup problems, it's on GitHub, for it I needed to analyse the response from the server, even for non-200 results, especially for non-200 results, so Invoke-WebRequest didn't work.

I removed some stuff and the essential code is below.

I use the .NET System.Net.WebRequest because even if a System.Net.WebException is thrown, you can still access the response-stream with reponse headers and the body.

$url = "http://localhost"

try {
    $r = [System.Net.WebRequest]::Create($url)
    $r.UserAgent=$userAgent
    $resp = $r.GetResponse()        

    Write-host "Response Headers:" 
    foreach ($HeaderKey in $resp.Headers) {
            $caption = $HeaderKey.PadLeft(15," ")
            Write-host "$caption`: $($resp.Headers[$HeaderKey])";
    }                            

    $reqstream = $resp.GetResponseStream()
    $sr = New-Object System.IO.StreamReader $reqstream
    $body = $sr.ReadToEnd()
    Write-host "$body"

    $resp.StatusCode 
}           
catch [System.Net.WebException] 
{              
    $resp = $_.Exception.Response

    if ($resp -eq $null)
    {
        Write-host $_.Exception
    }
    else
    {
        $reqstream = $resp.GetResponseStream()
        $sr = New-Object System.IO.StreamReader $reqstream
        $body = $sr.ReadToEnd()

        Write-host -Text "Response Headers:" 
        Echo "         Status: $([int]$resp.StatusCode) - $($resp.StatusCode)"
        foreach ($HeaderKey in $resp.Headers) {
                $caption = $HeaderKey.PadLeft(15," ")
                Write-host "$caption`: $($resp.Headers[$HeaderKey])";
        }
        Write-host "$body"

        $resp.StatusCode                
    }                    

} catch {            
    Write-host  $_.Exception
}
Peter Hahndorf
  • 13,763
  • 3
  • 37
  • 58