Skip to content

Instantly share code, notes, and snippets.

@talkingmoose
Last active March 19, 2020 04:57
Show Gist options
  • Save talkingmoose/6fbb6eb228f10f40039de52c4db9c093 to your computer and use it in GitHub Desktop.
Save talkingmoose/6fbb6eb228f10f40039de52c4db9c093 to your computer and use it in GitHub Desktop.
Takes a list of Extension Attribute names in a file and disables them. Adds a comment to each "2020-04-17 - OK to DELETE after this date if EA is still disabled.". Edit lines 4-9 with custom details. Useful in conjunction with "Delete Extension Attributes.bash"
#!/bin/bash
# server connection information
URL="https://talkingmoose.jamfcloud.com"
userName="API-Editor"
password="P@55w04d"
# number of days from now that the extension attribute can be safely deleted because of non-use
deleteInDays="30"
# plain text file with Extension Attribute names listed on each line
eaList=$1
# show usage if no parameter supplied
if [[ "$eaList" = "" ]]; then
echo "Usage: Run this script followed by a plain text list of Extension Attributes
(Example: /path/to/this/script.bash /path/to/file.txt)"
exit 0
fi
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"
# read list of extension attribute names from list
eaNames=$( /bin/cat "$1" )
# get list of extension attributes in Jamf Pro
apiList=$( /usr/bin/curl -s "$URL/JSSResource/computerextensionattributes" \
--user "$userName":"$password" \
--header "Accept: text/xml" \
--request GET )
# recurse through the list of names and disable them in Jamf Pro
while IFS= read anEA
do
# get extension attribute ID
eaID=$( /usr/bin/xpath "//name[text()='$anEA']/preceding-sibling::id/text()" 2> /dev/null <<< "$apiList" )
echo "Disabling extension attribute: $anEA [ID: $eaID]"
# XML data to upload
putXML="<computer_extension_attribute><description>$( /bin/date -v +${deleteInDays}d '+%Y-%m-%d' ) - OK to DELETE after this date if EA is still disabled.</description><enabled>false</enabled></computer_extension_attribute>"
# execute the command
result=$( /usr/bin/curl -s "$URL/JSSResource/computerextensionattributes/id/$eaID" \
--write-out "%{http_code}" \
--user "$userName":"$password" \
--header "Content-Type: text/xml" \
--request PUT \
--data "$putXML" )
# evaluate HTTP status code and return result
resultStatus=${result: -3}
code=$( /usr/bin/grep "$resultStatus" <<< "$httpErrorCodes" )
echo "$code"
# adding a new line for readability
echo
done <<< "$eaNames"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment