且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 SQL Server 2005 查询 Active Directory

更新时间:2023-02-02 22:32:49

很笼统的问题,但这里有一些提示.

Pretty general question but here are some pointers.

您需要在 SQL Server 上创建一个指向 ADSI(Active Directory 服务接口)的链接服务器,这样就可以了.

You need a linked server creating on the SQL Server that points to ADSI (Active Directory Service Interface) something like this will do it.

EXEC sp_addlinkedserver 'ADSI', 'Active Directory Services 2.5', 'ADSDSOObject', 'adsdatasource'

然后您可以使用以下类型的查询.

Then you can use the following sort of query.


SELECT *
FROM OPENQUERY(ADSI, 'SELECT sAMAccountName
FROM ''LDAP://DC=MyDC,DC=com,DC=uk''
WHERE objectCategory = ''Person''
AND objectClass = ''user'')

您需要适当地设置 LDAP://行(详细信息请咨询您的 AD 管理员),并注意 SQL Server 中默认禁用使用 OpenQuery 的分布式即席查询.一旦你有了上述内容,谷歌搜索任何特定的变化应该很容易.

You'll need to set the LDAP:// line appropriately (ask your AD admin for the details) and be aware that distributed adhoc queries using OpenQuery are disabled by default in SQL Server. Once you have the above though it should be pretty easy to google for any particular variations.