11-21-2023 09:07 AM
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
}
11-21-2023 11:14 AM
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
11-22-2023 11:32 AM
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.
11-22-2023 12:01 PM
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).
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
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide