Knowledge Base

cancel
Showing results for 
Search instead for 
Did you mean: 

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.

 

Table of Contents

 

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:

  • http
  • https 

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/"
The first leading character "/" should be omitted

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
  • PUT
  • DELETE

GET
POST with JSON

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

https://curl.haxx.se/windows/

Version history
Revision #:
17 of 17
Last update:
‎09-25-2019 09:56 AM
Updated by:
 
Labels (1)


Found this article helpful? Click the Thumbs Up button.
Have an additional comment? Post it below.
0 Kudos