Hi,
First of all, thank you for taking the time to write and share your script. It's been invaluable in solving the many issues we've seen with shared printers through group policy preferences, where a printer can appear on a login one day, then disappear the next. :)
I have been testing out your script within our domain, and for the most part, it works very well. What I have discovered is that if the printer is assigned to a computer within an OU, without specifying a user group, then it's added to all logins everywhere. I've got around this for the most part by adding in a second check for the user against a user group or groups, which as it's good practice anyway, and really, I should have done that from the start, I'm good with that as a solution.
What I can't figure out, with my very limited Powershell knowledge and debugging techniques, is why, no matter what I do to the item level targeting on the following three printers, they appear on any computer.
<SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="School 1-061 Business Ed" status="5389P900016" image="2" changed="2021-05-21 11:44:23" uid="{89466D86-F024-48E3-99AF-E18F3FDFCF33}" userContext="1" bypassErrors="1"><Properties action="U" comment="" path="\\printer.school.domain\5389P900016" location="" default="1" skipLocal="0" deleteAll="0" persistent="0" deleteMaps="0" port=""/><Filters><FilterCollection bool="AND" not="0"><FilterOrgUnit bool="OR" not="0" name="OU=Business Studies 1-061,OU=Business Studies,OU=Enterprise,OU=Pupils,OU=School1,OU=Secondary,OU=Computers,OU=Schools W10,DC=School,DC=Domain" userContext="0" directMember="0"/><FilterOrgUnit bool="OR" not="0" name="OU=Computer Studies 1-060,OU=Computing,OU=Enterprise,OU=Pupils,OU=School1,OU=Secondary,OU=Computers,OU=Schools W10,DC=School,DC=Domain" userContext="0" directMember="0"/></FilterCollection><FilterGroup bool="AND" not="0" name="School\G SchoolStudents" sid="S-1-5-21-2723276998-1066919283-1565034395-81442" userContext="1" primaryGroup="0" localGroup="0"/></Filters></SharedPrinter>
<SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="School 1-058 Business Ed" status="5389P900017" image="2" changed="2021-05-21 12:47:57" uid="{9C177E2D-AC6B-4DB5-9FDA-AAE66D0ED090}" userContext="1" bypassErrors="1"><Properties action="U" comment="" path="\\printer.school.domain\5389P900017" location="" default="1" skipLocal="0" deleteAll="0" persistent="0" deleteMaps="0" port=""/><Filters><FilterCollection bool="AND" not="0"><FilterOrgUnit bool="OR" not="0" name="OU=Business Studies 1-058,OU=Business Studies,OU=Enterprise,OU=Pupils,OU=School1,OU=Secondary,OU=Computers,OU=Schools W10,DC=School,DC=Domain" userContext="0" directMember="0"/><FilterOrgUnit bool="OR" not="0" name="OU=Computer Studies 1-054,OU=Computing,OU=Enterprise,OU=Pupils,OU=School1,OU=Secondary,OU=Computers,OU=Schools W10,DC=School,DC=Domain" userContext="0" directMember="0"/></FilterCollection><FilterGroup bool="AND" not="0" name="School\G SchoolStudents" sid="S-1-5-21-2723276998-1066919283-1565034395-81442" userContext="1" primaryGroup="0" localGroup="0"/></Filters></SharedPrinter>
<SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="School 1-057 Business Ed" status="5389P900038" image="2" changed="2021-05-21 12:47:33" uid="{16775890-B060-4FC0-8292-5FCBC8698D89}" userContext="1" bypassErrors="1"><Properties action="U" comment="" path="\\printer.school.domain\5389P900038" location="" default="1" skipLocal="0" deleteAll="0" persistent="0" deleteMaps="0" port=""/><Filters><FilterCollection bool="AND" not="0"><FilterOrgUnit bool="OR" not="0" name="OU=Business Studies 1-057,OU=Business Studies,OU=Enterprise,OU=Pupils,OU=School1,OU=Secondary,OU=Computers,OU=Schools W10,DC=School,DC=Domain" userContext="0" directMember="0"/><FilterOrgUnit bool="OR" not="0" name="OU=Business Studies 1-055,OU=Business Studies,OU=Enterprise,OU=Pupils,OU=School1,OU=Secondary,OU=Computers,OU=Schools W10,DC=School,DC=Domain" userContext="0" directMember="0"/><FilterOrgUnit bool="OR" not="0" name="OU=Business Studies 1-056,OU=Business Studies,OU=Enterprise,OU=Pupils,OU=School1,OU=Secondary,OU=Computers,OU=Schools W10,DC=School,DC=Domain" userContext="0" directMember="0"/></FilterCollection><FilterGroup bool="AND" not="0" name="School\G SchoolStudents" sid="S-1-5-21-2723276998-1066919283-1565034395-81442" userContext="1" primaryGroup="0" localGroup="0"/></Filters></SharedPrinter>
What I can say from testing is that deployment was across all user groups until I added in the user group item level target, and then they only appeared for students. If I change that group to staff, then they only appeared everywhere for staff. It doesn't matter if those targeted OUs were within or out with a collection, or even stripped down to an individual OU the end result is the exactly same. From putting in various breakpoints and checks on the script, I think I narrowed down the issue to the evaluatefilters function returning a wrong boolean on a check to see whether the OU was user or computer, and the whole thing going wrong from there, but that was a week or two back and other work got in the way, the test debug script was lost, so I can't confirm if this is 100% true or not. For that I apologise.
Other printers that contain computer item level targets, such as:
<SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="School 2-001 Library" status="5389P900037" image="2" changed="2021-05-21 11:44:59" uid="{69EA519F-D6EF-4C56-9DF3-5760857F5DD4}" userContext="1" bypassErrors="1"><Properties action="U" comment="" path="\\printer.school.domain\5389P900037" location="" default="1" skipLocal="0" deleteAll="0" persistent="0" deleteMaps="0" port=""/><Filters><FilterOrgUnit bool="AND" not="0" name="OU=Library,OU=Pupils,OU=School1,OU=Secondary,OU=Computers,OU=Schools W10,DC=School,DC=Domain" userContext="0" directMember="0"/><FilterGroup bool="AND" not="0" name="School\G SchoolStudents" sid="S-1-5-21-2723276998-1066919283-1565034395-81442" userContext="1" primaryGroup="0" localGroup="0"/></Filters></SharedPrinter>
work as intended. I'm thinking this may have something to do with the string length of the OUs in question?
The full XML code of printers I've been testing against is attached to this post, along with screen grabs of example printer targeting.
Not Working
Working
If you have any further questions, need clarification on any point, or need me to assist in running tests or debugging, I am more than happy to help.
Cheers,
Mark
printers.txt