Re: On ADSI and LDAP



Wouldn't it be easier to do an LDAP search for the samAccountName and return
the distinguishedName attribute?
--
Ed Crowley
MVP - Exchange
"Protecting the world from PSTs and brick backups!"

"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxxxxxxxxxxxxxxxx> wrote in
message news:uC8s2nE3HHA.1204@xxxxxxxxxxxxxxxxxxxxxxx
jimt wrote:

How can I extract a user DN given its sam account name?

I need to put into a variable the user DN in the format
"CN=something,OU=...(nested OUs),DC=....,DC=...,DC=..."

I have nested OUs so I have to use some kind of recursion i guess.


Use the NameTranslate object for this. For example:
============
' Constants for the NameTranslate object.

Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

' Specify the NetBIOS name of the domain and the NT name of the user

' (The same as the value of the sAMAccountName attribute).
strNTName = "MyDomain\TestUser"

' Use the NameTranslate object to convert the NT user name to the
' Distinguished Name required for the LDAP provider.
Set objTrans = CreateObject("NameTranslate")

' Initialize NameTranslate by locating the Global Catalog.
objTrans.Init ADS_NAME_INITTYPE_GC, ""
' Use the Set method to specify the NT format of the object name.
objTrans.Set ADS_NAME_TYPE_NT4, strNTName

' Use the Get method to retrieve the RPC 1779 Distinguished Name.
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)



' Bind to the user object in Active Directory with the LDAP provider.
Set objUser = GetObject("LDAP://"; & strUserDN)

============



For more see this link:



http://www.rlmueller.net/NameTranslateFAQ.htm


--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--




.