Rich wrote:
Quote:
>I have users spread out in Active Directory under 20 sub-OU's of a Users
>OU.
> What I'd like to do is have a GPO on Users call a script that will map
> different printers based on what sub-OU the user account is in. Does
> anyone
> have a script that will do that? I'm assuming its possible to do.
It's more common to map printers according to group membership. To determine
the parent OU you can use the Parent method of the user object, which
returns the Distinguished Name of the OU. For example:
========
Option Explicit
Dim objSysInfo, strUserDN, objUser
Dim strParentDN, objNetwork
' Retrieve DN of current user.
Set objSysInfo = CreateObject("ADSystemInfo")
strUserDN = objSysInfo.UserName
' Escape any forward slash characters.
strUserDN = Replace(strUserDN, "/", "\/")
' Bind to current user object.
Set objUser = GetObject("LDAP://" & strUserDN)
' Retrieve DN of parent OU/Container.
strParentDN = objUser.Parent
' Map printers according to parent OU.
Set objNetwork = CreateObject("Wscript.Network")
Select Case LCase(strParentDN)
Case "ou=west,ou=users,dc=mydomain,dc=com"
objNetwork.AddWindowsPrinterConnection "\\PrintServer1\HPLaser2"
objNetwork.SetDefaultPrinter "\\PrintServer1\HPLaser2"
Case "ou=east,ou=users,dc=mydomain,dc=com"
objNetwork.AddWindowsPrinterConnection "\\PrintServer2\HPLaser3"
objNetwork.SetDefaultPrinter "\\PrintServer2\HPLaser3"
End Select
=======
Using the DN of the parent OU makes sure the name is unique. You can also
parse for the Relative Distinguished Name (RDN), but that may not be unique.
Code to parse for the RDN could be similar to:
=========
Dim objSysInfo, strUserDN, objUser
Dim strOUPath, arrContainers, arrOU, strOU
' Retrieve DN of current user.
Set objSysInfo = CreateObject("ADSystemInfo")
strUserDN = objSysInfo.UserName
' Escape any forward slash characters.
strUserDN = Replace(strUserDN, "/", "\/")
' Bind to current user object.
Set objUser = GetObject("LDAP://" & strUserDN)
' Retrieve DN of parent OU/Container.
strOUPath = objUser.Parent
' Replace any escaped commas with Chr(164).
strOUPath = Replace(strOUPath, "\,", Chr(164))
' Parse Parent DN into comma delimited components.
arrContainers = Split(strOUPath, ",")
' Parse the first component to retrieve name of the OU/Container.
arrOU = Split(arrContainers(0), "=")
strOU = arrOU(1)
' Restore any escaped commas.
strOU = Replace(strOU, Chr(164), "\,")
Wscript.Echo "User is in OU/Container " & strOU
========
Again, you can use a Select Case construction to map printers. If you know
your AD has no DN's with embedded commas or forward slashes you can
eliminate the code that accounts for that, but it doesn't hurt in any case.
--
Richard Mueller
MVP Directory Services
Hilltop Lab -
http://www.rlmueller.net
--