Posted on 03-12-2018 02:00 AM
We need to use API to identify if a computer is a member of one of five existing Static Computer Groups (StCG), on the client side, using an API script. The value is used locally to feed other non-JSS related workflows.
The StCG names would look like these:
We typically use UUID to find the computer, and this API call seems to work:
staticComputerGroup=$( /usr/bin/curl -s -k -u ${apiUser}:${apiPass} ${jssURL}/JSSResource/computers/udid/${uuid} | /usr/bin/xpath '/computer/groups_accounts/computer_group_memberships[1]' 2>/dev/null | /usr/bin/sed -e 's/<group>//g;s/</group>/++/g' | /usr/bin/tr '++' '
' | /usr/bin/sed '/^$/d' | /usr/bin/grep MyGroup )
After wrestling with this for a couple hours, I wonder if the above is as convoluted as it seems. I created a few other API scripts that were easier and less of an eye sore:
assignedUser=$( /usr/bin/curl -s -k -u ${apiUser}:${apiPass} ${jssURL}/JSSResource/computers/udid/${uuid} | /usr/bin/xpath '/computer/location/username/text()' 2>/dev/null )
mgmtAccount=$( /usr/bin/curl -s -k -u ${apiUser}:${apiPass} ${jssURL}/JSSResource/computers/udid/${uuid} | /usr/bin/xpath '/computer/general/remote_management/management_username/text()' 2>/dev/null )
siteName=$( /usr/bin/curl -s -k -u ${apiUser}:${apiPass} ${jssURL}/JSSResource/computers/udid/${udid} | /usr/bin/xpath '/computer/general/site/name/text()' 2>/dev/null )
I'm ok with leaving the first API command the way it is, if necessary, but was hoping maybe I'm missing an easier way to do it.
Solved! Go to Solution.
Posted on 03-12-2018 09:12 AM
You can use the same /text()
syntax for the groups. You have to use 2>&1
to ensure that the output remains as it would be seen in Terminal so a grep will work on it.
So for example:
staticComputerGroup=$( /usr/bin/curl -s -k -u ${apiUser}:${apiPass} ${jssURL}/JSSResource/computers/udid/${uuid}/subset/groups_accounts | /usr/bin/xpath '/computer/groups_accounts/computer_group_memberships/group/text()' 2>&1 | sed 's/-- NODE --//g' | grep "MyGroup" )
I also threw in a subset to the call so it only gets more specific data back.
Incidentally, if the groups happen to not all have the same string in their names, you can use egrep
to look for all groups in one shot, like so
... | /usr/bin/egrep "MyGroup_01|MyGroup_02|MyGroup_03|MyGroup_04|MyGroup_05"
That should print back any results it finds, so, for example, say the computer is only part of group 1, 2,3 and 5, it would send back a result like
MyGroup_01
MyGroup_02
MyGroup_03
MyGroup_05
Posted on 03-12-2018 09:12 AM
You can use the same /text()
syntax for the groups. You have to use 2>&1
to ensure that the output remains as it would be seen in Terminal so a grep will work on it.
So for example:
staticComputerGroup=$( /usr/bin/curl -s -k -u ${apiUser}:${apiPass} ${jssURL}/JSSResource/computers/udid/${uuid}/subset/groups_accounts | /usr/bin/xpath '/computer/groups_accounts/computer_group_memberships/group/text()' 2>&1 | sed 's/-- NODE --//g' | grep "MyGroup" )
I also threw in a subset to the call so it only gets more specific data back.
Incidentally, if the groups happen to not all have the same string in their names, you can use egrep
to look for all groups in one shot, like so
... | /usr/bin/egrep "MyGroup_01|MyGroup_02|MyGroup_03|MyGroup_04|MyGroup_05"
That should print back any results it finds, so, for example, say the computer is only part of group 1, 2,3 and 5, it would send back a result like
MyGroup_01
MyGroup_02
MyGroup_03
MyGroup_05
Posted on 01-18-2022 01:15 PM
Hi! I am trying to use this with egrep command as you suggested and I am not getting any output. I double checked the group names and everything. Any tips?
Posted on 03-12-2018 09:42 PM
@mm2270 your example worked like a charm!