SQLServer 使用ADSI执行分布式查询ActiveDorectory对象

时间:2024-01-18 11:22:18 

Step 1:Creating a Linked Server.
EXEC sp_addlinkedserver 'ADSI', 'Active Directory Services 2.5', 'ADSDSOObject', 'adsdatasource'
Step 2:Creating a SQL Server Authenticated Login
EXEC sp_addlinkedsrvlogin @rmtsrvname = N'ADSI', @locallogin = NULL , @useself = N'False', @rmtuser = N'domain\Account', @rmtpassword = N'Password'
对于 SQL Server 授权登录,可以使用sp_addlinkedsrvlogin 系统存储过程配置用于连接到目录服务的适当的登录/密码.
参考这里: http://blogs.msdn.com/euanga/archive/2007/03/22/faq-how-do-i-query-active-directory-from-sql-server.aspx
如果SQLServer使用Windows 授权登录,只需自映射就足以通过使用 SQL Server 安全委托来访问AD。简单点说就是直接运行第三步语句即可.
Step 3:Querying the Directory Service.


-- Query for a list of User entries in an OU using the SQL query dialect
select convert(varchar(50), [Name]) as FullName,
convert(varchar(50), Title) as Title,
convert(varchar(50), TelephoneNumber) as PhoneNumber
from openquery(ADSI,
'select Name, Title, TelephoneNumber
from ''LDAP://OU=Directors,OU=Atlanta,OU=Intellinet,DC=vizability,DC=intellinet,DC=com''
where objectClass = ''User''')
-- Query for a list of Group entries in an OU using the SQL query dialect
select convert(varchar(50), [Name]) as GroupName,
convert(varchar(50), [Description]) GroupDescription
from openquery(ADSI,
'select Name, Description
from ''LDAP://OU=VizAbility Groups,DC=vizability,DC=intellinet,DC=com''
where objectClass = ''Group''')


引用:
http://msdn2.microsoft.com/en-us/library/aa772380.aspx
http://www.atlantamdf.com/presentations/AtlantaMDF_111201_examples.txt

说明:但是这样默认查询出来的是1000个对象.怎么办呢?
方法一,通过字母来循环.见以下:


CREATE TABLE #tmpADUsers
( employeeId varchar(10) NULL,
SAMAccountName varchar(255) NOT NULL,
email varchar(255) NULL)
GO
/**//* AD is limited to send 1000 records in one batch. In an ADO interface you can define this batch size, not in OPENQUERY.
Because of this limitation, we just loop through the alphabet.
*/
DECLARE @cmdstr varchar(255)
DECLARE @nAsciiValue smallint
DECLARE @sChar char(1)
SELECT @nAsciiValue = 65
WHILE @nAsciiValue < 91
BEGIN
SELECT @sChar= CHAR(@nAsciiValue)
EXEC master..xp_sprintf @cmdstr OUTPUT, 'SELECT employeeId, SAMAccountName, Mail FROM OPENQUERY( ADSI, ''SELECT Mail, SAMAccountName, employeeID FROM ''''LDAP://dc=central,dc=mydomain,dc=int''''WHERE objectCategory = ''''Person'''' AND SAMAccountName = ''''%s*'''''' )', @sChar
INSERT #tmpADUsers
EXEC( @cmdstr )
SELECT @nAsciiValue = @nAsciiValue + 1
END
DROP TABLE #tmpADUsers


以上方法源自于:http://www.sqlservercentral.com/Forums/Topic231658-54-1.aspx#bm231954

我推荐的方法:在微软搜索到的.如何通过 NTDSUtil为服务器修改限制 maxPageSize

1.

Click Start, and then click Run.

2.

In the Open text box, type ntdsutil, and then press ENTER. To view help at any time, type ? at the command prompt.

Modifying policy settings

1.

At the Ntdsutil.exe command prompt, type LDAP policies, and then press ENTER.

2.

At the LDAP policy command prompt, type Set setting to variable, and then press ENTER. For example, type Set MaxPoolThreads to 8.

This setting changes if you add another processor to your server.

3.

You can use the Show Values command to verify your changes.

To save the changes, use Commit Changes.

4.

When you finish, type q, and then press ENTER.

5.

To quit Ntdsutil.exe, at the command prompt, type q, and then press ENTER.

资料来源:

http://support.microsoft.com/kb/315071/en-us

http://support.microsoft.com/?scid=kb%3Bzh-cn%3B299410&x=16&y=10

如何使用SQL查询活动目录对象语法: http://www.microsoft.com/china/technet/community/columns/scripts/sg0505.mspx#EMBAC

标签:SQLServer,ADSI,分布式查询
0
投稿

猜你喜欢

  • Pycharm操作Git及GitHub的步骤详解

    2023-02-19 04:34:59
  • Python海龟绘图之绘制趣味简笔画

    2023-03-13 09:09:40
  • 利用python爬取软考试题之ip自动代理

    2023-01-30 01:17:28
  • SQL窗口函数之排名窗口函数的使用

    2024-01-26 18:49:31
  • Python实现读取txt文件中的数据并绘制出图形操作示例

    2021-07-21 17:01:15
  • 3个 Python 编程技巧

    2023-11-30 08:05:19
  • WEB前端开发规范文档

    2010-10-19 12:32:00
  • Python数据结构dict常用操作代码实例

    2022-05-13 02:16:43
  • python对一个数向上取整的实例方法

    2023-04-12 09:07:50
  • Python实现操作Redis的高级用法分享

    2022-07-29 04:26:29
  • 解决selenium+Headless Chrome实现不弹出浏览器自动化登录的问题

    2022-01-14 12:27:24
  • python爬虫获取淘宝天猫商品详细参数

    2021-06-08 09:27:29
  • Python3通过Luhn算法快速验证信用卡卡号的方法

    2022-05-29 12:54:58
  • python爬虫爬取淘宝商品信息(selenum+phontomjs)

    2022-07-07 05:40:52
  • expdp 中ORA-39002、ORA-39070错误详解及解决办法

    2024-01-19 16:37:47
  • SQL Server错误代码大全及解释(留着备用)

    2012-07-11 16:17:03
  • 8个js表单验证函数

    2007-10-28 19:19:00
  • Idea 2019.3 本应该搜索到的插件却搜索不到的解决方法

    2022-03-18 14:42:22
  • 详解Django中views数据查询使用locals()函数进行优化

    2021-01-21 00:53:13
  • Python实现FTP弱口令扫描器的方法示例

    2023-12-16 04:46:45
  • asp之家 网络编程 m.aspxhome.com