Skip to content

Instantly share code, notes, and snippets.

@patgmac
Created October 1, 2020 16:32
Show Gist options
  • Save patgmac/df0c4ca788d92e48ce21c15864184336 to your computer and use it in GitHub Desktop.
Save patgmac/df0c4ca788d92e48ce21c15864184336 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Purpose: Given a list of Apple serials (from purchasing, etc), this will help
# confirm those serials were in fact added to ABM/ASM by your reseller.
#
# Written by: Patrick Gallagher
# Updated: 06/17/2020
# Borrowed from https://gist.github.com/talkingmoose/327427d23b422000f9d17183f8ef1d22
# This requires "jq", install with "brew install jq" or from https://stedolan.github.io/jq/
jamfproURL="" ## Set the Jamf Pro URL here, with port if not 443
apiUser="" ## Set the username here if you want it hardcoded.
apiPass="" ## Set the password here if you want it hardcoded.
prestageID="1" ## Enter the ID of your prestage, not currently using this. If using this, switch the jq commands towards end of script.
serialList="serialList.csv"
inputFile="$1"
inputFileFormatted="formatted_${inputFile}"
outputFile="missing_from_prestage-${inputFile}"
# Confirm a file was given as an arg
if [[ $# -eq 0 || $1 != *.csv ]]; then
echo "Usage: $0 filename.csv"
echo ""
echo "filename.csv should exist in the same directory as this script"
exit 1
else
# Remove the leading S in the serial numbers, if present.
echo "Working with serials from $inputFile, removing leading S from serials"
touch $inputFileFormatted
sed 's/^S//g' $inputFile > $inputFileFormatted
fi
# Confirm jq is installed
if [[ ! -f /usr/local/bin/jq ]]; then
echo "ERROR: You must install jq before running this script"
exit 1
fi
# Grab API user and password if not specified above
if [[ $apiUser == "" ]]; then
printf "Username: "
read apiUser
printf "\n"
fi
if [[ $apiPass == "" ]]; then
stty -echo
printf "Password: "
read apiPass
stty echo
printf "\n"
fi
# Obtain an auth token
encodedCredentials=$( printf "$apiUser:$apiPass" | /usr/bin/iconv -t ISO-8859-1 | /usr/bin/base64 -i - )
authToken=$( /usr/bin/curl "$jamfproURL/uapi/auth/tokens" \
--silent \
--request POST \
--header "Authorization: Basic $encodedCredentials" )
# parse authToken for token, omit expiration
token=$( /usr/bin/awk -F \" '{ print $4 }' <<< "$authToken" | /usr/bin/xargs )
touch serials.json
curl $jamfproURL/uapi/v1/computer-prestages/scope \
--silent \
--request GET \
--header "Authorization: Bearer $token" > serials.json
#cat serials.json | jq -r '.assignments[] | "\(.serialNumber)"' > $serialList # Used when checking a specific prestage by ID
cat serials.json | /usr/local/bin/jq -r '.serialsByPrestageId | keys | .[]' > $serialList # Used when checking all prestages
# Checks to see which serials are missing from
grep -v -f $serialList $inputFileFormatted > $outputFile
echo "List of any serials missing from prestage (if any) are listed in $outputFile"
echo "Cleaning up temp files"
rm serials.json
rm $serialList
rm $inputFileFormatted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment