Printer Script that uses a machines AD OU
In this topic I asked how to find out what Active directory OU and Machine was in.
Using a bit of code from the posts there and a Regex function i found i wrote this:
Code:
'Printer Script based on AD Group
'Written by Adam Laycock (Arcath)
'July 2009
'Variables
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections
printServer = "SERVERNAME" 'Set this to the machine that is your Print Server
DefaultColourPrinter = "\PRINTERNAME" 'The Main Colour Printer
DefaultBWPrinter = "\PRINTERNAME" 'The Main Black and White Printer
'Functions
'Get First Match
'Returns the First REGEX Match for the pattern you supply
'from http://www.somacon.com/p138.php
Function GetFirstMatch(PatternToMatch, StringToSearch)
Dim regEx, CurrentMatch, CurrentMatches
Set regEx = New RegExp
regEx.Pattern = PatternToMatch
regEx.IgnoreCase = True
regEx.Global = True
regEx.MultiLine = True
Set CurrentMatches = regEx.Execute(StringToSearch)
GetFirstMatch = ""
If CurrentMatches.Count >= 1 Then
Set CurrentMatch = CurrentMatches(0)
If CurrentMatch.SubMatches.Count >= 1 Then
GetFirstMatch = CurrentMatch.SubMatches(0)
End If
End If
Set regEx = Nothing
End Function
'Remove all the Network Printers from the Machine
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
'Connect to AD
Set objSysInfo = CreateObject("ADSystemInfo")
'Get LDAP entry to current computer object.
strComputerDN = objSysInfo.ComputerName
Set objComputer = GetObject("LDAP://" & strComputerDN)
strOU = GetFirstMatch("OU=(.*?),OU=", strComputerDN)
Select Case (LCase(strOU))
'For Each OU make a Case
'note that the OU Name is made to be all lower case
case "room1"
WshNetwork.AddWindowsPrinterConnection "\\" & printServer & DefaultColourPrinter
WshNetwork.SetDefaultPrinter "\\" & printServer & DefaultColourPrinter
case "room2"
WshNetwork.AddWindowsPrinterConnection "\\" & printServer & DefaultBWPrinter
WshNetwork.AddWindowsPrinterConnection "\\" & printServer & DefaultColourPrinter
WshNetwork.SetDefaultPrinter "\\" & printServer & DefaultBWPrinter
End Select
First off it deletes all network printer connections from a machine (needed this to clean up from the old script) This helps if any kids happen to add any persistent connections (e.g. with a script of thier own).
It then creates a connection to Active Directory and gets the computers AD String (Normally CN=COMPUTERNAME,OU=OU1,OU=OU2,DC=DOMAIN,DC=DOMAIN)
This string is then passed through a regex function that returns the first OU for that computer, in my case this is where the OU for each room was.
It then has a big case statement for each ou (you have to write each case)
it would be possible to do away with the case statement and have it add a printer with the same name as the OU assuming that your printers where named like that
I Have tested this on a couple of domains and it works as i expected it to
Another Printer script...
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