cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
487
Views
0
Helpful
3
Replies

Secure Endpoint API Error

DMontalbano
Level 1
Level 1

I have been using the Powershell code below to access Secure Endpoint to do maintenance tasks for a couple of years now.  Removing dups, exporting computers...etc.  As of a few days ago I am getting a "Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error" when it tries to run the Invoke-RestMethod command below.  Has anything changed in accessing using the API?

Function Computerlist {
$list=$null
$url="https://api.amp.cisco.com/v1/computers"
Log "Retrieving Computers list"
$list = Invoke-RestMethod -Method Get -Uri $url -Headers $Headers -ErrorVariable RestError -ErrorAction SilentlyContinue
if ($RestError){
$HttpStatusCode = $RestError.ErrorRecord.Exception.Response.StatusCode.value__
$HttpStatusDescription = $RestError.ErrorRecord.Exception.Response.StatusDescription
Log " - Unable to retrieve the computer list"
Log " - Http Status Code: $($HttpStatusCode) - Http Status Description: $($HttpStatusDescription)"
Log "Exiting the script"
Exit
}

3 Replies 3

Matthew Franks
Cisco Employee
Cisco Employee

I just ran an API call to the /v1/computers endpoint with Powershell and received results with no issues.  As far as I know, nothing has changed that would break this API call. I modified it slightly to show a count if it succeeds and have the auth and headers above this code block.

$list=$null
$url="https://api.amp.cisco.com/v1/computers"
Write-Host "Retrieving Computers list"
$list = Invoke-RestMethod -Method Get -Uri $url -Headers $Headers -ErrorVariable RestError -ErrorAction SilentlyContinue
if ($RestError){
    Write-Host $RestError
    $HttpStatusCode = $RestError.ErrorRecord.Exception.Response.StatusCode.value__
    $HttpStatusDescription = $RestError.ErrorRecord.Exception.Response.StatusDescription
    Write-Host " - Unable to retrieve the computer list"
    Write-Host " - Http Status Code: $($HttpStatusCode) - Http Status Description: $($HttpStatusDescription)"
    Write-Host "Exiting the script"
    Exit
}
else {
    Write-Host $list.data.count
}

Here are the results.

.\test.ps1
Retrieving Computers list
9

Thanks,

-Matt

Thanks Matt.  It's very inconsistent.  It ran once today with no issues.  Ran it again and it crashed after reading 2500 records.  Other times it crashes and didn't read any records.  I'll keep troubleshooting.

Matthew Franks
Cisco Employee
Cisco Employee

Seems like you might be hitting a rate limit.  The response headers should show how many requests you have remaining and the time until the count resets.

https://developer.cisco.com/docs/secure-endpoint/#!overview/rate-limits

Here is a screenshot of my headers (from a request in Postman).

MatthewFranks_0-1700683198321.png

You should also get a 429 response if you hit the rate limit so you may need to add handling for that scenario in your script so it can sleep until the counter resets and then try again.

Hope that helps.

-Matt