Another Script along similar lines - just runs quicker than the one above - and mixes OU with computer name for mapping.
Code:
Dim wshShell, wshNetwork
Dim strComputerName
Dim computerName
' Create Global Objects
Set wshShell = CreateObject("WScript.Shell")
Set wshNetwork = CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections
On Error Resume Next
' Initialize Variables
strComputerName = wshNetwork.ComputerName
computerName = UCase(strComputername)
Function GetDN()
' Use the NameTranslate object to convert the NT name of the computer to
' the Distinguished name required for the LDAP provider. Computer names
' must end with "$". Returns comma delimited string to calling code.
Dim objTrans, objDomain
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
Set objTrans = CreateObject("NameTranslate")
Set objDomain = getObject("LDAP://rootDse")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_NT4, wshNetwork.UserDomain & "\" _
& strComputerName & "$"
GetDN = objTrans.Get(ADS_NAME_TYPE_1779)
'Set DN to upper Case
GetDN = UCase(GetDN)
End Function
' Delete existing connections to network printers (uncomment to use this)
For i = 0 to oPrinters.Count - 1 Step 2
On Error Resume Next
if Left(oPrinters.Item(i), 3) <> "lpt" And Left(oPrinters.Item(i), 3) <> "usb" then
WshNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true
else WScript.Echo "No network printers found"
end if
Next
'Now define the main Print Server if required - can also use specific names
'such as "\\PS1\Printqueuename" in select statements
Dim printServer
printServer = "PrintServerName"
' Add printer connections dependant upon location (in this case lowest-level OU)
' GetOU is DN from the first "OU=" statement
GetOU = Mid(GetDN,InStr(GetDN,"OU=")+3)
' Now add the printers - NB Case Stements MUST be Unique or more specific lower down the file
' as last match will be actioned - also need to be in UPPERCASE.
Select Case (Left(GetOU, 3))
Case "D&T"
WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\D&T A3 Colour"
WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\D&T Laser"
WshNetwork.SetDefaultPrinter "\\" & printServer & "\D&T Laser"
Case "ART"
WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\Art Printer"
WshNetwork.SetDefaultPrinter "\\" & printServer & "\Art Printer"
End Select
Select Case (Left(GetOU, 4))
Case "BIOL"
WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\Biology Laser"
WshNetwork.SetDefaultPrinter "\\" & printServer & "\Biology Laser"
Case "EXAM"
WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "\Exams Printer"
End Select
'Pick up any random computers and allocate them a printer
'Select Case (computerName) {or Select Case Left(ComputerName,n)
' Case "RANDOM"
' WshNetwork.AddWindowsPrinterConnection "\\" & printServer & "RANDOM PRINTER"
' WshNetwork.SetDefaultPrinter "\\" & printServer & "\RANDOM PRINTER"
'End Select