Solved

Get all AD groups in one OU

Posted on 2013-12-20
8
354 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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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
This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

724 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