Skip to content

Instantly share code, notes, and snippets.

@talkingmoose
Created March 21, 2020 00:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save talkingmoose/ad57503c09fac1ba73768c32b187284b to your computer and use it in GitHub Desktop.
Save talkingmoose/ad57503c09fac1ba73768c32b187284b to your computer and use it in GitHub Desktop.
Prompts an administrator to choose a Jamf Pro static group and provide a computer serial number to add to that group.
#!/bin/bash
# server connection information
URL="https://talkingmoose.jamfcloud.com"
userName="API-Editor"
password="P@55w0rd"
httpErrorCodes="200 Request successful
201 Request to create or update object successful
400 Bad request
401 Authentication failed
403 Invalid permissions
404 Object/resource not found
409 Conflict
500 Internal server error"
# get list of static groups
computerGroupXML=$( /usr/bin/curl "$URL/JSSResource/computergroups" \
--user "$userName":"$password" \
--header "Accept: text/xml" \
--request GET \
--silent )
# parse XML for static groups
staticGroupList=$( /usr/bin/xpath "//is_smart[text()='false']/preceding-sibling::name/text()" 2>&1 <<< "$computerGroupXML" | /usr/bin/sed 's/-- NODE --//g' | /usr/bin/tail -n +3 | /usr/bin/sort )
# display dialog to choose a group and endcode for HTTP submission
theCommand="choose from list every paragraph of \"$staticGroupList\" with title \"Add to Static Group\" with prompt \"Choose a static group...\" multiple selections allowed false empty selection allowed false"
staticGroupName=$( /usr/bin/osascript -e "$theCommand" | /usr/bin/sed -e 's/ /%20/g' )
# display dialog to prompt for serial number
theCommand="display dialog \"Enter a computer serial number...\" default answer \"\" with title \"Add to Static Group\" buttons {\"Cancel\", \"OK\"} default button {\"OK\"}"
results=$( /usr/bin/osascript -e "$theCommand" )
serialNumber=$( echo "$results" | /usr/bin/awk -F "text returned:" '{print $2}' )
# add new serial number to list
xmlData="<computer_group><computer_additions><computer><serial_number>$serialNumber</serial_number></computer></computer_additions></computer_group>"
# add serial number to static group
uploadData=$( /usr/bin/curl "$URL/JSSResource/computergroups/name/$staticGroupName" \
--write-out "%{http_code}" \
--user "$userName":"$password" \
--header "Content-Type: text/xml" \
--data "$xmlData" \
--request PUT \
--silent )
# evaluate HTTP status code
resultStatus=${uploadData: -3}
code=$( /usr/bin/grep "$resultStatus" <<< "$httpErrorCodes" )
echo "$code"
# display status dialog
theCommand="display dialog \"Status: $code\" with title \"Add to Static Group\" buttons {\"OK\"} default button {\"OK\"}"
/usr/bin/osascript -e "$theCommand"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment