Force removing Printer Generator printers


Nick McSpadden has a wonderful project called Printer Generator that generates nopkg printer items for Munki.

I have a fork of Nick’s project that has a few tweaks.

Both of our projects really focus on seeing if a printer is installed with certain settings (name, location, IP address, make and model).

But what if your focus is on removing printers and not adding them or fixing them? If you use Nick’s original project or my fork of his project, someone can add the printer via IP address (assuming she knows the IP) and give it a different name, and the nopkg installcheck_script won’t know it, and so won’t know to remove it… or what name to remove it using.

If you want to employ Munki’s autoremove feature or just want a more aggressive managed_uninstall, here’s how you can do it.

For the installcheck_script, something like this, where you substitute in the actual printer IP address for printerIPaddress:

#!/bin/bash

# Check if the IP address exists in the printer list
printer_check=$(lpstat -s | grep “printerIPaddress“)

if [ -z “$printer_check” ]; then

# Not installed
exit 0

else
# Installed
exit 1

fi
and then for the uninstall_script, something like this, with the same substitution:
#!/bin/bash

printer_name=$(lpstat -s | grep -w “printerIPaddress” | sed -n ‘1 p’ | awk -F ” ” ‘{print $3}’ | awk -F “:” ‘{print $1}’)

/usr/sbin/lpadmin -x “$printer_name”
The sed -n ‘1 p’ just takes care of the extremely fringe case of someone having the same IP addresses for “two” printers, so it will just uninstall the first printer, and when Munki runs again, it will uninstall the “second” printer.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.