v0.2 = Lots of logic and user experience improvements

+ Improved logic to make sense with the workflow of being run from Jamf or CLI.
 + Changed a few bits to improve user experience
 + Added/improved several checks after tasks
 + Added loop to prompt to create another printer after creating a printer
 + Adjusted language outputed by checkStatusCode()
This commit is contained in:
Zack T
2018-03-06 13:37:16 -07:00
parent 220b6d9d3f
commit d012dccead

View File

@@ -3,7 +3,7 @@
###################################################################################################
# Script Name: jamf_CreatePrinters.sh
# By: Zack Thompson / Created: 3/1/2018
# Version: 0.1 / Updated: 3/5/2018 / By: ZT
# Version: 0.2 / Updated: 3/6/2018 / By: ZT
#
# Description: The purpose of this script is to assist Site Admins in creating Printers in Jamf without needing to use the Jamf Admin utility.
#
@@ -12,27 +12,10 @@
echo "***** CreatePrinters process: START *****"
##################################################
# Either use CLI arguments or prompt for choice
if [[ "${4}" == "Jamf" ]]; then
ranBy="Jamf"
else
action="${1}"
ranBy="CLI"
case $action in
-help | -h | * )
# Function getHelp
getHelp
;;
esac
fi
# Define Variables
# Either hard code or prompt for credentials
# jamfAPIUser="APIUsername"
# jamfAPIPassword="APIPassword"
jamfAPIUser=$(/usr/bin/osascript -e 'set userInput to the text returned of (display dialog "Enter your Jamf Username:" default answer "")' 2>/dev/null)
jamfAPIPassword=$(/usr/bin/osascript -e 'set userInput to the text returned of (display dialog "Enter your Jamf Password:" default answer "" with hidden answer)' 2>/dev/null)
jamfPS="https://jss.company.com:8443"
apiPrinters="${jamfPS}/JSSResource/printers/id"
@@ -53,76 +36,86 @@ Actions:
-help Displays this help section.
Example: jamf_CreatePrinters.sh -help
"
exit 0
}
createPrinter() {
# Set the osascript parameters and prompt User for Printer Selection
promptForChoice="choose from list every paragraph of \"$printerNames\" with prompt \"Choose printer to create in the JSS:\" OK button name \"Select\" cancel button name \"Cancel\""
# Set the osascript parameters and prompt User for Printer Selection.
promptForChoice="tell application (path to frontmost application as text) to choose from list every paragraph of \"$printerNames\" with prompt \"Choose printer to create in the JSS:\" OK button name \"Select\" cancel button name \"Cancel\""
selectedPrinterName=$(osascript -e "$promptForChoice")
# Handle if the user pushes the cancel button.
if [[ $selectedPrinterName == "false" ]]; then
echo "No printer selection was made."
createAnother="button returned:No"
return
fi
# Get the Printer ID of the selected printer.
printerID=$(printf $selectedPrinterName | cut -c 1)
# Get only the selected printers info.
selectedPrinterInfo=$(printf '%s\n' "$printerInfo" | xmllint --format - | xpath "/printers/printer[$printerID]/display_name | /printers/printer[$printerID]/cups_name | /printers/printer[$printerID]/location | /printers/printer[$printerID]/device_uri | /printers/printer[$printerID]/model" 2>/dev/null | LANG=C sed -e 's/<[^/>]*>//g' | LANG=C sed -e 's/<[^>]*>/,/g')
# Read the printer info into variables.
while IFS="," read -r printerName printerCUPsName printerLocation printerIP printerModel; do
if [[ -e "/private/etc/cups/ppd/${printerCUPsName}.ppd" ]]; then
printerPPDContents=$(printf "/private/etc/cups/ppd/${printerCUPsName}.ppd")
else
echo "Unable to locate the PPD file."
informBy "Unable to locate the PPD file -- unable to create the printer."
return
fi
# PUT changes to the JSS.
curlReturn="$(/usr/bin/curl "${curlAPI[@]}" PUT ${apiPrinters} --data "<printer>
# POST changes to the JSS.
curlReturn="$(/usr/bin/curl "${curlAPI[@]}" POST ${apiPrinters}/0 --data "<printer>
<name>$printerName</name>
<uri>$printerIP</uri>
<CUPS_name>$printerCUPsName</CUPS_name>
<location>$printerLocation</location>
<model>$printerModel</model>
<notes>Created by the jamf_CreatePrinters.sh script.</notes>
<make_default>$printerDefault</make_default>
<uri>${printerIP}</uri>
<CUPS_name>${printerCUPsName}</CUPS_name>
<location>${printerLocation}</location>
<model>${printerModel}</model>
<notes>Created by ${createdByUser} running the jamf_CreatePrinters.sh script.</notes>
<ppd>${printerCUPsName}.ppd</ppd>
<ppd_contents>$(cat $printerPPDContents)</ppd_contents>
<ppd_contents>$(cat ${printerPPDContents})</ppd_contents>
<ppd_path>/Library/Printers/PPDs/Contents/Resources/${printerCUPsName}.ppd</ppd_path>
</printer>")"
# Check if the API call was successful or not.
curlCode=$(echo "$curlReturn" | awk -F statusCode: '{print $2}')
checkStatusCode $curlCode $appID
checkStatusCode $curlCode
# Prompt if we want to create another printer.
createAnother=$(/usr/bin/osascript -e 'tell application (path to frontmost application as text) to display dialog "Do you want to create another printer?" buttons {"Yes", "No"}')
done < <(printf '%s\n' "${selectedPrinterInfo}")
}
checkStatusCode() {
case $1 in
200 )
# Turn off success notifications
# informBy " -> Request successful"
# Request successful
informBy "Printer created successfully!"
;;
201)
# Turn off success notifications
# informBy "App ID: ${appID} -> Request to create or update object successful"
informBy "Request to create or update object successful"
;;
400)
informBy "App ID: ${appID} -> Bad request. Verify the syntax of the request specifically the XML body."
informBy "Bad request. Verify the syntax of the request specifically the XML body."
;;
401)
informBy "App ID: ${appID} -> Authentication failed. Verify the credentials being used for the request."
informBy "Authentication failed. Verify the credentials being used for the request."
;;
403)
informBy "App ID: ${appID} -> Invalid permissions. Verify the account being used has the proper permissions for the object/resource you are trying to access."
informBy "Invalid permissions. Verify the account being used has the proper permissions for the object/resource you are trying to access."
;;
404)
informBy "App ID: ${appID} -> Object/resource not found. Verify the URL path is correct."
informBy "Object/resource not found. Verify the URL path is correct."
;;
409)
informBy "App ID: ${appID} -> Conflict"
informBy "Conflict! A resource by this printer name likely already exists."
;;
500)
informBy "App ID: ${appID} -> Internal server error. Retry the request or contact Jamf support if the error is persistent."
informBy "Internal server error. Retry the request or contact Jamf support if the error is persistent."
;;
esac
}
@@ -141,6 +134,25 @@ informBy() {
##################################################
# Bits Staged
# Set how to display verbose messages.
if [[ "${4}" == "Jamf" ]]; then
ranBy="Jamf"
createdByUser="${3}"
else
action="${1}"
ranBy="CLI"
case $action in
-help | -h )
# Function getHelp
getHelp
;;
esac
# Prompt for credentials.
jamfAPIUser=$(/usr/bin/osascript -e 'set userInput to the text returned of (display dialog "Enter your Jamf Username:" default answer "")' 2>/dev/null)
jamfAPIPassword=$(/usr/bin/osascript -e 'set userInput to the text returned of (display dialog "Enter your Jamf Password:" default answer "" with hidden answer)' 2>/dev/null)
fi
# Verify credentials were provided.
if [[ -z "${jamfAPIUser}" && -z "${jamfAPIPassword}" ]]; then
@@ -163,13 +175,11 @@ else
fi
# Get a list of all printer configurations.
printerInfo=$(sudo jamf listprinters | xmllint --format - | xpath /printers 2>/dev/null)
printerInfo=$(sudo jamf listprinters | xmllint --format - | xpath /printers 2>/dev/null)
# Get the number of printers.
numberOfPrinters=$(echo $(printf '%s\n' "$printerInfo") | xmllint --format - | xpath 'count(//printers/printer)' 2>/dev/null)
numberOfPrinters=$(echo $(printf '%s\n' "$printerInfo") | xmllint --format - | xpath 'count(//printers/printer)' 2>/dev/null)
# Clear the variable, in case we're rerunning the process.
unset printerNames
unset printerNames
# Loop through each printer to only get the printer name and add in it's printer "ID" -- node number in the xml.
for ((i=1; i<=$numberOfPrinters; ++i)); do
@@ -178,7 +188,13 @@ for ((i=1; i<=$numberOfPrinters; ++i)); do
done
# Drop the final \n (newline).
printerNames=$(echo -e ${printerNames} | perl -pe 'chomp if eof')
printerNames=$(echo -e ${printerNames} | perl -pe 'chomp if eof')
# We prompt to create another printer in the function; either continue create printers or complete script.
until [[ $createAnother == "button returned:No" ]]; do
# Function createPrinter
createPrinter
done
informBy "Script successfully completed!"
echo "***** CreatePrinters process: COMPLETE *****"