Add a smart computer group to another smart group via script

SMR1
Contributor III

Here is what I'm trying to do. When we do 3rd party patching, we have to do them in phases, alpha, beta and uat. We currently have 10 applications that we update monthly. For each application smart group, we have to add the group manually to each one. For example, this is what it would look like. On day one we add Alpha group and the next we add Beta group. I've messing with different scripts, but keep getting 404 error. It would be nice to run a script that would add the groups instead of going in to each one. Not sure if this even possible.

Here is what our update groups look like.

SMR1_0-1691769608639.png

This is one of the scripts I was trying.

#!/bin/sh

# API login info
apiuser="xxx"
apipass="xxx"
jamfProURL="xxx"

# Smart group ID
SmartGroupID="120"
SmartGroupAPIURL="JSSResource/computergroups/id/${SmartGroupID}"

# XML header stuff
xmlHeader="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"

# Get the current smart group criteria
currentCriteria=$(curl -sSkiu ${apiuser}:${apipass} "${jamfProURL}/${SmartGroupAPIURL}" \
-H "Accept: application/xml" \
-X GET)

# Extract the current criteria priority
priority=$(echo "${currentCriteria}" | xmllint --xpath '/computer_group/smart_group_criteria/and/criterion/priority/text()' -)

# Increment the priority by 1
newPriority=$((priority+1))

# XML data to add "Smart Group" criteria with value "120" to smart group
apiData="<computer_group><id>${SmartGroupID}</id><name>Whatever the SmartGroupName Is</name><smart_group_criteria><and><criterion><name>Smart Group</name><priority>${newPriority}</priority><and_or>and</and_or><search_type>is</search_type><value>${SmartGroupID}</value></criterion></and></smart_group_criteria></computer_group>"

curl -sSkiu ${apiuser}:${apipass} "${jamfProURL}/${SmartGroupAPIURL}" \
-H "Content-Type: text/xml" \
-d "${xmlHeader}${apiData}" \
-X PUT > /dev/null

 

2 REPLIES 2

mm2270
Legendary Contributor III

@SMR1 I think some of the tags in your xml data is wrong. Can you check them? I don't see such a tag called <smart_group_criteria> in my smart computer groups. But your xml data has that tag in it.

Also, in examining your data, I don't see that you're adding the additional computer group in there. I only see one group included. I think to have both groups, the final xml data needs to have both of them present.

SMR1
Contributor III

Looks like I posted the wrong script. Not all that great with scripting, was using AI to assist with it. The script I'm running, is getting a 404 error trying to reach the server.