環境: Sql server 2008R2SP1 +Windows server 2008 R2 AD
1.開啓SQL 'Ad Hoc Distributed Queries',
exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigure
2.鏈接AD DB,
EXEC sp_addlinkedserver 'ADSI', 'Active Directory Services 2.5', 'ADSDSOObject', 'adsdatasource' GO
3.用DC的IP查詢
SELECT * FROM OpenQuery(ADSI, 'SELECT sAMAccountName FROM ''LDAP://192.168.xx.xx/OU=HR,OU=Usertest,DC=test,DC=COM,DC=CN'' WHERE objectCategory=''user'' ' )
參考:
http://msdn.microsoft.com/ZH-CN/library/windows/desktop/aa772380(v=vs.85).aspx
http://zh.wikipedia.org/wiki/Active_Directory_Service_Interface