Marcus Müller
2008-03-06 00:27:20 UTC
This simple question drove me mad and took me a long time to figure it out:
How to get all users of a security group from an Active Directory with LDAP
using SQL Server 2005?
The reason for getting all users of a group was that I wanted to get the
email addresses of all group members and send them an email via a stored
procedure. After seaching the web, newsgroups and MSDN I haven't found
anyone who had implemented this with T-SQL. There are some solutions with VB
Script but they are useless if you want to transfer them them to T-SQL.
Thus, here is the pure, short T-SQL code (with a little bit of ldap):
SELECT name, mail
FROM OPENQUERY( ADSI,
'SELECT name, mail
FROM ''LDAP:// DC=[domain],DC=[tld]'' WHERE
memberOf=''CN=[groupname],OU=[organizationalunit],DC=[domain],DC=[tld]'' AND
objectCategory=''Person'' AND objectClass = ''USER''')
Variables are marked with brackets:
[domain] = Name of the Active Directory domain (e.g. "YourDomain")
[tld] = Name of the top level domain (e.g. "local")
[groupname] = This is the name of the security group whose members you want
to know (e.g. "Domain Admins")
[organizationalunit] = Name of the organizational unit (e.g. "Users")
You might think, uhh that's really simple, but the statement uses a feature
of the ADSI provider which isn't really obvious:
The "memberOf" attribute of a user returns an error 7346 if you try to
retrive the attribute as a column in the select statement because it
contains multiple values. I guess the attribute is returned as an array
which can't be casted into a sql compliant data type. Thus, I was really
amazed that the "FROM" clause was able to transfer the "memberOf=..."
statement into something meaningfull.
As far as I tested, the ADSI provider translates the "memberOf=..."
statement into an "EXISTS" statement in which the left part of the equation
has to exist as array entry. Additionally, wildcards like
"memberOf=''CN=abc*'' " are not allowed. You have to enter the complete path
of the group in your Active Directory.
Hopefully, this helps anybody who has searched for the same issue and did't
find any clue. If I have overlooked something, please tell me. The basics of
joining Active Directory Objects with Microsoft SQL Server are explained in
the MSDN article "Distributed Query" (
http://msdn2.microsoft.com/en-us/library/aa772380(VS.85).aspx ).
Yours,
Marcus Müller
How to get all users of a security group from an Active Directory with LDAP
using SQL Server 2005?
The reason for getting all users of a group was that I wanted to get the
email addresses of all group members and send them an email via a stored
procedure. After seaching the web, newsgroups and MSDN I haven't found
anyone who had implemented this with T-SQL. There are some solutions with VB
Script but they are useless if you want to transfer them them to T-SQL.
Thus, here is the pure, short T-SQL code (with a little bit of ldap):
SELECT name, mail
FROM OPENQUERY( ADSI,
'SELECT name, mail
FROM ''LDAP:// DC=[domain],DC=[tld]'' WHERE
memberOf=''CN=[groupname],OU=[organizationalunit],DC=[domain],DC=[tld]'' AND
objectCategory=''Person'' AND objectClass = ''USER''')
Variables are marked with brackets:
[domain] = Name of the Active Directory domain (e.g. "YourDomain")
[tld] = Name of the top level domain (e.g. "local")
[groupname] = This is the name of the security group whose members you want
to know (e.g. "Domain Admins")
[organizationalunit] = Name of the organizational unit (e.g. "Users")
You might think, uhh that's really simple, but the statement uses a feature
of the ADSI provider which isn't really obvious:
The "memberOf" attribute of a user returns an error 7346 if you try to
retrive the attribute as a column in the select statement because it
contains multiple values. I guess the attribute is returned as an array
which can't be casted into a sql compliant data type. Thus, I was really
amazed that the "FROM" clause was able to transfer the "memberOf=..."
statement into something meaningfull.
As far as I tested, the ADSI provider translates the "memberOf=..."
statement into an "EXISTS" statement in which the left part of the equation
has to exist as array entry. Additionally, wildcards like
"memberOf=''CN=abc*'' " are not allowed. You have to enter the complete path
of the group in your Active Directory.
Hopefully, this helps anybody who has searched for the same issue and did't
find any clue. If I have overlooked something, please tell me. The basics of
joining Active Directory Objects with Microsoft SQL Server are explained in
the MSDN article "Distributed Query" (
http://msdn2.microsoft.com/en-us/library/aa772380(VS.85).aspx ).
Yours,
Marcus Müller