Tuesday, September 24, 2013

Query Active directory from SQL Server

This post was formerly published at my beloved old blog "On the quest of a DBA's adventure......"

Thanks to the sharing from Eralper YILMAZ at http://www.kodyaz.com/, I found this article very interesting and useful. Before I read about this method, I have never imagined that one can query the LDAP directory from SQL and this is URL to the original article.

Running Active Directory Services Queries Using MS SQL Server T-SQL OPENQUERY Command

In summary, you need to enable the instance-level option 'Ad Hoc Distributed Queries' and setup a linked server toward the AD object (Hope I am using the right terms here).

Then, you can magically start to query the AD, like the following SQL statement:

Note that there are some other blog suggested to limit the number of result returned by WHERE clause or the TOP clause if your directory has over 1000 entries.

Also to explore more attribute from the AD, here is good reference to find out what attribute is in Active directory's LDAP schema - Reading LDAP Directory Object Attributes

No comments:

Post a Comment