How to get StatusCode from request in PowerShell
Asked Answered
M

3

11

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:

$resource = "some url"
$Logfile = "C:/path/log.log"

function LogWrite
{
    Param([string]$logstring)

    Add-content $logfile -value $logstring
}

Try
{
    $Response = Invoke-WebRequest -Method Post -Uri $resource 
    Write-Output("Success.")
    LogWrite $Date
    LogWrite SuccessOnCall
    LogWrite  $Response.StatusCode
}
Catch
{
    $ErrorMessage = $_.Exception.Message
    Write-Output($ErrorMessage)
    $FailedItem = $_.Exception
    Write-Output($FailedItem)
    LogWrite $Date
    LogWrite ErrorOnCall
    LogWrite $ErrorMessage
    Break
}

I have also tried:

LogWrite "StatusCode:" $Response.Exception.Response.StatusCode.value__ 

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.

Mange answered 19/4, 2019 at 0:53 Comment(3)
I can't replicate this using www.google.com as the URL. Using a get succeeds and a post fails but the status code is written correctly to the log in both cases. SuccessOnCall 200 ErrorOnCall The remote server returned an error: (405) Method Not Allowed.Currier
As an aside: Please avoid pseudo method syntax: instead of Write-Output(arg1, ...), use Write-Output arg1 ... - PowerShell cmdlets and functions are invoked like shell commands , not like methods. That is, no parentheses around the argument list, and whitespace-separated arguments (, constructs an array as a single argument). Better yet, instead of calling Write-Output, use implicit output.Ecklund
You'll want to refer to $_.Exception.Response.StatusCode.value__ inside the catch blockDeloisedelong
N
21

did you try pipe'ing with select-object ?

Invoke-WebRequest -Uri apiEndpoint -UseBasicParsing | Select-Object StatusCode

output

StatusCode
 ----------
   200

or just to get status code

Invoke-WebRequest -Uri apiEndpoint -UseBasicParsing | Select-Object -Expand StatusCode

output

200

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.

Neo answered 21/5, 2020 at 18:55 Comment(3)
How can I parse output if there is an error? For example, If code is 200, all okay, i get "200" as output. But if 404, I get several error strings even with "Select-Object -Expand StatusCode" :)Brauer
open to exploration :)Neo
learn.microsoft.com/en-us/powershell/module/…Metro
P
4

The below example shows how to get the response Status code (even if the response status code was not 200). We can get the exception information in the catch block using the $_ variable ( https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-exceptions?view=powershell-7.4#trycatch )

$Url = "https://some.web.page.net"
try
    {  
    $request = Invoke-WebRequest $Url -UseBasicParsing -UseDefaultCredentials       
    [int]$StatusCode = $request.StatusCode
    }
catch 
    {
    [int]$StatusCode = $_.Exception.Response.StatusCode
    $errorMessage = $_.Exception.Message
    }
$StatusCode
Partisan answered 8/2 at 15:15 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Palaeontography
L
0
curl -Uri 'google.com' | select-object StatusCode
Loquitur answered 24/11, 2021 at 14:8 Comment(1)
does this rly work? I cannot get it to runSushi

© 2022 - 2024 — McMap. All rights reserved.