Solved

Get all AD groups in one OU

Posted on 2013-12-20
8
334 Views
Last Modified: 2013-12-29
Hi experts,

I'm trying to query Active directory using VB.Net on .Net Framework 3.5

I need to get all security groups in a specific OU.

I've got:

Dim searcher As New DirectorySearcher()
searcher.Filter = "(&(Objectcategory=group)(OU=ThisIsTheOUThatIWant,OU=Groups,OU=SecondLevel,OU=CompanyLevel))"

It's not working.  I get 0 results.  I tried variations but none works.

I have not written code at all in the last 5 years - only started again now in the last week.

Please help me to get this working.

Thanks
0
Comment
Question by:PantoffelSlippers
  • 5
  • 2
8 Comments
 
LVL 53

Expert Comment

by:Will Szymkowski
ID: 39733537
If you are not opposed to use Powershell to get your results it can be done very easy. See below for syntax...
Import-module activedirectory
Get-ADGroup -Filter * -SearchBase "OU=testou,DC=domain,DC=com" | select Name, GroupCategory | ft -autosize

Open in new window


The command above will return all security/distribution groups in the "testou". For yours change the OU and also DC=yourdomain,DC=com.

Will.
0
 

Author Comment

by:PantoffelSlippers
ID: 39734490
Thanks Will,

In my other post it worked.

In this case though, it forms part of a larger system/application so I need to incorporate it into a .Net application.
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 300 total points
ID: 39741869
I think you need a bit more code. Here's a rewrite of some code I used in another project. Take from it what you need, but I suggest you start by setting the correct root path (like previous poster, I would expect something containing ",DC=yourdomain,DC=com" for example) and examine the output.
            Dim strFilter As String = "(&(objectCategory=group)(groupType:1.2.840.113556.1.4.803:=2147483648))" ' filter: only security groups ' found: http://msdn.microsoft.com/en-us/library/windows/desktop/aa746475(v=vs.85).aspx
            Dim arrstrPropertiesToLoad() As String = "ADsPath,name,dn,displayName,description".Split(",")
            Dim searcher As New DirectorySearcher(New DirectoryEntry("LDAP://OU=ThisIsTheOUThatIWant,OU=Groups,OU=SecondLevel,OU=CompanyLevel"), strFilter, arrstrPropertiesToLoad, SearchScope.OneLevel)
            Dim objSearchResultCollection As SearchResultCollection = searcher.FindAll
            Console.WriteLine("** total results found: " & objSearchResultCollection.Count)
            For Each objSearchResult As SearchResult In objSearchResultCollection
                Console.WriteLine(" * result found: " & objSearchResult.Path)
                For Each strPropertyName As String In arrstrPropertiesToLoad
                    Dim strPropertyValue As String = String.Empty
                    Try
                        If objSearchResult.Properties.Contains(strPropertyName) Then
                            If objSearchResult.Properties(strPropertyName).Count > 0 Then
                                strPropertyValue = objSearchResult.Properties(strPropertyName).Item(0)
                                'strPropertyValue = strPropertyValue.Replace(vbCrLf, ", ")
                            End If
                        End If
                        Console.WriteLine("   " & strPropertyName & " = " & strPropertyValue)
                    Catch ex As Exception
                        Console.WriteLine("--- Error on " & strPropertyName & ": " & ex.Message)
                    End Try
                Next
            Next

Open in new window

0
 

Author Comment

by:PantoffelSlippers
ID: 39743356
Thanks Robert - let me try that
0
 

Author Comment

by:PantoffelSlippers
ID: 39743375
Robert,

I'm getting the following error:

A first chance exception of type 'System.DirectoryServices.DirectoryServicesCOMException' occurred in System.DirectoryServices.dll

on

Dim objSearchResultCollection As SearchResultCollection = searcher.FindAll


I'm trying to troubleshoot now....
0
 

Author Comment

by:PantoffelSlippers
ID: 39743380
Robert,

Got it running - let me wrap my head around results.

Thanks
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39743646
The whole block of looping through the results I constructed once because I was gettting some strange results, something to do with multi-valued fields and possibly typing although I've always only used strings. But something like Properties("name") just doesn't fly...
0
 

Author Closing Comment

by:PantoffelSlippers
ID: 39744585
It's what I wanted - and it works - thanks
0

Join & Write a Comment

Do you have users whose passwords are expiring and they are constantly calling you?  Well I sure did and needed a way to put an end to this.  We have a lot of remote users which would not be notified that their passwords were expiring since they wer…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

743 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

15 Experts available now in Live!

Get 1:1 Help Now