The purpose of our Developer APIs is to allow customers to integrate our products into their own. In order to access the API with a PowerShell script using the Invoke-WebRequest command, run the command below.
This example assumes a Customer ID (CID) of 12345, as well as the Denver data center API endpoint. It fetches a list of all sources from the Log Manager API.
The username and the password string are for creating a basic HTTP authentication header. It will convert the username (API Key) and the password (blank) into a string separated by a colon, and base64-encodes it all. For our Cloud Defender APIs, the username is equivalent to your API key and the password should be a blank value.
The way that this information is passed to the Cloud Defender API is via an HTTP Header. The header requires the username and password to be separated by a colon. This entire string of text is then base64-encoded. This is a way of representing text that is a normal part of Basic HTTP Authentication.
The end of the process converts the authentication header from JSON into a PowerShell object and then returns it to JSON to ensure a clean PowerShell console response. Use the example script below to guide you.
Note: For the remote host below, the base URL will change depending on the data center you are connecting to.
Base URL |
Description |
https://publicapi.alertlogic.net |
Use this URL when accessing the US datacenter. |
https://publicapi.alertlogic.com |
Use this URL when accessing the Ashburn datacenter. |
https://publicapi.alertlogic.co.uk |
Use this URL when accessing the UK datacenter. |
$customerID ="12345"
$user = "01234abcde01234abcde01234abcde01234abcde01234abcde"
$RemoteHost = "https://publicapi.alertlogic.com/api/lm/v1/$customerID/sources"
$pass = ""
$pair = "${user}:${pass}"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"
$headers = @{ Accept= "application/json"
Authorization = $basicAuthValue
"Content-Length"= 0 }
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -uri $RemoteHost -Headers $headers |
ConvertFrom-Json | ConvertTo-Json
Comments
3 comments
For some Powershell users consider using...
$headers = @{}
$headers.Add("Accept","application/json" )
$headers.Add("Authorization",$basicAuthValue)
...technique for adding headers individually. the example as provided is potentially flawed, with PS not recognizing the reserved word Authorization as a valid value for the array.
Adding them separately worked for me.
Thanks,
EC
Thanks for sharing this, Evan! We're going to run it by our technical support team to see if it's valuable to add into the article. Your feedback is much appreciated.
In case you would like to see the entire response, please consider to add -Depth to the last command in the pipe:
Invoke-WebRequest -uri $RemoteHost -Headers &headers | ConvertFrom-Json | ConvertTo-JSON -Depth 5
Please sign in to leave a comment.