Extension Attribute to show number of printers on computers

Contributor III

I use JAMF to manage our lab computers, which should always have 2 printers set up (B&W and Color). I noticed recently that some of the computers have duplicate printers (some with as many as 2 B&W and 3 Color) and I want to clean that up.

My thinking was to:
1. Create Extension Attribute that tells me the number or printers installed on a computer
2. Create Smart Group that has as Criteria computers where the created EA is not 2 (there are other Criteria, but this is the relevant one)
3. Create policy scoped to that Smart Group that deletes all printers and pushes the PKG I have to reinstall the 2 printers.

After some troubleshooting I came up with the following for the EA:


#Get list of printers installed on computer
printers="$(lpstat -a |awk '{print $1}')"

#Get number of printers
numberPrinters=$(echo "$printers" | wc -l)

#Write to Extension attribute
echo "<result> $numberPrinters </result>"

It seemed to work if I ran the first 2 individual commands in Terminal on a test computer (gave me the result of 3 for a computer that had 3 printers installed). I created the EA (named Printers Installed) in JSS, setting the Data Type to Integer and Input Type to macOS Script. Then I created a Smart Group with the Criteria "Printers Installed - IS NOT - 2" (figuring this would find computers with both too many and too few printers). When I saved everything and clicked to View the Smart Group, it did pull up some computers that had too many, but it didn't pull up all of them and it also pulled up one computer that had 2 printers.

This is the first EA I've tried to create, so I was wondering if anyone could tell if I was missing something obvious?



Legendary Contributor III

How are the EA values actually showing up on machines that have run an inventory collection? Are there are any spaces or other extraneous characters appearing in the data? I see a few places where it may be giving you numbers with some spaces or other characters in the result, which could throw off your searches/Smart Groups.

You could try this slight modification to see if it gives you better results


printerCount=$(lpstat -a |awk '{print $1}' | awk 'END{print NR}')

echo "<result>$printerCount</result>"

Esteemed Contributor

@el2493 I could see a timing issue causing you to get too many results since any computer that hasn't run the EA should match your "IS NOT 2" criteria as the result will be blank at that point, but that you'd get too few or a false positive seems pretty odd.

Contributor III

Everything seems to be working correctly now, I think it might've just needed some time for it to correctly read the computers.