Removing printers with a script

mpittcasd
Contributor

Hello all,

I'm trying to create a policy to remove printers with a script. The script I found on here that I'm trying to use is:

#!/bin/sh
# remove all printers
rm -rf /etc/cups/printers.conf

# restart cups
killall cupsd

exit 0

When I put it in a policy in Self Service and run it I get the following back in the log:

Executing Policy Remove Printers Script...
[STEP 1 of 2]
Mounting casdjamf.chambersburg.k12.pa.us to /Volumes/CasperShare...
[STEP 2 of 3]
Mounting casdmac to /Volumes/CasperShare 1...
Error: Could not mount distribution point "casdmac".
[STEP 3 of 3]
Running script removeprinters.sh...
Script exit code: 126
Script result: sh: /Library/Application Support/JAMF/tmp/removeprinters.sh: /bin/sh
#: bad interpreter: No such file or directory

I tried running the script using the sh command in terminal I don't get any error messages, but I still have my printers as well. I tried doing some research here and through Google but I'm still kind of stuck. Any advice is greatly appreciated.

8 REPLIES 8

CasperSally
Valued Contributor II

Here's my script to remove all printers

#!/bin/sh

launchctl stop org.cups.cupsd

#Then rename the the cupsd.conf file:
sudo mv /etc/cups/cupsd.conf /etc/cups/cupsd.conf.backup

#Restore the default cupsd.conf file:
sudo cp /etc/cups/cupsd.conf.default /etc/cups/cupsd.conf

#Then rename the printers.conf file:
sudo mv /etc/cups/printers.conf /etc/cups/printers.conf.backup

#Next, restart cups with:
sudo launchctl start org.cups.cupsd

stevewood
Honored Contributor II
Honored Contributor II

I use a script very similar to @CasperSally. It looks like the problem you are having delivering it from the JSS is that the machine cannot mount your Casper Share. Can you deploy any other software or scripts to that machine?

As far as running from the terminal and not having it delete, try the script that Casper Sally listed above.

Look
Valued Contributor III

I was wanting to remove printers with UNIFLOW in the name used this. Pretty sure you could pull something useful out of it. Seemed to work without any issues.

echo "--Current printers"
lpstat -a | cut -d" " -f1 | while read PRTA
do
echo "-"$PRTA
done

lpstat -a | lpstat -a | cut -d" " -f1 | grep "UNIFLOW" | while read PRTB
do
echo "-- UNIFLOW printer detected attempting to delete: "$PRTB
lpadmin -x $PRTB
sleep 1
done

echo "--Remaining printers"
lpstat -a | cut -d" " -f1 | while read PRTC
do
echo "-"$PRTC
done

stevevalle
Contributor III

I have a self service policy that runs this command:

lpstat -p | cut -d" " -f2 | xargs -I{} lpadmin -x {}

Removes all installed printers. Works quite well.

mramola
New Contributor III

I know this post is from several years ago - but wondering if there is a way to change this script to name the printer I was removed?  I don't want to remove all printers just one named HS Aux Workroom

JAMAUAI
New Contributor II

@stevevalle - I run the exact same command. Awesome.

mpittcasd
Contributor

Sorry for such a late reply, but I finally got time to sit down and work on this.

I started with the script CasperSally posted. When I run that in terminal with the sh command I get the following:

launchctl stop error: No such process
mv: /etc/cups/printers.conf: No such file or directory

I then used sudo launchctl unload -w /System/Library/LaunchDaemons/org.cups.cupsd.plist and sudo launchctl load -w /System/Library/LaunchDaemons/org.cups.cupsd.plist. After that I tried the script with the sh command again and get:

mv: /etc/cups/printers.conf: No such file or directory

@stevewood][/url I am able to push things out otherwise, I wasn't sure if it was an issue with the script or how I uploaded in Casper Admin or how I tried to run it through a policy. I did use connect to server on one of the macs I'm working on to make sure I could reach both of my distribution points.

After all that, I tried the command lpstat -p | cut -d" " -f2 | xargs -I{} lpadmin -x {} that @stevevalle][/url posted. It gave me the following:

lpadmin: Undefined error: 0

That kept occurring until I stopped the command. It did seem to remove 1 printer though, but left 3. I'll keep my search up, but I appreciate your replies so far.

I tried the commands posted by @jacob_salmela in this thread: https://jamfnation.jamfsoftware.com/discussion.html?id=8695

When I run them individually they seem to work; The printers were cleared off of this system and the policies added back the appropriate printers. I tried running the script through terminal but it doesn't appear to do anything, would that behavior be the same if I upload the script to Casper Admin and deploy it with a policy? The other problem I'm running into is print jobs requiring a username and password. After printing the icon in the dock will bounce and say "Hold for authentication" even though there shouldn't be any security like that on the printer. Is that an option in the OS I can disable somewhere?

sam_clark
New Contributor III

Hello all,

Great stuff in this thread. I wanted to pass along my solution to only remove mcx managed printers from the user's printer list. There are instances where we allow users to add unmanaged printers to their printer list and we don't want to remove those when we modify our own mcx printer configurations.

I modified @stevevalle's command above to only remove MCX printers:

lpstat -p | cut -d' ' -f2 | grep mcx | xargs -I{} lpadmin -x {}

Simple and effective.