Delete Multiple Users From An Organization

  • 8 July 2021
  • 1 reply
  • 182 views

Userlevel 5

This API script will delete multiple users from an organization based on the users’ email address. It will then log the successes and failures in a location you specify.


Just set the following:


$apiKey = 'YOUR_API_KEY' - in your console, click on the three dots in the upper right, select KEYS, then copy your API key. Note that the API key is per admin user, so you and another admin in your console will have different API keys.


$orgID = 'YOUR_ORG_ID' - put your Org ID which can be found by looking at the URL from any Automox page and selecting the value after the “?o=”: [https://console.automox.com/device-detail?s=99999&o=1234 ]. In this example the Org ID is 1234.


$logPath - set where you want the logs saved


$toDelete - set your list of users you want deleted by their email addresses


### Replace the variables in this block with your Org ID & API key ###
$orgID = 'YOUR_ORG_ID'
$apiKey = 'YOUR_API_KEY'

# Modify log path as desired
$logPath = 'C:\Temp\'

# List of users identified by email to delete
$toDelete = @('user@email.com','user2@email.com','user3@email.com')

######################################################################

$page = 0
$limit = 500
$headers = @{ "Authorization" = "Bearer $apiKey" }

# Easier to maintain, especially if multiple organizations, or for repurposing for different API Tables
$apiInstance = 'https://console.automox.com/api/'
$apiTable = 'users'

# Initialize empty arrays to store server IDs
$users = @()
$delList = @()
$failDelete = @()
$successDelete = @()

# Generate list of all users
while($true) {

$orgAndKey = "?o=$orgID&api_key=$apiKey&l=$limit&p=$page"

#put components together
$uri = $apiInstance + $apiTable + $orgAndKey

#Get the json body of the Web Request
$resp = (Invoke-WebRequest -Method GET -Uri $uri -UseBasicParsing).Content | ConvertFrom-Json

$users += $resp
$page += 1

if($resp.count -lt $limit) {
break
}
}

# Narrow down to list of users that need to be deleted
ForEach ($user in $toDelete) {

$match = $users | Where-Object {$_.email -EQ $user}
$delList += $match
}

# Delete designated users. Log whether successful or failed to delete.
ForEach ($delUser in $delList) {
try {
$id = $delUser.id
$email = $delUser.email
$name = $delUser.firstname+' '+$delUser.lastname
$url = "https://console.automox.com/api/users/$id"+"?o=$orgID"
Invoke-WebRequest -Method Delete -Uri $url -Headers $headers
$successDelete += @{"Email"=$email;"ID"=$id;"Name"=$name}
Write-Output "Successfully Deleted User: $email"
}
catch {
$failDelete += @{"Email"=$email;"ID"=$id;"Name"=$name}
Write-Output "Failed to Delete User: $email"
}
}

# Output logging into json files for later review/manipulation
$successDelete | ConvertTo-Json | Out-File $logPath\Delete_Success.json
$failDelete | ConvertTo-Json | Out-File $logPath\Delete_Failed.json

1 reply

Hi Tony, 

 

Thanks for the script. Trying to do the opposite, adding users to the console. what do I need to change? 

Reply