Command Not Found - JAMF School simple loop script

Almost_a_Coder
New Contributor

Just trying to run a script on machines that deletes all the garbage entries of printers that JAMF School has put onto systems while trying to manage/add printers in the JAMF Cloud web interface (which is actually a nightmare since there is no real documentation other than "just do this" and nothing is actually validated before it is pushed to systems).  Even though I removed the printers in JAMF, the printers still show up as local entries on each machine and I can also do lplist which dumps all the printer entries that were added to CUPS from JAMF.  I can also see all the printers in CUPS WebUI.

Now, I just want to a simple bash script that is sent to each machine once to fully clear the printers so I can make one more push in JAMF as a clean slate for printers here on out.  Scripting Module is enabled and works(already tested).

Here are two versions of a simple looping script I have tried to run - both fail:

#!/bin/bash

lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" $printer
lpadmin -x $printer
done


ERRORS from JAMF:
1:233: execution error: /Library/Application Support/ZuluDesk Scripting/com.zuludesk.scripting.8f682a35-7629-11ed-b773-027c376c5399/com.zuludesk.scripting.8f682a35-7629-11ed-b773-027c376c5399.command: line 2: 
: command not found
/Library/Application Support/ZuluDesk Scripting/com.zuludesk.scripting.8f682a35-7629-11ed-b773-027c376c5399/com.zuludesk.scripting.8f682a35-7629-11ed-b773-027c376c5399.command: line 7: syntax error near unexpected token `done'
/Library/Application Support/ZuluDesk Scripting/com.zuludesk.scripting.8f682a35-7629-11ed-b773-027c376c5399/com.zuludesk.scripting.8f682a35-7629-11ed-b773-027c376c5399.command: line 7: `done' (2)

Here is the second:

for printer in `lpstat -p | awk '{print $2}'`
do
echo Deleting $printer
lpadmin -x $printer
done


ERRORS from JAMF:
1:233: execution error: /Library/Application Support/ZuluDesk Scripting/com.zuludesk.scripting.8f682a35-7629-11ed-b773-027c376c5399/com.zuludesk.scripting.8f682a35-7629-11ed-b773-027c376c5399.command: line 2: syntax error near unexpected token `do
'
/Library/Application Support/ZuluDesk Scripting/com.zuludesk.scripting.8f682a35-7629-11ed-b773-027c376c5399/com.zuludesk.scripting.8f682a35-7629-11ed-b773-027c376c5399.command: line 2: `do
' (2)

 

Now, the first script was actually pulled from here on JAMF Nation.  Just really getting under my skin since I am getting command problems yet I can run either of these scripts locally on a machine without problems.

 

I could use a hand here.

2 REPLIES 2

Almost_a_Coder
New Contributor

Replying to myself in case anyone finds this thread.  After digging really deep, I was able to get this script to fire off onto a machine via Jamf School with sucess:

#! /bin/bash
echo "admin password" | su -S localadminaccount
whoami
echo "If root is shown: success"
echo "Attempting to make all users into 'print administrators'"
echo "admin password" | sudo -S dseditgroup -o edit -n /Local/Default -a staff -t group _lpadmin
for p in `lpstat -p | awk '{print $2}'`
do
    echo "Deleting $p"
    lpadmin -x $p
done
echo "Process finished without errors!"

Unsafe way of doing thigs so I am all ears if someone has a more secure way of doing this.  Right now, users are by default already print admins but they have to log in as a confirmation.  Right now, this allows them to avoid that.  It might not be a necessary line though since this script runs the localmachine account anyway.

Since we setup all of our 50 some MACs individually with the localaccount before binding to our domain, this worked out.

Fluffy
Contributor III

If you want a truly clean slate, there is a command that is the same as the GUI "Reset printing system...":

 

/System/Library/Frameworks/ApplicationServices.framework/Frameworks/PrintCore.framework/Versions/A/printtool --reset -f

 

 I don't know if you'll run into the same issue with print admins. I spent a lot of time making a custom app for adding and removing printers, but it was all local and none of it through Jamf School's script module.