Question

Output from API GET command "List All Devices"

  • 22 March 2023
  • 1 reply
  • 140 views

Badge

I am running the “List All Devices” API GET command in a PowerShell script that looks like this:

$apiKey = '<MyAPIKey>'
$headers = @{ "Authorization" = "Bearer $apiKey" }
$url = "https://console.automox.com/api/servers?o=106231&page=0&limit=500"
$response = (Invoke-WebRequest -Method Get -Uri $url -Headers $headers).Content

I am running the script from a powershell window as: .\ListAllDevices.ps1

It completes, but I have no idea where the output goes.

I would like to have the data dump to a .csv file on my desktop.  How do I make that happen?

 

 


1 reply

Userlevel 3

Hi @randygarland,

 

You are pretty close with this one!

 

To output the contents of your API call, you’ll just need to call the $response variable again.  This will return the ‘Content’ of the Invoke-Webrequest cmdlet inside your Powershell terminal.

 

Note, the contents of the variable will be in JSON format so I recommend using the ConvertFrom-Json cmdlet to make the data coherent:

$response = (Invoke-WebRequest -Method Get -Uri $url -Headers $headers).Content | ConvertFrom-Json

 

If you are looking to dump contents to your desktop, you can pipe the $response variable to the Export-CSV cmdlet, with the destination -Path being the UNC to your desktop folder. 

 

Tip: Using the $env:username variable is a quick way to pull the user name of the person running the script:

$response | Export-Csv -Path C:\Users\$env:username\Desktop\csv-output.csv -NoTypeInformation -Force

 

If we put it all together we get something like this:

$apiKey = 'xxxx-xxxx-xxxx-xxxx-xxxx'
$orgID = 'xxxx'
$headers = @{ "Authorization" = "Bearer $apiKey" }
$url = "https://console.automox.com/api/servers?o=$orgID&page=0&limit=500"
$response = (Invoke-WebRequest -Method Get -Uri $url -Headers $headers).Content | ConvertFrom-Json
$response | Export-Csv -Path C:\Users\$env:username\Desktop\csv-output.csv -NoTypeInformation -Force

 

More on getDevices API call can be found here.

 

I’d also recommend checking out this post for retrieving a hardware inventory from your Automox console. This script includes pagination which is a nice touch.

 

Have a great day!

Reply