Solved

Finding Users RDN

Posted on 2004-08-11
2
334 Views
Last Modified: 2010-05-18
How do I search AD to find out a users RDN?  I want to create a vbscript that goes out and finds a users RDN.  Problem is that the user may be in a OU or a CN (will be used to query different users)

I know I need to bind to AD, then query the user.  Do I have to go through the GC? or can i just do an LDAP call?
0
Comment
Question by:randyt200
2 Comments
 
LVL 14

Expert Comment

by:dlwyatt82
ID: 11778284
You can search the directory for a partcular user, and then read the user's IADs::Name property to get the RDN (I believe there is also an IADs::Get_Name method to retrieve this data). See the following section of the Active Directory Programmer's Guide at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ad/ad/searching_active_directory.asp for information on how to perform a search of the directory with a query.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/iadsuser.asp details the properties and methods of IADsUser, including those it inherits from IADs (such as Name and Get_Name).
0
 
LVL 15

Accepted Solution

by:
Colosseo earned 125 total points
ID: 11783041
Hi

The following function is something I have been using for a while... cant remember where i found it now. It Starts at the root of the Active Directory and goes from there so it wont matter where the user is... he or she will be found :)

Used in conjunction with the information in the links from dlwyatt82 it might get you started

Bit to pay attention to is sAttribsToReturn parameter as this is the field that will be returned if the user is found ( i believe cn is equivalent to RDN)

' This function returns the users email address if found
Function get_Info(strName)

    ' Create ADO connection object for Active Directory
    Set oConn = CreateObject("ADODB.Connection")
    oConn.Provider = "ADsDSOObject"
    oConn.Open "Active Directory Provider"
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConn
   
    ' Get the ADsPath for the domain to search.
    Set oRoot = GetObject("LDAP://rootDSE")
    sDomain = oRoot.Get("defaultNamingContext")
    Set oDomain = GetObject("LDAP://" & sDomain)
   
    ' Build the ADsPath element of the commandtext
    sADsPath = "<" & oDomain.ADsPath & ">"

    ' Build the filter element of the commandtext
    If (strName = "") Then
        sFilter = "(&(objectCategory=person)(objectClass=user))"
    Else
        sFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & strName & "))"
    End If
 
    ' Build the returned attributes element of the commandtext.
    sAttribsToReturn = "cn"
 
    ' Build the depth element of the commandtext.
    sDepth = "subTree"
    oCommand.CommandText = sADsPath & ";" & sFilter & ";" & sAttribsToReturn & ";" & sDepth

    ' Execute the query.
    Set oRS = oCommand.Execute
     
    ' If a record is found then return the email address
    If Not oRS.EOF Then
      get_Email = oRS.Fields(0).Value
    End If
   
    ' Tidy up
    oRS.Close
    Set oRS = Nothing
    Set oDomain = Nothing
    Set oRoot = Nothing
    Set oCommand = Nothing
    Set oConn = Nothing
   
End Function

HTH

Scott
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Raising Domain/Forest Level to Windows 2003 (from a retired Windows 2000 DC) 8 148
Server 2012R2 Foundation and Server 2000 3 124
Screen Mirroring 7 66
Can’t delete a file 14 161
NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question