Link to home
Start Free TrialLog in
Avatar of PantoffelSlippers
PantoffelSlippersFlag for South Africa

asked on

Get all AD groups in one OU

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
Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

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.
Avatar of PantoffelSlippers

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Robert - let me try that
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....
Robert,

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

Thanks
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...
It's what I wanted - and it works - thanks