Skip to content

Instantly share code, notes, and snippets.

@apizz
Last active May 17, 2023 20:08
Show Gist options
  • Save apizz/48da271e15e8f0a9fc6eafd97625eacd to your computer and use it in GitHub Desktop.
Save apizz/48da271e15e8f0a9fc6eafd97625eacd to your computer and use it in GitHub Desktop.
Jamf extension attribute for checking for and removing any failed macOS MDM commands
#!/bin/bash
jssurl="https://yourjss.com"
apiuser="yourapiuser"
apipass="yourapiuserpass"
serial=$(/usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice | /usr/bin/awk -F'"' '/IOPlatformSerialNumber/{print $4}')
####### FUNCTIONS
clearfailedmdmcommands () {
/usr/bin/curl -sfku "$apiuser":"$apipass" "$jssurl":8443/JSSResource/commandflush/computers/id/"$computerID"/status/Failed -X DELETE
}
getjsscomputerid () {
computerID=$(/usr/bin/curl -u "$apiuser":"$apipass" "$jssurl":8443/JSSResource/computers/serialnumber/"$serial" -H "accept: text/xml" | /usr/bin/xpath "/computer[1]/general/id/text()")
}
getfailedmdmcommands () {
xmlresult=$(/usr/bin/curl -sfku "$apiuser":"$apipass" "$jssurl":8443/JSSResource/computerhistory/serialnumber/"$serial"/subset/Commands -X GET -H "accept: application/xml" | /usr/bin/xpath "/computer_history/commands/failed")
}
####### SCRIPT
getfailedmdmcommands
# An empty failed XML node will look like this: <failed />
parseresult=$(/bin/echo "$xmlresult" | /usr/bin/grep "<failed />")
exitcode=$(/bin/echo $?)
# Clear failed MDM commands if they exist
if [ "$exitcode" != 0 ]; then
getjsscomputerid
/bin/echo "Removing failed MDM commands ..."
clearfailedmdmcommands
result="Removed Failed MDM Commands"
else
/bin/echo "No failed MDM commands exist."
result="No Failed MDM Commands Exist"
fi
/bin/echo "<result>$result</result>"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment