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-
<https://ourinstance.service-

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/
"=" + $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.
$Reader = New-Object System.IO.StreamReader($
$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