Using PowerShell for Aria OPS API
In a previous article, I wrote about accessing the Aria OPS Chargeback API. In that article, I used Postman for testing the API calls. I also wrote that I would convert those Postman API calls into a Powershell script. So here goes…
The steps to generate a bill and to retrieve the information from that bill are the same:
- Log in and get a token
- Generate a bill
- Retrieve the information from that bill
Login and get a token
# Define API URL
$apiUrl = "https://$vrops/suite-api/api/auth/token/acquire"
# Define login credentials
$body = @{
username = $username
password = $password
authSource = "WorkspaceONE" # For me this is WorkspaceONE
} | ConvertTo-Json
# Define HTTP headers
$headers = @{
"Content-Type" = "application/json"
}
# Make the POST request to log in
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body
# Extract the token from the response
$token = $response.token
# Display the token
Write-Output "Token: $token"
# Optionally store the token for future requests
$authHeaders = @{
"Authorization" = "OpsToken $token"
}
You now have a token that can be used in the subsequent API calls.
Generate a bill
# Define API URL
$apiUrl = "https://$vrops/suite-api/api/chargeback/bills"
# Define the JSON body as a PowerShell hashtable
$body = @{
title = "Test Bill Generation"
description = "Test Bill Generation"
billingStartTime = 1665122400000
billingEndTime = 1666322044280
resourceIds = @("dfc2d007-be10-45c7-9fef-bd2f77b274ff")
policyId = "5e3d8255-1c5e-48dd-92c5-b7a687c3c898"
timeZone = "UTC"
} | ConvertTo-Json -Depth 3 # -Depth ensures nested objects/arrays are serialized properly
# Define HTTP headers
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "OpsToken $token" # Replace with your actual token if required
}
# Make the POST request
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body
# Output the API response
Write-Output $response
Information about billingStartTime, billingEndTime, and both IDs can be found in the referenced article.
Get the bill
# Define API URL
$apiUrl = "https://$vrops/suite-api/api/chargeback/bills/$id"
# Define HTTP headers
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "OpsToken $token" # Replace with your actual token if required
}
# Make the POST request
$response = Invoke-RestMethod -Uri $apiUrl -Method Get -Headers $headers
# Output the API response
Write-Output $response
Of course, you would need some additional code to retrieve some IDs and some code for reporting and other stuff. But you get the idea…