Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get all AD groups in one OU

Posted on 2013-12-20
8
Medium Priority
?
367 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
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 to another domain controller. Log onto the new domain controller with a user account t…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

610 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