cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
4016
Views
20
Helpful
2
Replies

Some handy tips for AMP4E API connections with PowerShell

ChiefSec-SF
Beginner
Beginner

Thought I would share some things I found in case it saves someone else some time. The API documentation is good, but all in cURL and some of the items can be a little bit misleading when coming at it from a Windows Admin background. (I am not going to cover creating your API key)

First off, in the Documentation they state:

Using the API Key and Client ID

Once you have the API client ID and API key, you can make the API calls as follows:

https://<your_client_id>:<your_api_key>@<api_endpoint>

So this is basically not right at all. If you are in the US region the endpoint url you want is just "https://api.amp.cisco.com"

The client id and api key get sent in the header section.

The method that I found worked the best was to use Invoke-RestMethod

The tricky part is how to get the formatting of the header section correct. (The required header entries are covered in the documentation but not the formatting)

Here is what I found from some other online postings and modified for my purposes:

####

$clientid = "xxxxxxxx"
$apikey = "xxxxxxxxxxx"
$APIendpoint = "https://api.amp.cisco.com"

#$Credential = Get-Credential
# Much better to use get-credential and not leave your key sitting in a script, but I used this for a simple example

$EncodedUsernamePassword = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($('{0}:{1}' -f $clientid, $apikey)))
$Headers = @{'Authorization' = "Basic $($EncodedUsernamePassword)"; 'accept' = 'application/json'; 'Content-type' = 'application/json'; 'Accept-Encoding' = 'gzip, deflate'}

####

This shows you the correct formatting, you can then run one of the example queries:

####

$comp = "SomeComputerName"

$filter = ("/v1/computers?hostname[]=" + $comp + "&limit=10")
$url = ($APIendpoint + $filter)    

 #Those three could all be one string, but I set it up this way to make it easier to drop in a foreach block later
$query = Invoke-RestMethod -Method Get -Uri $url -Headers $Headers

####

$query.data.guid would then be the guid of the returned computer object for an example

Once you have the headers part down any of the Get requests are then pretty simple to do.

If you want to write a change, you must change to the Patch method, but here we run into another formatting issue no covered in the documentation.

Initially I tried this to get a computer moved to a new group:

####

$body = @{"group_guid" = "xxxxxxxxxxxxx"}

$url1 = ($APIendpoint + "/v1/computers/" + $compGUID)
$move = Invoke-RestMethod -Method Patch -Uri $url1 -Headers $Headers -Body $body

####

This fails as the server rejects it as a bad request. What you have to do for the body portion is convert it the the json format, like this:

####

$body = @{"group_guid" = "xxxxxxxxxxxxx"}

$body = $body | ConvertTo-json

$url1 = ($APIendpoint + "/v1/computers/" + $compGUID)
$move = Invoke-RestMethod -Method Patch -Uri $url1 -Headers $Headers -Body $body

####

Then it works like magic and you are up a running! 

2 Replies 2

Ken Stieers
VIP Advisor VIP Advisor
VIP Advisor

####

$body = @{"group_guid" = "xxxxxxxxxxxxx"}

$url1 = ($APIendpoint + "/v1/computers/" + $compGUID)
$move = Invoke-RestMethod -Method Patch -Uri $url1 -Headers $Headers -Body $body

####

This fails as the server rejects it as a bad request. What you have to do for the body portion is convert it the the json format, like this:

####

$body = @{"group_guid" = "xxxxxxxxxxxxx"}

$body = $body | ConvertTo-json

$url1 = ($APIendpoint + "/v1/computers/" + $compGUID)
$move = Invoke-RestMethod -Method Patch -Uri $url1 -Headers $Headers -Body $body

####

 

Then it works like magic and you are up a running! 

 


OMG Thanks so much!!!! I've been beating my head against that for a couple of days, trying to get Powershell to give me enough information to be useful, and I haven't been doing much in Powershell or with APIs as yet, so the epiphany of "maybe it wants json" didn't hit... 

justin.cooksey
Beginner
Beginner

404 Not found on https://api.apjc.amp.cisco.com

I'm getting 404 Not Found for just about any request I try.

If I alter the authorization I do get Unauthorized, so that makes sense, but if I try some of the APIs with Postman I always get 404 Not Found.

Some that Ive tried:

https://api.apjc.amp.cisco.com/v1/version

https://api.apjc.amp.cisco.com/v1/computer

https://api.apjc.amp.cisco.com/v0/version

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: