Invoke-Restmethod: how do I get the return code?

前端 未结 2 1646
轻奢々
轻奢々 2021-02-05 01:30

Is there a way to store the return code somewhere when calling Invoke-RestMethod in PowerShell?

My code looks like this:

$url = \"http://www         


        
2条回答
  •  别跟我提以往
    2021-02-05 02:21

    You have a few options. Option 1 is found here. It pulls the response code from the results found in the exception.

    try {
        Invoke-RestMethod ... your parameters here ... 
    } catch {
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
        Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
    }
    

    Another option is to use the old invoke-webrequest cmdlet found here.

    Code copied from there is:

    $resp = try { Invoke-WebRequest ... } catch { $_.Exception.Response }
    

    Those are 2 ways of doing it which you can try.

提交回复
热议问题