Skip to content

A Recursive SQL Query for Active Directory Group

May 5, 2010
--Get all members of a group
select cn,AdsPath
from openquery (ADSI, '<LDAP: dc="corp,dc=mycorp,dc=com">;
(&(objectCategory=person)
(memberOf:1.2.840.113556.1.4.1941:=CN=Administrators,CN=Builtin,DC=corp,DC=mycorp,DC=com));
cn, adspath;subtree')
order BY cn;

--get all groups a user is a member of
select cn,AdsPath
from openquery (ADSI, '<LDAP: dc="corp,dc=mycorp,dc=com">;
(&(objectClass=group)(member:1.2.840.113556.1.4.1941:=CN=John Doe,OU=Developers,OU=Staff,DC=corp,DC=mycorp,DC=com));
cn, adspath;subtree')
order BY cn;

 

See http://msdn.microsoft.com/en-us/library/aa746475(VS.85).aspx for recursive search conditions.

Advertisements
One Comment leave one →
  1. Karel Hrubeš permalink
    October 8, 2013 9:11 PM

    Thank you wery much, you saved my Time… 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: