cancel
Showing results for 
Search instead for 
Did you mean: 
Disclaimer
JUMPCLOUD EXPRESSLY DISCLAIMS ALL REPRESENTATIONS, WARRANTIES, CONDITIONS, AND LIABILITIES OF ANY KIND ARISING FROM OR RELATED TO THIRD-PARTY SOFTWARE, SCRIPTS, REPOSITORIES, AND APIS. JUMPCLOUD IS NOT REQUIRED TO SUPPORT ANY SUCH THIRD-PARTY MATERIALS AND ALL RISKS RELATED TO THIRD-PARTY MATERIALS ARE YOUR RESPONSIBILITY. PLEASE ALSO REVIEW THE JUMPCLOUD TOS.

[Script] Add a Printer to macOS devices with JumpCloud Commands

krichard
Novitiate III

This is the path I took to create a recurring scheduled Command in JumpCloud to install a printer to a group of macOS devices. If you're like me and were asked to programmatically add a printer using JumpCloud to your macOS machines, you quickly discover the built-in macOS programs lpadmin and lpstat. lpstat provides you with information on already installed printers and drivers. Whereas lpadmin can add, change, and remove printers. Learn more about both programs here https://www.cups.org/doc/admin.html 

 

Step 1 - Get your lpadmin command straight

The printer we will be installing is called Printer XYZ and is at IP address 1.2.3.4. We also need drivers to be installed for Printer XYZ. After manually installing the latest driver, the driver location and name can be confirmed by navigating to /Library/Printers/PPDs/Contents/Resources/ then searched for the printer Brand and Model number. In this case I found Printer XYZ and dragged the printer driver into Terminal to grab the full path. Save this.

 

/Library/Printers/PPDs/Contents/Resources/Printer\ XYZ

 

Now we have enough information to start building out our script using lpadmin. -p is our printer name. -D is our queue name, but we are using it hear to make the printer name that appears in System Preferences -> Printers & Scanners more human readable. -v is where our printer is located at on the network. -P is the location of our printers drivers -o can be called numerous time to add options to your printer, but we're making sure the printer is not shared by this computer here. Lastly, -E enables the printer and finishes mapping it in macOS. 

 

lpadmin -p Printer_XYZ -D "Printer XYZ" -v ipp://1.2.3.4/ipp/print -P /Library/Printers/PPDs/Contents/Resources/Printer\ XYZ -o printer-is-shared=false -E

 

Step 2 - Upload your driver package

In this example, I uploaded the driver package to Dropbox, but you could do this to any fileshare your organization may have such as an AWS S3 bucket. When you share the file from Dropbox, you want to replace the domain www.dropbox.com with dl.dropboxusercontent.com in order for it to work with curl consistently.

Step 3 - Install Driver Package

 

curl "https://dl.dropboxusercontent.com/s/XXXXX/Printer_Driver.pkg" --output /tmp/Printer_Driver.pkg
	installer -pkg /tmp/Printer_Driver.pkg -target /
	echo "Installing Printer"
	sleep 30
	rm /tmp/Printer_Driver.pkg

 

Step 4 - Test for existing printer with lpstat

 

if lpstat -s | grep "1.2.3.4" ; then
	echo "VERIFIED: Printer is installed"
else
       echo "Printer not installed"
fi

 

Step 5 - Put it all together

 

if lpstat -s | grep "1.2.3.4" ; then
	echo "VERIFIED: Printer is installed"
else
	curl "https://dl.dropboxusercontent.com/s/XXXXX/Printer_Driver.pkg" --output /tmp/Printer_Driver.pkg
	installer -pkg /tmp/Printer_Driver.pkg -target /
	echo "Installing Printer"
	sleep 30
	rm /tmp/Printer_Driver.pkg
	lpadmin -p Printer_XYZ -D "Printer XYZ" -v ipp://1.2.3.4/ipp/print -P /Library/Printers/PPDs/Contents/Resources/Printer\ XYZ -o printer-is-shared=false -E
fi

 

Finally

I would like to thank @BryanDalzell in the Slack Lounge for their help in tightening up the finished script by providing example code from their other App Install projects. This helped clarify for me how to best use of if, else, and fi statements to make this an ongoing Command.

0 REPLIES 0