How do I use REST API calls in a PowerShell script?
Overview
AppDynamics users can write their own monitoring extension scripts to add custom metrics to the set that AppDynamics already collects and reports to the Controller.
This article features resources (provided by AppDynamics) for Windows and Powershell (5.1) users who want to onboard REST API usage. It contains the necessary steps for adding custom metrics using a shell script.
- How do I create a monitoring extension?
- GET Request API Call
- POST Request with adding JSON Content
- POST Request with File Upload
- Parameters to be Customized
- Resources
How do I create a monitoring extension?
Found under Extensions and Custom Metrics, refer to Build a Monitoring Extension Using Scripts for the general steps to creating your own monitoring extension.
GET Request API Call
$controller = "YOUR_CONTROLLER_HOST"
$port = "YOUR_CONTROLLER_PORT"
$protocol = "YOUR_CONTROLLER_SCHEMA"
$account = "ACCOUNT_NAME"
$user = "USER_NAME"
$password = "PASSWORD"
$controllerEndpoint = "YOUR_REST_API_URL"
$restURL = "${protocol}://${controller}:${port}/${controllerEndpoint}"
$headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${user}@${account}:${password}"))}
$response = Invoke-WebRequest -Uri $restURL -Headers $headers
$response.content
POST Request with adding JSON content
$JSON = @'
{
"key1":"value1",
"key2":"value2",
"key3":[
{"key31":"value31"},
{"key32":"value32"},
{"key33":"value33"}
]
}
'@
$controller = "YOUR_CONTROLLER_HOST"
$port = "YOUR_CONTROLLER_PORT"
$protocol = "YOUR_CONTROLLER_SCHEMA"
$account = "ACCOUNT_NAME"
$user = "USER_NAME"
$password = "PASSWORD"
$controllerEndpoint = "YOUR_REST_API_URL"
$restURL = "${protocol}://${controller}:${port}/${controllerEndpoint}"
$headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("username@account:password"))}
$response = Invoke-RestMethod -Uri $restURL -Method Post -Headers $headers -Body $JSON -ContentType "application/json"
$response.content
POST Request with file upload
$controller = "YOUR_CONTROLLER_HOST"
$port = "YOUR_CONTROLLER_PORT"
$protocol = "YOUR_CONTROLLER_SCHEMA"
$account = "ACCOUNT_NAME"
$user = "USER_NAME"
$password = "PASSWORD"
$controllerEndpoint = "YOUR_REST_API_IMPORT_URL"
$restURL = "${protocol}://${controller}:${port}/${controllerEndpoint}"
$headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("username@account:password"))}
$filePath = "YOUR_FILE_TO_BE_IMPORTED";
$fileBin=[System.IO.File]::ReadAllBytes(".\" + $filePath)
$CODEPAGE="UTF-8"
$enc = [System.Text.Encoding]::GetEncoding($CODEPAGE)
$fileEnc = $enc.GetString($fileBin)
$boundary = [System.Guid]::NewGuid().ToString()
$LF = "`r`n"
$bodyLines = (
"--$boundary",
"Content-Disposition: form-data; name=`"file`"; filename=`"Import.xml`"",
"Content-Type: application/octet-stream$LF",
$fileEnc,"--$boundary--$LF") -join $LF
$response = Invoke-RestMethod -Uri $restURL -Method Post -Headers $headers -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines
$response.content
Parameters for customization
controller |
Controller host name |
GET POST with JSON POST with file upload |
port |
Controller port name |
GET POST with JSON POST with file upload |
protocol |
Controller schema type:
|
GET POST with JSON POST with file upload |
account |
Name of account in controller Single-tenant hosts use "customer1" |
GET POST with JSON POST with file upload |
user |
The username of your user with sufficient permissions to access target API |
GET POST with JSON POST with file upload |
password |
The password of your user with sufficient permissions to access target API |
GET POST with JSON POST with file upload |
controllerEndpoint |
Target Controller's REST API to be accessed Usually, it starts with "controller/" |
GET POST with JSON POST with file upload |
restURL |
Full URL for API to be used for file import, e.g., $restURL = "http://controller:port/controller/ transactiondetection/<app_id>/custom" |
GET POST with JSON POST with file upload |
headers |
Section to be modified to replace dummy credentials (username@account:password) with the user's real credentials |
GET POST with JSON POST with file upload |
methodName |
Rest API Method name
|
GET POST with file upload |
filePath |
Full valid path to the file to be imported on the Windows host e.g., $filePath = "C:/Import.xml"; |
POST with file upload |
Resources
To learn about extending AppDynamics with APIs, see:
https://docs.appdynamics.com/display/PRO45/AppDynamics+APIs
For more information about Microsoft PowerShell, see:
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/powershell
- Mark as Read
- Mark as New
- Bookmark Message
- Permalink
- Report Inappropriate Content
The "GET Request API Call" section has an error!
The line that reads:
$headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${user}@${account}:${password"}))}
Needs to be changed to:
$headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${user}@${account}:${password}"))}
The last double quote is in the wrong position
- Mark as Read
- Mark as New
- Bookmark Message
- Permalink
- Report Inappropriate Content
Hi, @Keith.Sanders
Thank you! I've corrected the error you pointed out.
On behalf of everyone in the Community, thanks for letting us know!
Claudia Landivar
Community Manager & Editor