?
Solved

Get all AD groups in one OU

Posted on 2013-12-20
8
Medium Priority
?
363 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
[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
  • 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 1200 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
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

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

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

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…
Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

765 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