Mapping Printers based on Computer OU (via Script)

KB ID 0000645 

Problem

Location based printing has long been a pain. In the past I’ve tackled it with Group Policy Preferences, and I’ve even gone ‘old school’ and mapped printers with con2prt.

A few months ago I put in a new network at a school, they were using a vbs script to deploy all their classroom printers, and I had a quick (unsuccessful) attempt to do the same. But time was against me and I used GPP and location variables to solve the problem.

I did however take a copy of the script to have a play with, so yesterday while it was quiet I dropped a copy on the test network, and failed again! So I trawled round the internet and cobbled together a new script which works they way I wanted.

Note: Please do not email me and ask “Can you change the scripts to do xyz” you probably know as much about vbs as I do!

Solution

Requirements

1. I want the script to run and map the printers based on the OU that the computer is in, in these example I’ve only got two OU’s, but in a live environment you might want all the computers in the maths classroom to get the black and white laser printer in that classroom as the default printer and also be connected to the colour printer in the same room.

Multiple OU's

2. On my test network I’ve only got two printers, an HP 4600 Colour Laser, and an HP 3055 multifunction printer, so to illustrate how the script works I’ll map both printers to the computers in both OU’s, but I’ll change the default printer for OU1 and OU2. Both these printers are already setup and installed on my server.

Note: You may need to add x64 AND x32 bit drivers to your printers if you have a mix of client operating systems, as they download the driver from the server.

Printers On The Server

Script to Map Printers Based on OU

3. This script will remove any mapped network printers, Note: Local printers are NOT removed. It will then connect the printers you require for each OU. Lastly it will set the default printer.

Note: You need to connect the printer before you can set it as default.

'=========================================================================<br />
' MAP PRINTERS BASED ON OU<br />
'<br />
' AUTHOR:  PeteLong<br />
' COMPANY: www.petenetlive.com<br />
' DATE:    03/08/12<br />
'=========================================================================<br />
Set objSysInfo = CreateObject("ADSystemInfo")<br />
strName = objSysInfo.ComputerName</p>
<p>arrComputerName = Split(strName, ",")<br />
arrOU = Split(arrComputerName(1), "=")<br />
strComputerOU = arrOU(1)</p>
<p>Set objNetwork = CreateObject("WScript.Network")</p>
<p>'=========================================================================<br />
'STEP 1 - Remove any NETWORK printers (NOT Local Printers)<br />
'=========================================================================</p>
<p>Set WshNetwork = WScript.CreateObject("WScript.Network")<br />
Set Printers = WshNetwork.EnumPrinterConnections</p>
<p>For i = 0 to Printers.Count - 1 Step 2</p>
<p>    If Left(ucase(Printers.Item(i+1)),2) = "" Then<br />
        WSHNetwork.RemovePrinterConnection Printers.Item(i+1)<br />
    End IF<br />
Next</p>
<p>'=========================================================================<br />
'STEP 2 - Connect Printers based on COMPUTER OU membership<br />
'=========================================================================</p>
<p>Select Case strComputerOU<br />
    Case "OU1"<br />
        objNetwork.AddWindowsPrinterConnection "PNL-DC3055"<br />
        objNetwork.AddWindowsPrinterConnection "PNL-DC4600"<br />
        objNetwork.SetDefaultPrinter "PNL-DC4600"<br />
    Case "OU2"<br />
        objNetwork.AddWindowsPrinterConnection "PNL-DC3055"<br />
        objNetwork.AddWindowsPrinterConnection "PNL-DC4600"<br />
        objNetwork.SetDefaultPrinter "PNL-DC3055"<br />
End Select

What you would need to change

Simply change PNL-DC for the name of your print server, add your OU’s and printers, you would just add a new ‘case’ for each OU you require.

4. I’m deploying this script as a USER logon script, though If you wanted you could also use a COMPUTER startup script.

Group Policy Logon Script

What computers in OU1 would see

Printers in OU1

What computers in OU2 would see

Printers in OU2

Related Articles, References, Credits, or External Links

NA

Author: Migrated

Share This Post On