Created
December 6, 2011 13:42
-
-
Save Arcath/1438247 to your computer and use it in GitHub Desktop.
OU Based Printer script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Printer Script based on AD OU | |
' Written by Adam Laycock (Arcath) | |
' July 2009 | |
' Updated December 2011 | |
' Variables | |
Set WshNetwork = WScript.CreateObject("WScript.Network") ' Network Object | |
Set oPrinters = WshNetwork.EnumPrinterConnections ' Printers Sub Object | |
printServer = "printserver" ' Hostname of your Print Server (add more if you have multiple print servers | |
' 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 | |
' Printer Defs | |
' Write a function for every printer you have, copy functions below and change the printer variable definition. | |
' All printers require a true/false input for weather they should be set as the default printer. | |
' The machines default printer will be the last printer defined as default. | |
Function Printer1(default) | |
printer = "\\" & printServer & "\Printer1" | |
WshNetwork.AddWindowsPrinterConnection printer | |
If default = true Then | |
WshNetwork.SetDefaultPrinter printer | |
End If | |
End Function | |
Function Printer2(default) | |
printer = "\\" & printServer & "\Printer2" | |
WshNetwork.AddWindowsPrinterConnection printer | |
If default = true Then | |
WshNetwork.SetDefaultPrinter printer | |
End If | |
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 | |
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) | |
' Global Printers | |
' Applied to all Computers on site regardless of OU | |
Printer1(true) | |
Printer2(false) | |
Select Case (LCase(strOU)) | |
'For Each OU make a Case | |
'note that the OU Name is made to be all lower case | |
case "office" | |
Printer2(true) | |
case "class room" | |
Printer2(true) | |
End Select |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment