Solved

VB.NET Active directory user member of

Posted on 2004-08-16
13
549 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now