Reference: http://www.pawlowski.cz/2011/04/querying-active-directory-sql-server-t-sql/
1. Set up link server.
Use master
Go
EXEC sp_addlinkedserver ' ADSI ', ' Active Directory Services 2.5 ', ' adsdsoobject ', ' Adsdatasource '
2.
exec sp_configure ' show advanced options ', 1
Reconfigure with override
exec sp_configure ' Ad Hoc distributed Queries ', 1
Reconfigure
3. Query domain account information:
SELECT * from OpenQuery (ADSI, ' SELECT * from ' ldap://dc=huanhua,dc=test ' WHERE objectcategory= ' user ')
SELECT * from OpenQuery (ADSI1, ' SELECT * from ' ldap://192.168.1.100/dc=huanhua,dc=test ' WHERE objectcategory= ' user ' ‘)
Query results
Actual domain account information:
Other:
SELECT * FROM
OpenQuery (ADSI1, ' SELECT cn,adspath from ' ldap://192.168.1.100/dc=huanhua,dc=test ' WHERE objectcategory= ' user ')
Note: If the prompt does not have permission, the linked database must be logged on with a domain account.
SQL Server 2008 reads domain account information