GET device/group: PowerShell

This example script uses PowerShell to query the /device/group API endpoint and output the contents as JSON.This call requires a private API key and returns a list of device groups.

API Key Security: Avoid hardcoding API keys directly in your scripts for production applications. Instead, consider using environment variables or securely retrieving the keys from a server-side application.

Copy
PowerShell: Device Group
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$headers.Add("INTEGRATION-KEY", "<my_private_api_key>")
$response = Invoke-RestMethod '<Your_domain_for_APIs>/device/group' -Method 'GET' -Headers $headers
$response | ConvertTo-Json

Breakdown

$headers.Add("Accept", "application/json") Initializes a new dictionary object to store HTTP request headers.
$headers.Add("Accept", "application/json") Adds an ‘Accept’ header to the dictionary, indicating that the client expects the response in JSON format.
$headers.Add("INTEGRATION-KEY", "<my_private_api_key>") Authenticates the API call by adding the "INTEGRATION-KEY" header.
$response = Invoke-RestMethod '<Your_domain_for_APIs>/device/group' -Method 'GET' -Headers $headers Sends a GET request to the specified URL using the previously defined headers. The response from the server is stored in the $response variable.
$response | ConvertTo-Json The response object is piped to ConvertTo-Json, which converts it into a JSON-formatted string.

Script outputs

You can choose to convert the response to different formats and output the results to a file:

CLI XML $response | Export-Clixml -Path "output.xml"
CSV $response | Export-Csv -Path "output.csv"
HTML $response | ConvertTo-Html -As Table | Out-File -FilePath "output.html"
JSON $response | ConvertTo-Json | Out-File -FilePath "output.json"
Text $response | Out-File -FilePath "output.txt"
XML $response | ConvertTo-Xml | Out-File -FilePath "output.xml"

Error Handling

The script may be tweaked to gracefully handle any returned error codes.

Copy
PowerShell: Device Group | Error handling
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$headers.Add("INTEGRATION-KEY", "<my_private_api_key>")

try {
    $response = Invoke-RestMethod '<Your_domain_for_APIs>/device/group' -Method 'GET' -Headers $headers
    $response | ConvertTo-Json
    Write-Host "Success: (200) Request was successful."
}
catch {
    $statusCode = $_.Exception.Response.StatusCode.value__
    $statusDescription = $_.Exception.Response.StatusDescription

    switch ($statusCode) {
        200 { Write-Host "Success: ($statusCode) $statusDescription" }
        400 { Write-Host "Error: Invalid request ($statusCode) $statusDescription" }
        403 { Write-Host "Error: Invalid key ($statusCode) $statusDescription" }
        429 { Write-Host "Error: Too many requests ($statusCode) $statusDescription" }
        default { Write-Host "Error: An unexpected error occurred: ($statusCode) $statusDescription" }
    }
}

Breakdown

try The try block executes the Invoke-RestMethod command and convert the response to JSON
catch The catch block catches any exceptions thrown due to HTTP errors.
switch The switch statement handles known error codes with specific messages.
default The default case in the switch statement catches any other status code and treats it as an unexpected error, printing a generic error message.