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

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/?scid=kb%3bzh-cn%3b299410&x=16&y=10

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

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐