Solved

Getting  the target  OU in Active Directory

Posted on 2012-03-28
22
456 Views
Last Modified: 2012-03-31
I am trying to get the groups out of Active Directory - I can get them all, thats no problem
What I am trying to do is get a list of groups out of a target directory
For example LDAP://COMPANY.COM/AN-OU/TARGET-OU
The code I am using is this

    Public Shared Function GetAllGroups() As DataSet
       
        Dim dsGroup As New DataSet()
        Dim dirEntry As DirectoryEntry = New DirectoryEntry("LDAP://COMPANY.com", "USERNAME", "PASS")
        'create instance fo the direcory searcher
        Dim dirSearch As New DirectorySearcher()
        'set the search filter
        dirSearch.SearchRoot = dirEntry
        'deSearch.PropertiesToLoad.Add("cn");
        'dirSearch.Filter = ("(OU=AN-OU,OU=TAGET_OU)")
        dirSearch.Filter = "(objectClass=group)" '(&(objectClass=group)(cn=CS_*)) check on this!!!
        'dirSearch.Filter = "(&(OU=AN_OU,OU=TARGET-OU,DC=COMPANY,DC=com)"
        'dirSearch.SearchScope = SearchScope.Subtree

        'find the first instance
        Dim searchResults As SearchResultCollection = dirSearch.FindAll()
        'Create a new table object within the dataset
        Dim dtGroup As DataTable = dsGroup.Tables.Add("Groups")
        dtGroup.Columns.Add("GroupName")
        'if there are results (there should be some!!), then convert the results
        'into a dataset to be returned.
        If searchResults.Count > 0 Then
            'iterate through collection and populate the table with
            'the Group Name
            For Each Result As SearchResult In searchResults
                'set a new empty row
                Dim drGroup As DataRow = dtGroup.NewRow()
                'populate the column
                drGroup("GroupName") = Result.Properties("cn")(0)
                'append the row to the table of the dataset
                dtGroup.Rows.Add(drGroup)
            Next
        End If
        Return dsGroup
    End Function

Open in new window

0
Comment
Question by:r3nder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
  • 2
22 Comments
 
LVL 23

Expert Comment

by:yo_bee
ID: 37779794
First and for most your Invoking of the LDAP:// is correct.

LDAP syntax should be LDAP://dc=company,dc=com  and if you are looking for an OU your would do it like this LDAP://OU=Target,DC=Company,DC=COM

If you are just looking for a group you can use DSQUERY.

You can also use DSQUERY Group  or DSGET Group to output the Distinguished names.
0
 
LVL 6

Author Comment

by:r3nder
ID: 37779945
I have a hierarchy pic of the OU, but are you saying
Dim dirEntry As DirectoryEntry = New DirectoryEntry("LDAP://OU=Target,DC=Company,DC=COM", "USERNAME", "PASS")
or what
Capture.JPG
0
 
LVL 23

Expert Comment

by:yo_bee
ID: 37780105
Your ldap syntax should LDAP:// OU=Target OU ,OU=AN OU,DC=company,DC=com

You work backwards. The lowest in the hierarchy to the highest. Where .com is your last in the list.

Have you tried dsquery ?
0
Office 365 Training for Admins

Learn how to provision tenants, synchronize on-premise Active Directory, and implement Single Sign-On with these master level course.  Only from Platform Scholar

 
LVL 6

Author Comment

by:r3nder
ID: 37780124
not yet I am trying to figure out with my function how I would use that
0
 
LVL 23

Accepted Solution

by:
yo_bee earned 500 total points
ID: 37780160
Here is a snippet from one of my codes. I do not have it modified for where you want to pass the values.

 Private Sub SearchAD()

        
        Dim objSchemaContainer
       
        
        objSchemaContainer = GetObject("LDAP://OU=target OU,OU=AN OU,DC=company,DC=com")



        Dim objSearch As New DirectorySearcher()
        objSearch.SearchRoot = New DirectoryEntry(objSchemaContainer)
        objSearch.Filter = "(&(objectCategory=group))"
        objSearch.SearchScope = SearchScope.Subtree
        objSearch.PropertiesToLoad.Add("cn")
        Dim colQueryResults As SearchResultCollection
        colQueryResults = objSearch.FindAll()
        Dim objResult As SearchResult
        For Each objResult In colQueryResults
            ComboBox2.Items.Add(objResult.Properties("cn")(0))
        Next

    End Sub

Open in new window

0
 
LVL 23

Expert Comment

by:yo_bee
ID: 37780167
Also why not use the querys saved feature in ADUC.
You can accomplish this and see the query that is being built to meet your needs.
0
 
LVL 6

Author Comment

by:r3nder
ID: 37780168
I will  have a chance to test it tomorrow - I will let you know - thanks for all your help yo_bee
0
 
LVL 23

Expert Comment

by:yo_bee
ID: 37780171
No problem
0
 
LVL 6

Author Comment

by:r3nder
ID: 37780183
Cool - that is a neet tool - I will use it first thing in the morning  - thanks for your wealth of knowledge
0
 
LVL 2

Expert Comment

by:MilesLogan
ID: 37781632
With this free tool , you can select the Groups option , then select All groups , then select the OU and choose the Attributes that you need in the report .

http://www.cjwdev.co.uk/Software/ADReportingTool/Info.html
0
 
LVL 6

Author Comment

by:r3nder
ID: 37781981
Excellect person, knowledgable and the solution was perfect
0
 
LVL 23

Expert Comment

by:yo_bee
ID: 37784701
@r3nder

Did you even try to apply the VB code snippet, DSQUERY or even my suggestion for ADUC?
0
 
LVL 6

Author Comment

by:r3nder
ID: 37785003
I apologize yo_bee I had intended that the credit go to you, I have notified the moderators and have asked them to give YOU the points
Once again I appologize
R3nder
@MilesLogan
Sorry man my fault yo_bee did all the work
0
 
LVL 23

Expert Comment

by:yo_bee
ID: 37785016
It's ok, I was just curious if any of the solutions I suggested worked?

If so which one
0
 
LVL 6

Author Comment

by:r3nder
ID: 37785029
I  changed the LDAP to what you suggested and changed the filter to what you suggested in the previous code snippet - worked like a charm - I will do my utmost to ensure you get the points - also that part about expert and knowledgable was to you
Thanks Yo_Bee
0
 
LVL 23

Expert Comment

by:yo_bee
ID: 37785211
Thank you, but if not I have enough. Just glad you confirmed what worked.
0
 
LVL 2

Expert Comment

by:MilesLogan
ID: 37785317
all good with me , I get so much help from people here not really an issue with me about points ..

 I will try his option also when I have time .
0
 
LVL 23

Expert Comment

by:yo_bee
ID: 37785367
I am going to look into the link recommend.
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 37790985
Thank you yo_bee
0
 
LVL 23

Expert Comment

by:yo_bee
ID: 37790996
Where did you get the original script from?
0
 
LVL 6

Author Comment

by:r3nder
ID: 37791708
I found it on the web - There is another question of mine you might want to chime in on (getting the group GUID at the same time)
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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