looking for a command to remove the jamf policies which are older than 30 days.

sashank
New Contributor

Hi

looking for a command to get remote policies in JAMF which are older than 30 days.

Here is the script i got.

URL="https://jamfpro.talkingmoose.net:8443"
userName="API-Editor"
password="password"

create the output directory and log file

in the same directory as this script

path to this script

currentDirectory=$( /usr/bin/dirname "$0" )

name of this script

CURRENTSCRIPT=$( /usr/bin/basename -s .sh "$0" )

create log file in same directory as script

logFile="$currentDirectory/$CURRENTSCRIPT - $( /bin/date '+%y-%m-%d' ).log"

functions

function logresult() { if [ $? = 0 ] ; then /bin/date "%Y-%m-%d %H:%M:%S $1" >> "$logFile" else /bin/date "%Y-%m-%d %H:%M:%S $2" >> "$logFile" fi
}

the time right now

startTime=$( /bin/date '+%s' )

start the log

logresult "--------------------- Begin Script ---------------------"

get list of existing policies in the Jamf Pro server

policyXML=$( /usr/bin/curl -k $URL/JSSResource/policies --user "$userName:$password" -H "Accept: text/xml" -X GET | /usr/bin/xmllint --format - )

logresult "Reading policy XML." "Failed to read policy XML."

create a list of IDs to delete

idList=$( echo "$policyXML" | /usr/bin/egrep -B1 '<name>[0-9]+-[0-9]{2}-[0-9]{2} at [0-9]{1,2}:[0-9]{2,2} [AP]M | . | .</name>' | /usr/bin/grep '<id>' | /usr/bin/awk -F '[><]' '{print $3}' )

while IFS= read aLine
do /usr/bin/curl -k "$URL/JSSResource/policies/id/$aLine" --user "$userName:$password" -X DELETE

logresult "Deleted ID "$aLine"." "Failed to delete ID "$aLine"."

idCount=$((idCount+1))

done <<< "$idList"

stop the timer

calculate how long the script ran

logresult "Completing script."
logresult "Processed $idCount policies."

the time right now

stopTime=$( /bin/date '+%s' )

subtract start time from stop time and log the time in seconds

DIFF=$(($stopTime-$startTime))
logresult "Script operations took $DIFF seconds to complete."

logresult "---------------------- End Script ----------------------
"

exit 0

0 REPLIES 0