Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/sh
# Name: addPackages.sh
# Date: 27 Mar 2018
# Author: Steve Wood (steve.wood@omnicomgroup.com)
# Purpose: used to stub out packages in the Jamf Pro database
# The CSV file needs to be saved as a UNIX file with LF, not CR
# Version: 1.1
# Update: 27 Mar 2018 - fixing an error in the counter
# A good portion of this script is re-purposed from the script posted in the following JAMF Nation article:
#
# https://jamfnation.jamfsoftware.com/discussion.html?id=13118#respond
#
args=("$@")
#jssAPIUsername="${args[0]}"
#jssAPIPassword="${args[1]}"
jssAPIUsername="apiuser"
jssAPIPassword="apipass"
jssAddress="https://yourserveraddress.com"
file="${args[0]}"
#Verify we can read the file
data=`cat $file`
if [[ "$data" == "" ]]; then
echo "Unable to read the file path specified"
echo "Ensure there are no spaces and that the path is correct"
exit 1
fi
totalqty=`awk -F, 'END {printf "%s\n", NR}' $file`
#Set a counter for the loop
counter="0"
duplicates=[]
echo $data
#Loop through the CSV and submit data to the API
while [ $counter -lt $totalqty ]
do
counter=$[$counter+1]
line=`echo "$data" | head -n $counter | tail -n 1`
package=`echo "$line" | awk -F , '{print $1}'`
apiData="<package><id>0</id><filename>${package}</filename><name>${package}</name></package>"
output=`curl -sS -k -i -u ${jssAPIUsername}:${jssAPIPassword} -X POST -H "Content-Type: text/xml" -d "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>$apiData" ${jssAddress}/JSSResource/packages`
echo $output
error=""
error=`echo $output | grep "Conflict"`
if [[ $error != "" ]]; then
duplicates+=($serialnumber)
fi
done
echo "The following packages could not be created:"
printf -- '%s\n' "${duplicates[@]}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.