Skip to main content

I’ve just started looking at the API documentation, and it looks like if I don’t know the Device ID but only the name, the only way I can query the API is to return all devices and then filter my results.

The only filter option I can find in the documentation for the servers ‘is_compatible’.

Is getting all servers and then filtering the results the only way?


Thanks!

 

 

Hi there,

Depending on what you’re trying to do, are you trying to get data about a device, or push specific attribute data to devices?

Thanks,

Angelo


Hi Angelo,

At this time I'm trying to delete specific Servers from Automox via script as part of server decommissioning.

Currently my script is getting all devices, filtering the results by the device name and then deleting with the device ID.

Thanks,

Lincoln


Lincoln, 

I take it you’ve gotten this far.


 

# REQUIRED USER PARAMETERS

# Define your Automox API key
$apiKey = 'xxx-xxx-xxx-xxx-xxx'

# Define your Automox orgID
$orgID =

# Define host names to search for
$targetHostNames = @('hostname1', 'hostname2')

#########################################
# PREDEFINED PARAMETERS

$headers = @{ 'Authorization' = "Bearer $apiKey" }
$limit = 500
$page = 0
$endpoint = 'servers'
$baseUrl = "https://console.automox.com/api/${endpoint}"

function Invoke-AutomoxRestMethod {
CmdletBinding() ]
param (
Parameter ( Mandatory ) ] ) string ] $Uri,
Parameter ( Mandatory ) ] ) hashtable ] $Headers,
Parameter ( Mandatory ) ] ) ValidateSet( 'GET', 'POST', 'PUT', 'DELETE' ) ] ) string ] $Method,
Parameter () ] ) object ] $Body = $null,
Parameter () ] ) int ] $MaxRetries = 5
)

$retryCount = 0
$sleepSeconds = 1

while ( $retryCount -lt $MaxRetries ) {
try {
if ( $Method -in 'GET', 'DELETE' ) {
return Invoke-RestMethod -Uri $Uri -Headers $Headers -Method $Method -ErrorAction Stop
}

else {
# For PUT/POST requests, send as JSON
return Invoke-RestMethod -Uri $Uri -Headers $Headers -Method $Method -Body $Body -ContentType 'application/json' -ErrorAction Stop
}
} catch {
# Handle rate limiting (429 responses)
$resp = $_.Exception.Response
if ( $resp -and $resp.StatusCode -eq 429 ) {
Write-Warning "Rate limit (429) from API. Sleeping for $sleepSeconds second(s) (attempt $( $retryCount+1 ) of $MaxRetries) before retrying..."
Start-Sleep -Seconds $sleepSeconds
$sleepSeconds = s Math ]::Min( $sleepSeconds * 2, 300 ) # cap at 300 seconds / 5 minutes
$retryCount++
}

else {
Write-Error "Failed to invoke API A $Method $Uri ]: $( $_.Exception.Message )"
throw
}
}
}

Write-Error "Max retry attempts ( $MaxRetries ) exhausted for URL: $Uri"
throw "Max retry attempts ( $MaxRetries ) exhausted for URL: $Uri"
}

$requiredParameters = @{
apiKey = "A variable named 'apiKey' is required for this script. Please review the script's description for instructions on implementation."
orgID = "A variable named 'orgID' is required for this script. Please review the script's description for instructions on implementation."
}

foreach ( $param in $requiredParameters.GetEnumerator() ) {
$value = Get-Variable -Name $param.Key -ErrorAction SilentlyContinue
if ( f string ]::IsNullOrEmpty( $value ) ) {
Write-Error $param.Value
Stop-Transcript
exit 87
}
}

#########################################
# Begin API Call

Write-Verbose "Fetching all devices from Automox Organization ID: $orgID..."
$allDevices = @()

do {
$url = "https://console.automox.com/api/${endpoint}?o=${orgID}&page=${page}&limit=${limit}"

try {
# query device API
$devices = Invoke-AutomoxRestMethod -Uri $url -Headers $headers -Method GET
}

catch {
Write-Error "Failed to retrieve devices: $( $_.Exception.Message )"
exit 2
}

Write-Verbose "Page ${page}: $( $devices.Count ) devices retrieved from OrgID $orgID."
$allDevices += $devices

$page++
}
while ( $devices.Count -ge $limit )

#########################################
# Identify host name matches

# Gather devices that have a host name matching the defined list
$deviceList = $allDevices | Where-Object { $_.display_name -in $targetHostNames }

if ( $deviceList ) {
Write-Output "Found $( $deviceList.Count ) device(s) with a host name match"
$deviceList | select display_name, id
}

else {
Write-Output "No host name matches were found."
}


You can also add a delete function to the end of your code such as the following code excerpts. In my testing, either of the two will work to remove a device.
 

## 1 method - to perform delete

$deviceList | ForEach-Object {
$deleteUrl = "$baseUrl/$($_.id)"

try {
Invoke-AutomoxRestMethod -Uri $deleteUrl -Headers $headers -Method DELETE
Write-Output "Deleted: $($_.display_name) nID: $($_.id)]"
} catch {
Write-Warning "Failed to delete: $($_.display_name) nID: $($_.id)]"
}
}


### 2nd method - both achieve the same effect


foreach ($device in $deviceList) {
$deviceID = $device.id
$deleteUrl = "https://console.automox.com/api/servers/$deviceID"

try {
Invoke-AutomoxRestMethod -Uri $deleteUrl -Headers $headers -Method DELETE
Write-Output "Successfully deleted device: $($device.display_name) nID: $deviceID]"
} catch {
Write-Warning "Failed to delete device: $($device.display_name) nID: $deviceID]"
}
}


 

Have you also considered the worklet Windows - Maintenance Tasks - Cleanup Disconnected Automox Devices?

You can configure this to remove devices after a period of days. 

Let me know your thoughts.

- Angelo


Hi Angelo,

I missed your post somehow even though I'm subscribed to the topic.

 

I just found what I was looking for via this post:
 

 

What I was missing was:

 

Invoke-WebRequest -Uri "https://console.automox.com/api/servers?name=$ComputerName*&o=$orgID" -Headers $headers”

 

Your reply went into a lot of detail!  I’d already had the code written up and working to remove a device in Automox if found by returning the entire list of devices and then deleting the one that matched the hostname.

Our environment treats server names uniquely (pets, not cattle 🙄) so I didn’t worry too much about getting more than one result except to throw an error to check manually.


Ah, well that’s good to hear! We’ll be here to assist if you need anything else.

Best~


Reply