[COLOR=green]'==========================================================================
'
' NAME: Printer Mapping by OU (insert for logon.vbs)
'
' AUTHOR: Paul S. Chapman, Halcyon Dreams
' URL: [URL unfurl="true"]http://www.halcyondreams.com[/URL]
' DATE : 6-6-2005
'
' COMMENT:
' Thanks to Richard L. Mueller ([URL unfurl="true"]www.rlmueller.net)[/URL] And
' Mark D. MacLachlan ([URL unfurl="true"]www.thespidersparlor.com)[/URL] for code segments And
' ideas to build this script.
'
'==========================================================================[/color]
Option Explicit
ON ERROR RESUME NEXT
Dim objShell, objNetwork, objDomain, objTrans, objWMI, objNIC
Dim strComputerDN, strComputerOU, strNum, strSubnet
Dim arrSplitDN, arrSplitIP
[COLOR=green]' Constants for the NameTranslate object.[/color]
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objDomain = getObject("LDAP://rootDse")
Set strComputerOU = Nothing
[COLOR=green]' 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 "$". [/color]
Set objTrans = CreateObject("NameTranslate")
objTrans.Set ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_NT4, objNetwork.UserDomain & "\" _
& objNetwork.computerName & "$"
strComputerDN = objTrans.Get(ADS_NAME_TYPE_1779)
'WScript.Echo strComputerDN
[COLOR=green]' Split the DN on the comma delimeter[/color]
arrSplitDN = Split(strComputerDN, ",")
[COLOR=green]' Determine where computer is in AD. If computer account is in computers,
' then error and quit. If computer is a laptop determine IP subnet.[/color]
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=LAPTOPS" Then
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set objNIC = objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each strNum in objNIC
arrSplitIP = Split(strNum.IPAddress(0),".")
Next
strSubnet = arrSplitIP(0) & "." & arrSplitIP(1) & "." & arrSplitIP(2) & ".0"
Else
[COLOR=green]'Combine OU entries back into one entry delimeted with comma.[/color]
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
'WScript.Echo strComputerOU
strComputerOU = UCase(strComputerOU)
[COLOR=green]'Assign printers based on OU membership[/color]
Select Case strComputerOU
Case "OU=COMPUTERS,OU=NORTH,OU=BUILDING40"
objNetwork.AddWindowsPrinterConnection "\\Server\Printer1"
Case "OU=COMPUTERS,OU=SOUTH,OU=BUILDING40"
WSHNetwork.AddWindowsPrinterConnection "\\Server\Printer2"
End Select
[COLOR=green]'Assign printers to laptop users[/color]
Select Case strSubnet
Case "192.168.1.0"
WSHNetwork.AddWindowsPrinterConnection "\\Server\Printer1"
Case "192.168.2.0"
WSHNetwork.AddWindowsPrinterConnection "\\Server2\Printer1"
End Select
WScript.Quit