Set Tags for All Computers in a Group

  • 13 October 2020
  • 4 replies
  • 511 views

Userlevel 5

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
}

4 replies

Userlevel 3
Badge

Just a note on this, applying tags from API will replace any existing tags. I wanted something to append to existing tags so I wrote a little snippet that formats for the body. I’ll post a fuller example of the code once I finish splicing it all together. I am using the API to pull all the devices into $items and for each $item.


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

#how to append tags to existing tags in automox

#$item is automox device info from api


$newtags = “tag1”,“tag2”,“tag3”

$tags = $item.tags + $newtags

$tags = $tags -join ‘","’

$tags = ‘"’ + $tags + ‘"’

Hey Tony,

Great script, I’ve been looking for a method for applying tags in bulk. I’ve made a few adjustments for my specific need, and I’m posting it in case it helps out others. I wanted to be able to apply multiple tags to machines of a specific OS version, while maintaining any existing tags, and removing duplicates.


# Originally sourced from https://community.automox.com/t/set-tags-for-all-computers-in-a-group/1391

$orgID = 'YOUR_ORG_ID'
$apiKey = 'YOUR_API_KEY'
$grpID = 'GROUP_ID_OF_SYSTEMS_TO_TAG'

#########################################################################
# If setting two tags, change tags line to $tags = "tag1", "tag2"
#########################################################################
$tags = "20H2", "Current"
$tags = '"' + (($tags -split "," | select -Unique) -join '","') + '"'

# Specify OS version to target
# 10.0.19042 = 20H2
$osVersion = '10.0.19042'

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

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


$headers = @{ "Authorization" = "Bearer $apiKey" }

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

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

foreach ($server in $servers) {

# Filter instances to only those of the targeted OS version
if($server.os_version -eq "$osVersion") {
$serverID = $server.id
$tagURI = $apiInstance + $apiTable + '/' + $serverID + '?o=' + $orgID

# Combine existing tags with new tags
$applyTags = ('"' + ($server.tags -join '","') + '"' + ",$tags")

# Remove duplicate tags
$applyTags = ($applyTags -split "," | select -Unique) -join ','

#Tag server
$headers = @{
"Authorization" = "Bearer $apiKey"
"Content-Type" = "application/json"
}

$body = @"
{
"server_group_id": "$grpID",
"tags": [ $applyTags ],
"exception": false
}
"@

Invoke-WebRequest -Method Put -Uri $tagURI -Headers $headers -Body $body | Out-Null

}

}
Userlevel 3
Badge

Awesome work. You beat me to it. Been a busy week at work and haven’t had a chance to circle back. I think this will help a lot of people.

Found and issue where blank tags were applied. I’ve got a fix, kind of hacky, but it works for now. Maybe a real dev can clean it up. I couldn’t figure out how to edit the prior post, so I’m going to add a snippet here. Replace the script starting with "foreach ($server in $servers) { "


foreach ($server in $servers) {
$applyTags = @()
if($server.os_version -eq "$osVersion") {
$serverID = $server.id
$tagURI = $apiInstance + $apiTable + '/' + $serverID + '?o=' + $orgID

# Combine existing tags with new tags
$tempTags = ('"' + ($server.tags -join '","') + '"' ) + ",$tags"

# Remove duplicate tags
$tempTags = ($tempTags -split "," | Select-Object -Unique) -join ','

# Remove any empty tags
foreach ($tag in ($tempTags -split ",")) {

if($tag -ne '""') {
$applyTags = $applyTags + $tag
}
}

$applyTags = ($applyTags -split "," | Select-Object -Unique) -join ','

#Tag server
$headers = @{
"Authorization" = "Bearer $apiKey"
"Content-Type" = "application/json"
}

$body = @"
{
"server_group_id": "$grpID",
"tags": [ $applyTags ],
"exception": false
}

"@


    Invoke-WebRequest -Method Put -Uri $tagURI -Headers $headers -Body $body | Out-Null

}

}

Reply