Option Explicit
On ERROR RESUME Next
Dim wshShell, wshNetwork, objDomain, objTrans, objPrinters
Dim strComputerDN, strComputerOU, strNum
Dim arrSplitDN
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
Set wshShell = CreateObject("WScript.Shell")
Set wshNetwork = CreateObject("WScript.Network")
Set objDomain = getObject("LDAP://rootDse")
Set strComputerOU = Nothing
' 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 "$".
Set objTrans = CreateObject("NameTranslate")
objTrans.Set ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_NT4, wshNetwork.UserDomain & "\" _
& wshNetwork.computerName & "$"
strComputerDN = objTrans.Get(ADS_NAME_TYPE_1779)
'WScript.Echo strComputerDN
' Split the DN on the comma delimeter
arrSplitDN = Split(strComputerDN, ",")
' Determine where computer is in AD. If computer account is in computers,
' then error and quit. If computer is a laptop determine IP subnet.
If UCase(arrSplitDN(1)) = "CN=COMPUTERS" Then
WScript.Echo "Computer is in Computers Container. No printers" _
& " assigned. Contact your administrator."
WScript.Quit
Elseif UCase(arrSplitDN(1)) = "OU=DOMAIN CONTROLLERS" Then
WScript.Echo "Computer is a Domain Controller. Exiting Script."
WScript.Quit
Else
'Combine OU entries back into one entry delimeted with comma.
For strNum = 0 To UBound(arrSplitDN)
If Left(arrSplitDN(strNum),3) = "OU=" Then
If strComputerOU = "" Then
strComputerOU = arrSplitDN(strNum)
Else
strComputerOU = strComputerOU & "," & arrSplitDN(strNum)
End If
End If
Next
End If
'Remove old network printers
'Enumerate all printers first, then delete all network printers
Set objPrinters = wshNetwork.EnumPrinterConnections
For strNum = 0 To objPrinters.Count - 1 Step 2
If Left(objPrinters.Item(strNum +1),2) = "\\" Then
wshNetwork.RemovePrinterConnection objPrinters.Item(strNum +1),True,True
End If
Next
strComputerOU = UCase(strComputerOU)
'Assign printers based on OU membership
Select Case strComputerOU
Case "OU=COMPUTERS,OU=NORTH,OU=BUILDING40"
wshNetwork.AddWindowsPrinterConnection "\\Server1\Printer1"
Case "OU=COMPUTERS,OU=SOUTH,OU=BUILDING40"
wshNetwork.AddWindowsPrinterConnection "\\Server1\Printer2"
End Select