Set Tags for All Computers in a Group

This script will set the tag(s) for all computers in a group. It will override any tags that are currently assigned to any of those systems.

There are some areas in the code you’ll need to update to get the script to function:

  1. $orgID = 'YOUR_ORG_ID' - put your Org ID, which can be found by looking at the URL of your console and selecting the value after the “?o=”: https://console.automox.com/dashboard?o=999999 . In this example URL the Org ID is the 999999 portion.

  2. $apiKey = 'YOUR_API_KEY' - in your console, go to Settings->API and select the API key. Note that the API key is per admin user, so you and another admin in your console will have different API keys.

  3. $grpID = 'GROUP_ID_OF_SYSTEMS_TO_TAG' - put your Group ID of the systems you want to set the tags on. It can be found by viewing the group page that lists devices in that particular group and grabbing the value after “gid=” in the URL. One way to get to that page is to go to the System Mgmt page then find the group that contains the systems you want to have tagged and click the little computer icon with a number under it. In this example, the group ID is 99999: https://console.automox.com/group-editor?frompage=devices&gid=99999&o=1234.

  4. $tag = - Set the name for the tag you want on all of the systems. For multiple tags, you’ll just create multiple tag variables such as $tag2, $tag3, etc. For multiple tags, you’ll also need to modify the tag section in the body of the code so that it has the list of tags you want. For example, "tags": [ "$tag", "$tag2" ], or "tags": [ "$tag", "$tag2", "$tag3" ], etc…

# NOTE: This code will override any currently assigned tags on the computers in $grpID
#
#       All computers in the specified group will have their tags changed, so if you only
#       want certain computers modified, you'll want to put them all in a group where you
#       can modify them all to the same tag(s)

# Replace the variables below with your Org ID, API key, & server group systems are in to tag
$orgID = 'YOUR_ORG_ID'
$apiKey = 'YOUR_API_KEY'
$grpID = 'GROUP_ID_OF_SYSTEMS_TO_TAG'

######################################################################
# Define tag(s).
# If using two tags:
#    1) un-remark the $tag2 line and define it
#    2) modify the body at the bottom of the code to reflect two tags
######################################################################
$tag = 'Tag1'
#$tag2 = 'Tag2'

$apiInstance = 'https://console.automox.com/api/'
$apiTable = 'servers'
$orgAndKey = "?o=$orgID&groupId=$grpID&api_key=$apiKey"

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

#Get the json body of the Web Request
$jsonReturn = (Invoke-WebRequest -UseBasicParsing -Method Get -Uri $getURI).Content

#Convert to object with manipulatable properties/values
$servers = $jsonReturn | ConvertFrom-Json

#Re-tag each server in group
foreach ($server in $servers) {
    $serverID = $server.id
    $tagURI = $apiInstance + $apiTable + '/' + $serverID + '?o=' + $orgID
        
    #Tag server
    $headers = @{
        "Authorization" = "Bearer $apiKey"
        "Content-Type" = "application/json"
    }
    
    #########################################################################
    # If setting two tags, change tags line to "tags": [ "$tag", "$tag2" ],
    #########################################################################
    $body = @"
    {
        "server_group_id": $grpID,
        "tags": [ "$tag" ],
        "exception": false  
    }
"@

    Invoke-WebRequest -Method Put -Uri $tagURI -Headers $headers -Body $body
}