'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 3.1
'
' NAME: PrinterByOU.vbs
'
' AUTHOR: Paul Chapman , Halcyon Dreams
' DATE : 2-4-2006
'
' COMMENT: Map Printers based on the computer's location in AD.
'
'==========================================================================
Dim strOU, arrDN
' Get the DN of the computer
arrDN = Split(GetDN, ",")
' DN will look like: CN=computername,OU=yoursubou,OU=yourou,DC=domain,DC=tld
' Strip the OU section out of the array and make it a string
strOU = GetOU(arrDN)
' Map printers based on OU string (all capital letters)
Select Case strOU
Case "OU=YOURSUBOU,OU=YOUROU"
wshNetwork.AddWindowsPrinterConnection "\\server\printer1"
wshNetwork.AddWindowsPrinterConnection "\\server\printer2"
wshNetwork.AddWindowsPrinterConnection "\\server\printer3"
End Select
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, objNetwork
Set objNetwork = CreateObject("WScript.Network")
' 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, objNetwork.UserDomain & "\" _
& objNetwork.ComputerName & "$"
GetDN = objTrans.Get(ADS_NAME_TYPE_1779)
'Set DN to upper Case
GetDN = UCase(GetDN)
End Function
Function GetOU(DN)
' Select OU's from arrComputerDN and concatenates them using comma
' as a delimiter
Dim Counter
For Each Counter in DN
If Left(arrSplitDN(Counter),3) = "OU=" Then
If GetOU = "" Then
GetOU = DN(Counter)
Else
GetOU = GetOU & "," & DN(Counter)
End If
End If
Next
End Function