Solved

VB.NET Active directory user member of

Posted on 2004-08-16
13
559 Views
Last Modified: 2008-02-01
How can I get all the groups a member is in?

We will Call the member FooBar
0
Comment
Question by:Vartana
  • 7
  • 3
  • 3
13 Comments
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11812639
There is a "memberOf" property in the user DirectoryEntry, you can iterate over the array of objects in the property like this

You loop this array of object, convert to string and it will give you all the groups he belongs to.
0
 
LVL 1

Author Comment

by:Vartana
ID: 11812663
Example please ?
0
 
LVL 3

Expert Comment

by:DJ_Back-Q
ID: 11812680
This is what I had for example. It's in C-Sharp... pertty easy to convert in VB .NET

DirectoryEntry user = new DirectoryEntry("LDAP://server/CN=Administrator,CN=Users,DC=xxx,DC=yyyy,DC=zzzz");
PropertyCollection pcoll = user.Properties;
for (int i = 0; l < pcoll["memberOf"].Count; i++)
Console.WriteLine("\t" + (pcoll["memberOf"])[i].ToString());
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:Vartana
ID: 11812809
Can you help me convert it?
I tried but was not successful
0
 
LVL 3

Assisted Solution

by:DJ_Back-Q
DJ_Back-Q earned 100 total points
ID: 11812867
Dim user As New DirectoryEntry("LDAP://server/CN=Administrator,CN=Users,DC=xxx,DC=yyyy,DC=zzzz");

Dim pcoll As PropertyCollection = user.Properties

Dim iCounter As Integer

For iCounter = 0 To pcoll("memberOf").Count - 1

        MessageBox("Group " & pcoll("memberOf")(iCounter).ToString())

Next



Tell me if it works

Andre
MCSD
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 400 total points
ID: 11812884
Dim user As DirectoryEntry = New DirectoryEntry ("LDAP://server/CN=Administrator,CN=Users,DC=xxx,DC=yyyy,DC=zzzz")
Dim pcoll As PropertyCollection = user.Properties

Dim i As Integer = 0
While l < pcoll("memberOf").Count
 Console.WriteLine("" + Microsoft.VisualBasic.Chr(9) + "" + (pcoll("memberOf"))(i).ToString())
 i += 1
End While

Bob
0
 
LVL 1

Author Comment

by:Vartana
ID: 11813049
no luck

Maybe this will help our domains name is

Jamba.net
0
 
LVL 1

Author Comment

by:Vartana
ID: 11813074
i think the LDAP string is wrong
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11813077
All you really need is server name:

Dim user As New DirectoryEntry("LDAP://server/cn=Users")

Bob
0
 
LVL 1

Author Comment

by:Vartana
ID: 11813094
i did that and i get

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in system.directoryservices.dll

Additional information: An operations error occurred
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11813335
What is the code that you have now?

Bob
0
 
LVL 1

Author Comment

by:Vartana
ID: 11813359
   Public Sub GetADGroup()
        Dim user As DirectoryEntry = New DirectoryEntry("LDAP://Jamba.net/DC=Jamba,DC=com")
        Dim pcoll As PropertyCollection = user.Properties
        Dim i As Int32 = 0

        While i < pcoll("memberOf").Count
            Debug.Write("" + Microsoft.VisualBasic.Chr(9) + "" + (pcoll("memberOf"))(i).ToString())
            i += 1
        End While
    End Sub
0
 
LVL 1

Author Comment

by:Vartana
ID: 11815563
OK i figured this out

Final Code

    Public Function GetAdInfo(ByVal UserName As String) As String()
        Dim entry As New DirectoryEntry("LDAP://computername/CN=Users,DC=domain,DC=net")
        Dim mySearcher As New DirectorySearcher(entry)
        Dim Groups() As String

        mySearcher.Filter = "(samaccountname=" & UserName & ")"
        mySearcher.PropertiesToLoad.Add("memberof")

        Dim ResEnt As SearchResult = mySearcher.FindOne()
        If (ResEnt Is Nothing) Then
            MsgBox("You are not set-up in domain controller")
            Exit Function
        End If

        ReDim Groups(0)
        Dim prop_value As String
        For Each prop_value In ResEnt.Properties("memberof")
            'Debug.Write(Replace(Replace(prop_value, ",CN=Users,DC=pcs1,DC=net", ""), "CN=", ""))
            ReDim Preserve Groups(UBound(Groups) + 1)
            Groups(UBound(Groups)) = Replace(Replace(prop_value, ",CN=Users,DC=pcs1,DC=net", ""), "CN=", "")
        Next
        GetAdInfo = Groups
    End Function
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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