Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Parsing results after calling PowerShell Invoke-RestMethod

$
0
0
Hi all,

Hoping someone can help as I am stuck on this for a couple of days now -

I am calling a REST api for a vendor's service using the Invoke-RestMethod
cmdlet. I was wondering :

- How can I retrieve the http status code (200, etc).
- if there was a way to get a COUNT of the elements returned if the query
is successful.

Here's a snippet of what I am trying -

$headers = @{ #Ask to send XML back so that it can be searched etc using
XPATH. Dont know how to do this using JSON.
Accept  = "application/xml"
}
Try
{
#build a URL like:
https://OURINSTANCe.service-now.com/api/now/table/sys_user?sysparm_query=email=ravi@gmail.com
<https://ourinstance.service-now.com/api/now/table/sys_user?sysparm_query=email=ravi@gmail.com>
#to
see if this user is present in the user table
$Result  = Invoke-RestMethod -Credential $SNowCreds -Headers $headers
-Method Get -Uri ("https://" + $SNowInstance + ".
service-now.com/api/now/table/" + $Table + "sysparm_query=" + $AttribName +
"=" + $AttribValue)
return $Result.response.result.sys_id
#***How do we get the response code to make sure that the http call went
thro OK? If it is not 200, how can I retrieve the message that is returned
saying "constraint violation" etc..



#***If the http call is successful, check if we have exactly one record
retrieved - how do I do this?



}
catch
{
$Result = $_.Exception.Response.GetResponseStream()
        $Reader = New-Object System.IO.StreamReader($result)
        $ResponseBody = $Reader.ReadToEnd();
$message = "Unable to run a query on table" + $Table + "`tattribute name: "
+ $AttribName + "`tattribute value: " + $AttribValue + "`t" +
$_.Exception.Message + "`t" + $ResponseBody
writelog -msg ("ERROR `t" + $message)
return ""
}


thanks,
-Ravi

Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>