How to get StatusCode from request in PowerShell

Question:

I need to get success and/or error status code from a request using power shell. And i’m always getting a blank status.

I have tried with Invoke-WebRequest and Invoke-RestMethod.
I have sucess on the call but can’t find a way of getting the status code.

Here how is currently written:

I have also tried:

I used this question (and other links) : Invoke-Restmethod: how do I get the return code?

Trying to solve this, my log does write “SuccessOnCall” but the StatusCode is blank.

Thank you.

Answer:

did you try pipe’ing with select-object ?

output

or just to get status code

output


to handle unsuccessful cases,
on PowerShell v#7 + include -SkipHttpErrorCheck parameter
or
you may need to make use of $Error array to get most recent error and access properties accordingly.

Source:

How to get StatusCode from request in PowerShell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply