Script to delete all classes imported by ASM in Jamf Pro

jamflund
New Contributor III

Hi,

I need to delete all classes in Jamf pro imported by ASM. Does anybody have a working sh. script?

I have used this one below but getting in terminal this error "-:1: parser error : Document is empty"

 

#!/bin/bash
jssUser="xxx"
jssPass="xxx"
jssURL="https://xxxx:8443"

echo "Downloading list of class IDs..."
ids+=($(curl -X GET -s -k -u "$jssUser:$jssPass" "$jssURL/JSSResource/classes" | xmllint --format - | awk -F'>|<' '/<id>/{print $3}' | sort -n))

for n in "${ids[@]}"; do
curl -kvu $jssUser:$jssPass ${jssURL}JSSResource/classes/id/$n -X DELETE
done

5 REPLIES 5

cbrewer
Valued Contributor II

Depending on your Jamf Pro version, you likely now need a bearer token to authenticate.

https://derflounder.wordpress.com/2022/01/05/updated-script-for-obtaining-checking-and-renewing-bear... 

mdp
Contributor

Here's a quick script I wrote to delete all classes — based on your snippet, I'm assuming that's what you're looking to do, rather than just delete classes that were imported by ASM (which is doable, but less straightforward). Works in my instance of Jamf Pro.

https://github.com/MatthewPrins/Jamf/blob/main/Delete_Classes.sh

---
Matthew Prins -- Jamf Scripts @ Github

jamflund
New Contributor III

I got this error

Failed conversion of ``<stdin>: Unexpected character { at line 1'' using format ``%Y-%m-%dT%T''

date: illegal time format

usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...

            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

I've seen that error before — it's usually what I get when I forget to change or incorrectly enter the Jamf username/password/URL variables. Check that info (lines 11-13) to make sure it's entered in right.

Edit: After some testing, it's most likely the URL — that's the exact error I get with an incorrect URL, while the UN/PW error is slightly different

---
Matthew Prins -- Jamf Scripts @ Github

S_Whalen
New Contributor II

Hey. This code did it for me. Hopefully it is equally as successful for you as well. Oh a note: If on Apple's TextEditor, be sure to "Convert to Plain Text" and ensure the extension is .sh. Be well and have a great day!:

#!/bin/bash

####################################################################################################
#
# THIS SCRIPT IS NOT AN OFFICIAL PRODUCT OF JAMF SOFTWARE
# AS SUCH IT IS PROVIDED WITHOUT WARRANTY OR SUPPORT
#
# BY USING THIS SCRIPT, YOU AGREE THAT JAMF SOFTWARE
# IS UNDER NO OBLIGATION TO SUPPORT, DEBUG, OR OTHERWISE
# MAINTAIN THIS SCRIPT
#
####################################################################################################
#
# DESCRIPTION
# This script retrieves class information from the JAMF Pro RESTful API, saves it as an XML file,
# and then uses that XML file to delete the classes.
# Requires a user that has READ and DELETE privileges for Classes.
#
####################################################################################################
#
# DEFINE VARIABLES & READ IN PARAMETERS
#
####################################################################################################
echo "#####################"
echo "###!!! WARNING !!!###"
echo "#####################"
echo "This script will retrieve class information from the JAMF Pro server and delete all classes."
echo "Please ensure you have a database backup."
echo "There is no magic undo button other than restoring to a backup when the classes were in existence."
read -p "Are you sure you want to continue? [ y | n ] " answer
if [[ $answer != 'y' ]]; then
echo "Exiting script!"
exit 1
fi
read -p "Jamf Pro URL: " server
read -p "Jamf Pro Username: " username
read -s -p "Jamf Pro Password: " password
echo ""

# Trim the trailing slash off if necessary
if [ "${server: -1}" == "/" ]; then
server="${server%?}"
fi

# Add protocol and specify port 443
server="https://${server}:443"

# Base64 encode the username and password for basic authentication
auth=$(echo -n "$username:$password" | base64)

# Filename for the XML file
xmlFile="classes.xml"

echo "Retrieving class information..."
curl -s -H "Authorization: Basic $auth" -H "Accept: application/xml" "$server/JSSResource/classes" -o "$xmlFile"

echo "Deleting classes..."
classIDs=$(xmllint --xpath "//id/text()" "$xmlFile")
for classID in $classIDs; do
echo "Deleting class with ID: $classID"
curl -s -H "Authorization: Basic $auth" -H "Content-type: application/xml" -X DELETE "$server/JSSResource/classes/id/$classID"
done

echo "All classes have been deleted."

# Clean up XML file
rm "$xmlFile"