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:
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
Invoke-WebRequest
, you have to go deeper and useSystem.Net.WebRequest
directly, I think I have some code somewhere where I do exactly what you need, is that an option?-SkipHttpErrorCheck
parameter toInvoke-WebRequest
. Ref: Invoke-WebRequest # -SkipHttpErrorCheck