Skip to content
Notifications
Clear all

Check out my script to auto-tag devices based on AD group membership.

3 Posts
3 Users
0 Reactions
71 Views
(@calebs)
Reputable Member
Joined: 4 months ago
Posts: 318
Topic starter   [#20371]

Managing GravityZone endpoints in a dynamic AD environment is a pain. Static tags don't scale. I wrote a script to auto-tag devices based on their computer object's AD group membership.

It runs on a schedule, queries AD for a mapping of computer names to groups, then uses the GravityZone API to ensure each endpoint has the corresponding tag. Keeps inventory organized for policies and reports.

Requirements: `jq`, `curl`, and an API key with 'Manage Network Inventory' permission.

```bash
#!/bin/bash
# Set your GZ API URL and key
GZ_BASE_URL="https://your-gz-server.com"
API_KEY="your_api_key_here"

# Define your AD group to GZ tag mapping
declare -A TAG_MAP=( ["AD-Group-Servers"]="Server" ["AD-Group-Workstations"]="Workstation" )

# AD Query to get computer group membership (using ldapsearch from ldap-utils)
# Returns format: computerName:group1,group2
COMP_GROUPS=$(ldapsearch -x -H ldap://your-domain-controller -b "DC=yourdomain,DC=com" "(&(objectCategory=computer)(memberOf=*))" name memberOf | parse_to_simple_format) # Replace with actual query/parsing

echo "$COMP_GROUPS" | while IFS=: read -r comp groups; do
for adgroup in ${groups//,/ }; do
if [[ -v TAG_MAP["$adgroup"] ]]; then
TAG_NAME="${TAG_MAP[$adgroup]}"
# Get endpoint ID by computer name
ENDPOINT_ID=$(curl -s -X GET "$GZ_BASE_URL/api/v1/endpoints?computerName=$comp" -H "Authorization: $API_KEY" | jq -r '.[].id')
# Apply the tag via API
curl -s -X POST "$GZ_BASE_URL/api/v1/endpoints/$ENDPOINT_ID/tags" -H "Authorization: $API_KEY" -H "Content-Type: application/json" -d "{"tagName": "$TAG_NAME"}"
fi
done
done
```
You need to flesh out the AD query parsing. The API calls are the core. This eliminates manual tagging drift.

-c



   
Quote
(@harpera)
Estimable Member
Joined: 4 months ago
Posts: 214
 

That's a solid approach for a batch synchronization model. I've built similar systems for auto-provisioning in SaaS platforms. You'll want to incorporate some form of delta detection to avoid hammering the API with redundant requests on every run. Grabbing the current tags for each endpoint first, comparing them to the computed desired state, and only making the API call when they differ will significantly reduce your API call volume and prevent hitting any potential rate limits.

Also, consider the failure mode when a computer object is removed from an AD group but remains in GravityZone. Your current loop only adds tags based on the `TAG_MAP`. You might want to implement logic to also remove tags that are no longer applicable, which would require tracking all tags managed by this script for a given endpoint. Otherwise, you'll have drift over time. A simple method is to treat the script as authoritative for a defined set of tags; for each endpoint, you clear all tags in the managed set and then re-add the ones currently derived from AD.


— Harper


   
ReplyQuote
(@cloud_cost_hawk)
Reputable Member
Joined: 5 months ago
Posts: 250
 

Exactly right on delta detection, but the API cost is the real killer. Even with free tiers, unnecessary calls are a silent budget drain. You don't want your finance team flagging your service account's API usage.

> treat the script as authoritative for a defined set of tags

This is the only sane pattern. I'd extend it: maintain a local cache (a simple JSON file) of the last applied state. On each run, diff the computed desired state against that cache, not the live API. Only hit the API for the delta, and update the cache on success. That way you're also resilient to temporary API failures.

For removal, your authoritative set method works, but a `replace` API call if available is cleaner than separate remove/add calls.


cost optimization, not cost cutting


   
ReplyQuote