Using the Jamf Pro API to mass-delete obsolete packages and scripts
2021-04-17 04:52:16 Author: derflounder.wordpress.com(查看原文) 阅读量:229 收藏

If you’re using AutoPkg and tools like jamf-upload or JSSImporter to automate the uploading of packages and scripts to your Jamf Pro server, it may be necessary to periodically delete a large number of now-obsolete installer packages or scripts from your server. To help with this, I’ve written a couple of scripts to help automate the deletion process by using a list of Jamf IDs and the API to perform the following tasks:

  1. Delete the relevant installer packages or scripts.
  2. Generate a report of which packages or scripts were deleted.

For more details, please see below the jump.

Both scripts work with a text file of Jamf Pro IDs, and also include error checking to make sure that the text file’s entries contained only positive numbers.

To use these scripts, you will need four things:

  1. A text file containing the Jamf Pro package or script IDs you wish to delete.
  2. The address of the appropriate Jamf Pro server
  3. The username of an account on the Jamf Pro server which has the necessary privileges to delete computers and/or mobile devices.
  4. The password to that account.

The test file should contain only the relevant Jamf Pro IDs and appear similar to this:

924
1041
1079
1234
1244
1263
1269
1765
1213
1235
1253
1260
1273
1219
1334
1351
1298
1320
1394
1415
1430
1375
1464
1506
1444
1566
1585
1595
1606
1529
1542
1684
1625
1627
1654
1726
1742
1756
1705
1768
1772
1786
1527
1635
1677

Once you have the text file and the other prerequisites, the scripts can be run using the following commands:

To delete installer packages:

/path/to/delete_Jamf_Pro_Packages.sh /path/to/text_filename_here.txt

To delete scripts:

/path/to/delete_Jamf_Pro_Scripts.sh /path/to/text_filename_here.txt

For authentication, the scripts can accept manual input or values stored in a ~/Library/Preferences/com.github.jamfpro-info.plist file.

The plist file can be created by running the following commands and substituting your own values where appropriate:

To store the Jamf Pro URL in the plist file:

defaults write com.github.jamfpro-info jamfpro_url https://jamf.pro.server.goes.here:port_number_goes_here

To store the account username in the plist file:

defaults write com.github.jamfpro-info jamfpro_user account_username_goes_here

To store the account password in the plist file:

defaults write com.github.jamfpro-info jamfpro_password account_password_goes_here

It is also possible to simulate a run of the script, to make sure everything is working before running the actual deletion. To put the script into simulation mode, comment out the following line of the script.

delete_Jamf_Pro_Packages.sh

/usr/bin/curl -su ${jamfpro_user}:${jamfpro_password} "${jamfproIDURL}/$PackagesID" -X DELETE

delete_Jamf_Pro_Scripts.sh

/usr/bin/curl -su ${jamfpro_user}:${jamfpro_password} "${jamfproIDURL}/$ScriptsID" -X DELETE

To take it out of simulation mode and enable deletion, uncomment the line.

In simulation mode, you can test out if the script is reading the text file properly and the authentication method. For example, the following output should be seen in simulation mode if the text file is being read properly and manual input is being used.

username@computername ~ % /path/to/delete_Jamf_Pro_Scripts.sh ~/Desktop/home_scripts_report.txt

Please enter your Jamf Pro server URL : https://jamf.pro.server.goes.here:8443
Please enter your Jamf Pro user account : jpadmin
Please enter the password for the jpadmin account:
Deleting iscasperonline.sh - script ID 13.

Deleted iscasperonline.sh - script ID 13.

Deleting xcode_uninstall.sh - script ID 15.

Deleted xcode_uninstall.sh - script ID 15.

Report on deleted scripts available here: /var/folders/wz/mp27mjl97h505nvff787hh3c0000gn/T/tmp.IaiOiHgI.tsv
username@computername ~ %

The following output should be seen in production mode if the text file is being read properly and the needed values are being read from a ~/Library/Preferences/com.github.jamfpro-info.plist file.

username@computername ~ % /path/to/delete_Jamf_Pro_Scripts.sh ~/Desktop/home_scripts_report.txt

Deleting iscasperonline.sh - script ID 13.
<?xml version="1.0" encoding="UTF-8"?><script><id>13</id></script>
Deleted iscasperonline.sh - script ID 13.

Deleting xcode_uninstall.sh - script ID 15.
<?xml version="1.0" encoding="UTF-8"?><script><id>15</id></script>
Deleted xcode_uninstall.sh - script ID 15.

Report on deleted scripts available here: /var/folders/wz/mp27mjl97h505nvff787hh3c0000gn/T/tmp.vZgL8WOk.tsv
username@computername ~ %

Once the script has completed its run, it will generate a report on the deleted items in tab-separated format and display the .tsv file’s location.

Screen Shot 2021 04 16 at 2 43 29 PM

The scripts are available below, and at the following addresses on GitHub:

https://github.com/rtrouton/rtrouton_scripts/tree/master/rtrouton_scripts/Casper_Scripts/delete_Jamf_Pro_Packages

https://github.com/rtrouton/rtrouton_scripts/tree/master/rtrouton_scripts/Casper_Scripts/delete_Jamf_Pro_Scripts

delete_Jamf_Pro_Packages.sh:

#!/bin/bash
##########################################################################################
# Packages Delete Script for Jamf Pro
#
#
# Usage: Call script with the following four parameters
# – a text file of the Jamf Pro package IDs you wish to delete
#
# You will be prompted for:
# – The URL of the appropriate Jamf Pro server
# – Username for an account on the Jamf Pro server with sufficient API privileges
# – Password for the account on the Jamf Pro server
#
# The script will:
# – Delete the specified packages using their Jamf Pro package IDs
# – Generate a report of all successfully deleted packages in TSV format
#
# Example: ./delete_Jamf_Pro_Packages.sh jamf_pro_id_numbers.txt
#
##########################################################################################
filename="$1"
ERROR=0
report_file="$(mktemp).tsv"
if [[ -n $filename && -r $filename ]]; then
# If you choose to hardcode API information into the script, uncomment the lines below
# and set one or more of the following values:
#
# The username for an account on the Jamf Pro server with sufficient API privileges
# The password for the account
# The Jamf Pro URL
#jamfpro_url="" ## Set the Jamf Pro URL here if you want it hardcoded.
#jamfpro_user="" ## Set the username here if you want it hardcoded.
#jamfpro_password="" ## Set the password here if you want it hardcoded.
# If you do not want to hardcode API information into the script, you can also store
# these values in a ~/Library/Preferences/com.github.jamfpro-info.plist file.
#
# To create the file and set the values, run the following commands and substitute
# your own values where appropriate:
#
# To store the Jamf Pro URL in the plist file:
# defaults write com.github.jamfpro-info jamfpro_url https://jamf.pro.server.goes.here:port_number_goes_here
#
# To store the account username in the plist file:
# defaults write com.github.jamfpro-info jamfpro_user account_username_goes_here
#
# To store the account password in the plist file:
# defaults write com.github.jamfpro-info jamfpro_password account_password_goes_here
#
# If the com.github.jamfpro-info.plist file is available, the script will read in the
# relevant information from the plist file.
if [[ -f "$HOME/Library/Preferences/com.github.jamfpro-info.plist" ]]; then
if [[ -z "$jamfpro_url" ]]; then
jamfpro_url=$(defaults read $HOME/Library/Preferences/com.github.jamfpro-info jamfpro_url)
fi
if [[ -z "$jamfpro_user" ]]; then
jamfpro_user=$(defaults read $HOME/Library/Preferences/com.github.jamfpro-info jamfpro_user)
fi
if [[ -z "$jamfpro_password" ]]; then
jamfpro_password=$(defaults read $HOME/Library/Preferences/com.github.jamfpro-info jamfpro_password)
fi
fi
# If the Jamf Pro URL, the account username or the account password aren't available
# otherwise, you will be prompted to enter the requested URL or account credentials.
if [[ -z "$jamfpro_url" ]]; then
read -p "Please enter your Jamf Pro server URL : " jamfpro_url
fi
if [[ -z "$jamfpro_user" ]]; then
read -p "Please enter your Jamf Pro user account : " jamfpro_user
fi
if [[ -z "$jamfpro_password" ]]; then
read -p "Please enter the password for the $jamfpro_user account: " -s jamfpro_password
fi
echo ""
# Remove the trailing slash from the Jamf Pro URL if needed.
jamfpro_url=${jamfpro_url%%/}
# Set up the Jamf Pro Computer ID URL
jamfproIDURL="${jamfpro_url}/JSSResource/packages/id"
while read -r PackagesID
do
# Verify that the input is a number. All Jamf Pro
# IDs are positive numbers, so any other input will
# not be a valid Jamf Pro ID.
if [[ "$PackagesID" =~ ^[0-9]+$ ]]; then
if [[ ! -f "$report_file" ]]; then
/usr/bin/touch "$report_file"
printf "Deleted Package ID Number\tDeleted Package Name\n" > "$report_file"
fi
# Get package display name
PackagesName=$(/usr/bin/curl -su "${jamfpro_user}:${jamfpro_password}" -H "Accept: application/xml" "${jamfpro_url}/JSSResource/packages/id/$PackagesID" | xmllint –xpath '//package/name/text()'2>/dev/null)
# Remove comment from line below to preview
# the results of the deletion command.
echo -e "Deleting $PackagesName – package ID $PackagesID."
# Remove comment from line below to actually run
# the deletion command.
/usr/bin/curl -su ${jamfpro_user}:${jamfpro_password} "${jamfproIDURL}/$PackagesID" -X DELETE
if [[ $? -eq 0 ]]; then
printf "$PackagesID\t %s\n" "$PackagesName" >> "$report_file"
echo -e "\nDeleted $PackagesName – package ID $PackagesID.\n"
else
echo -e "\nERROR! Failed to delete $PackagesName – package ID $PackagesID.\n"
fi
else
echo "All Jamf Pro IDs are expressed as numbers. The following input is not a number: $PackagesID"
fi
done < "$filename"
else
echo "Input file does not exist or is not readable"
ERROR=1
fi
if [[ -f "$report_file" ]]; then
echo "Report on deleted installer packages available here: $report_file"
fi
exit "$ERROR"

delete_Jamf_Pro_Scripts.sh:

#!/bin/bash
##########################################################################################
# Scripts Delete Script for Jamf Pro
#
#
# Usage: Call script with the following four parameters
# – a text file of the Jamf Pro script IDs you wish to delete
#
# You will be prompted for:
# – The URL of the appropriate Jamf Pro server
# – Username for an account on the Jamf Pro server with sufficient API privileges
# – Password for the account on the Jamf Pro server
#
# The script will:
# – Delete the specified scripts using their Jamf Pro script IDs
# – Generate a report of all successfully deleted scripts in TSV format
#
# Example: ./delete_Jamf_Pro_Scripts.sh jamf_pro_id_numbers.txt
#
##########################################################################################
filename="$1"
ERROR=0
report_file="$(mktemp).tsv"
if [[ -n $filename && -r $filename ]]; then
# If you choose to hardcode API information into the script, uncomment the lines below
# and set one or more of the following values:
#
# The username for an account on the Jamf Pro server with sufficient API privileges
# The password for the account
# The Jamf Pro URL
#jamfpro_url="" ## Set the Jamf Pro URL here if you want it hardcoded.
#jamfpro_user="" ## Set the username here if you want it hardcoded.
#jamfpro_password="" ## Set the password here if you want it hardcoded.
# If you do not want to hardcode API information into the script, you can also store
# these values in a ~/Library/Preferences/com.github.jamfpro-info.plist file.
#
# To create the file and set the values, run the following commands and substitute
# your own values where appropriate:
#
# To store the Jamf Pro URL in the plist file:
# defaults write com.github.jamfpro-info jamfpro_url https://jamf.pro.server.goes.here:port_number_goes_here
#
# To store the account username in the plist file:
# defaults write com.github.jamfpro-info jamfpro_user account_username_goes_here
#
# To store the account password in the plist file:
# defaults write com.github.jamfpro-info jamfpro_password account_password_goes_here
#
# If the com.github.jamfpro-info.plist file is available, the script will read in the
# relevant information from the plist file.
if [[ -f "$HOME/Library/Preferences/com.github.jamfpro-info.plist" ]]; then
if [[ -z "$jamfpro_url" ]]; then
jamfpro_url=$(defaults read $HOME/Library/Preferences/com.github.jamfpro-info jamfpro_url)
fi
if [[ -z "$jamfpro_user" ]]; then
jamfpro_user=$(defaults read $HOME/Library/Preferences/com.github.jamfpro-info jamfpro_user)
fi
if [[ -z "$jamfpro_password" ]]; then
jamfpro_password=$(defaults read $HOME/Library/Preferences/com.github.jamfpro-info jamfpro_password)
fi
fi
# If the Jamf Pro URL, the account username or the account password aren't available
# otherwise, you will be prompted to enter the requested URL or account credentials.
if [[ -z "$jamfpro_url" ]]; then
read -p "Please enter your Jamf Pro server URL : " jamfpro_url
fi
if [[ -z "$jamfpro_user" ]]; then
read -p "Please enter your Jamf Pro user account : " jamfpro_user
fi
if [[ -z "$jamfpro_password" ]]; then
read -p "Please enter the password for the $jamfpro_user account: " -s jamfpro_password
fi
echo ""
# Remove the trailing slash from the Jamf Pro URL if needed.
jamfpro_url=${jamfpro_url%%/}
# Set up the Jamf Pro Computer ID URL
jamfproIDURL="${jamfpro_url}/JSSResource/scripts/id"
while read -r ScriptsID
do
# Verify that the input is a number. All Jamf Pro
# IDs are positive numbers, so any other input will
# not be a valid Jamf Pro ID.
if [[ "$ScriptsID" =~ ^[0-9]+$ ]]; then
if [[ ! -f "$report_file" ]]; then
/usr/bin/touch "$report_file"
printf "Deleted Script ID Number\tDeleted Script Name\n" > "$report_file"
fi
# Get script display name
ScriptsName=$(/usr/bin/curl -su "${jamfpro_user}:${jamfpro_password}" -H "Accept: application/xml" "${jamfpro_url}/JSSResource/scripts/id/$ScriptsID" | xmllint –xpath '//script/name/text()'2>/dev/null)
# Remove comment from line below to preview
# the results of the deletion command.
echo -e "Deleting $ScriptsName – script ID $ScriptsID."
# Remove comment from line below to actually run
# the deletion command.
#/usr/bin/curl -su ${jamfpro_user}:${jamfpro_password} "${jamfproIDURL}/$ScriptsID" -X DELETE
if [[ $? -eq 0 ]]; then
printf "$ScriptsID\t %s\n" "$ScriptsName" >> "$report_file"
echo -e "\nDeleted $ScriptsName – script ID $ScriptsID.\n"
else
echo -e "\nERROR! Failed to delete $ScriptsName – script ID $ScriptsID.\n"
fi
else
echo "All Jamf Pro IDs are expressed as numbers. The following input is not a number: $ScriptsID"
fi
done < "$filename"
else
echo "Input file does not exist or is not readable"
ERROR=1
fi
if [[ -f "$report_file" ]]; then
echo "Report on deleted scripts available here: $report_file"
fi
exit "$ERROR"

文章来源: https://derflounder.wordpress.com/2021/04/16/using-the-jamf-pro-api-to-mass-delete-obsolete-packages-and-scripts/
如有侵权请联系:admin#unsafe.sh