Learn how to a build a cloud-first strategyRegister Now


Getting AD Group memebrship from just a username

Posted on 2006-04-10
Medium Priority
Last Modified: 2011-08-18
hOw can I get a user's AD group membership form just his username. All the scripts I have seen require a full LDAP name and in the situation I am in I shan't know which OU a user is a member of.
Question by:cescentman
  • 2
LVL 11

Accepted Solution

star_trek earned 720 total points
ID: 16419846
You can get the LDAP name from the username. To do that:

First you will get the distinguished name of the user: For that you have to write the function

Public Function UserNameDN(sUserName)
     set oRootDSE = GetObject("LDAP://rootDSE")
   Set oConnection = CreateObject("ADODB.Connection")
   oConnection.Open "Provider=ADsDSOObject;"
   Set oCommand = CreateObject("ADODB.Command")
   oCommand.ActiveConnection = oConnection
   oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & ">;(&(objectCategory=User)(samAccountName=" & _
              sUserName & "));distinguishedName;subtree"
   Set oRecordSet = oCommand.Execute
        UserNameDN = oRecordSet.Fields("distinguishedName")
        If Err <> 0 Then
          outFile.WriteLine "There was an error getting the DN for " & strUser & "." & "    " & Err.Num
          strBadCount = strBadCount + 1
        End If
        On error Goto 0

And after you find the DN name the LDAP name would be
LDAP://DC Name/UserNameDN
LVL 23

Assisted Solution

TheCleaner earned 280 total points
ID: 16420240
Use dsquery and dsget:

dsquery user -name USERNAME | dsget user -memberof

the only variable is USERNAME...put in the username of the person you want.  So for Joe Smith:

dsquery user -name "Joe Smith" | dsget user -memberof

Author Comment

ID: 16421307
LVL 23

Expert Comment

ID: 16421390

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Organizations create, modify, and maintain huge amounts of data to help their businesses earn money and generally function.  Typically every network user within an organization has a bit of disk space to store in process items and personal files.   …
I guess it is not common knowledge to most Wintel engineers/administrators: If you have an SNMP-based monitoring system in your environment (and it's common to have SNMP or Syslog) it's reasonably easy to enable monitoring of the Windows Event logs,…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

810 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