Solved

Get all AD groups in one OU

Posted on 2013-12-20
8
351 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 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article runs through the process of deploying a single EXE application selectively to a group of user.
This article explains the steps required to use the default Photos screensaver to display branding/corporate images
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

733 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