Server Detail Report - Example API PowerShell Script

  • 1 September 2020
  • 5 replies
  • 358 views

Here is an API PowerShell script to export your Device Name, Model, Vendor, and Serial Number into a csv.


If you have tried this, you may have found it can be tricky, as the model, vendor, and serial number are stored in a nested field named detail. This script provides the expanded “detail” column data for each device by name.


Example Code:


#Update these variables with your API Key and orgID.  This script expects the $expDir path to already exist.#
$apiKey = 'YOUR_API_KEY'
$orgID = 'YOUR_ORG'
$expDir = 'C:\Temp'
#############################################################################################################

#Pull server detail from API and export as csv to defined directory
$uri = "https://console.automox.com/api/servers?o=$orgID&api_key=$apiKey"
$responseJson = (Invoke-WebRequest -UseBasicParsing -Uri $uri).Content | ConvertFrom-Json
$Output = $responseJson | Select-Object NAME -ExpandProperty detail | Select-Object NAME, MODEL, VENDOR, SERIAL | Export-Csv -Path $expDir\detail.csv -NoTypeInformation -Force

If you would like to review all the information in the detail column along with the device name, you can change this section:


| Select-Object NAME, MODEL, VENDOR, SERIAL | Export-Csv -Path $expDir\detail.csv -NoTypeInformation -Force

to this:


| Select-Object * | Out-Gridview

This will output your information in an interactive PS table where you can filter and sort. I suggest using the Out-Grid view as some of the detail columns do export cleanly to a csv. From the PS table, you can sort and filter your data, then copy and paste into another file type of your choice.


Hope this helps!


5 replies

This is great, super helpful. How about to pull this same list but for a specific group? (Assuming we know the group ID)

Hello @agranados,

I had a “verbose” version of the following script posted that would filter by group. After talking to one of our software engineers, I have a better solution to share. You can add the groupID as a filter in your API call directly. This is good for a few reasons. It simplifies the PS code, and it accounts for the the API returning a maximum of 500 items per call. This will let you get all members of a group with 500 or less devices without adding additional context for number of pages or length.


Example Code:


#Update these variables with your API Key and orgID.  This script expects the $expDir path to already exist.#
$apiKey = 'YOUR_API_KEY'
$orgID = 'YOUR_ORG'
$grpID = "Your_GROUP_ID"
$expDir = 'C:\Temp'
#############################################################################################################

#Pull server detail by group from API and export as csv to defined directory
$uri = "https://console.automox.com/api/servers?o=$orgID&groupId=$grpID&api_key=$apiKey"
$responseJson = (Invoke-WebRequest -UseBasicParsing -Uri $uri).Content | ConvertFrom-Json
$Output = $responseJson | Select-Object Name -ExpandProperty detail | Select-Object Name, MODEL, VENDOR, SERIAL | Export-Csv -Path $expDir\detail.csv -NoTypeInformation -Force
Userlevel 3
Badge

Awesome script!


Is it possible to to pull the info you have provided as well as info like “tags” in the same csv export? Gave a shot a some attempts, but had no success getting tags to export in the same line as the “NAME, MODEL, VENDOR, SERIAL”


I should clarify, the grid view would not work for my specific use case I would be running this as a worklet on a machine and don’t want to manually select the grid options every time.

Tags are represented in an array, and they are at the same level as the device names. I only had a few minutes to review, so I have a quick way to get the data in gridview, but I didn’t have a chance to update the script to handle the array properly in a CSV export.


Here is an example:


#Update these variables with your API Key and orgID.  This script expects the $expDir path to already exist.#
$apiKey = 'YOUR_API_KEY'
$orgID = 'YOUR_ORG'
$grpID = "Your_GROUP_ID"
$expDir = 'C:\Temp'
#############################################################################################################

#Pull server detail by group from API and export as csv to defined directory
$uri = "https://console.automox.com/api/servers?o=$orgID&groupId=$grpID&api_key=$apiKey"
$responseJson = (Invoke-WebRequest -UseBasicParsing -Uri $uri).Content | ConvertFrom-Json
$Output = $responseJson | Select-Object Tags, Name -ExpandProperty detail | Select-Object Name, MODEL, VENDOR, SERIAL, Tags | Out-Gridview
Userlevel 3
Badge

Hi David,


Any chance you have taken a look at this since? I’ve been messing with this on and off for a bit now, but haven’t had any luck figuring it out.


Thanks,

Reply