Follow

How can I access the API with a PowerShell script using the Invoke-WebRequest command? | API FAQ

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 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.

$customerID ="12345"

$user = "01234abcde01234abcde01234abcde01234abcde01234abcde"

$RemoteHost = "https://publicapi.alertlogic.com/api/lm/v1/12345/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

Was this article helpful?
1 out of 1 found this helpful
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.