14
December
2011

OU Based Printer Script

Tagged: VBS Domains Windows
Comments
Facebook_share Twitter_share Reddit_share
A while ago I wrote a printer script that used LDAP to find out what OU the computer was in and then connect the printers you had set for that machine. I stopped working on it when Windows Server 2008 became the standard server in my schools as the GPO Preferences it has allow for the same functionality and more, tell but recently I've had a couple of my 2003 schools want more control over their printing so it's come back under the spotlight and has been Improved!



The script is split into 6 sections, Variables, Functions, Printer Defs, Clean Up, AD and Adding.

Variables

There are only 3 variables in the script:
  • WshNetwork - The windows scripting network object
  • oPrinters - The Printers Sub Object
  • printServer - The host name of your print server


You can always add more print servers but in most cases 1 should be enough.

Functions

There is only one function in the script which came from http://www.somacon.com/p138.php which finds the first match for a regex pattern in a string. It's used to find the OU that the computer is in from is LDAP string.

Printer Defs

This section's size is dependent on how many printers you have on site. You need to define a function for every printer you want to use, I elected to do it this way to tidy up the case statement later on in the code. To write these functions copy one of the same functions and edit it, the printers variable at the start needs to be set to the printers path and the rest can be left.

All these functions require true or false to be passed to them to control weather the printer is set as the default. You can set the default multiple times and only the last one set will be the default.

Clean Up

The next step is to remove all network printers from the machine, this makes sure that the only printers the computers has are the ones set by the script.

This will remove all network printers from the computer! If you have any printers manually added comment this section out

Active Directory

The script now gets the computers LDAP string from Active Directory and runs a regex pattern on it to get the last OU.

Adding

Any functions you call outside the case statement will be applied to all computers, this is useful for your main printers, or backup printers.

You then need to add a case for every OU you want specific printers for. The OU name is down cased so there's no need to worry about capitals etc... You can ommit any OUs which don't need a custom printer.
blog comments powered by Disqus
Html5-badge-h-css3-graphics-semantics
CSS Based on Twitters Bootstrap

88x31
All Content is written by Adam Laycock (Arcath) unless otherwise stated.

A little bit About Me

I am an IT Technician working in Primary Schools, I spend my time outside work writing programs, normally they end up bieng bots and other web based dodads