Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Providers\ADSDSOObject] "AllowInProcess"=dword:00000001
-- Change 'adsi' to the desired name of the linked server exec sp_addlinkedserver 'adsi', '', 'ADSDSOObject', '' go -- Change 'adsi' to the desired name of the linked server -- Change 'sqlUser' to the username of local sql server user -- Change 'domainName\userName' to a domain account -- (the format can be 'domainName\userName' or 'userName@domainName') -- Change 'domainUserPassword' to the password of the domain account exec sp_addlinkedsrvlogin 'adsi', false, 'sqlServerUser', 'domainName\userName', 'domainUserPassword' go
declare @linkedServerSql nvarchar(4000), @linkedServerName varchar(100), @localSqlUsername varchar(100), @domainUsername varchar(100), @domainUserPassword varchar(100) -- Set the local sql server user set @localSqlUsername = 'sqlUser' -- format can be 'domainName\userName' or 'userName@domainName' set @domainUsername = 'domainName\userName' set @domainUserPassword = 'domainUserPassword' set @linkedServerName = 'adsi' set @linkedServerSql = ' exec sp_addlinkedserver ''' + @linkedServerName + ''', '''', ''ADSDSOObject'', '''' exec sp_addlinkedsrvlogin ''' + @linkedServerName + ''', false, ''' + @localSqlUsername + ''', ''' + @domainUsername + ''', ''' + @domainUserPassword + '''' exec sp_executesql @linkedServerSqlRun a query to verify that the linked server works. The query below will give you all the users in the dc=ica,dc=com (change that to match your own domain):
select *
from openquery(adsi, '
select givenName,
sn,
sAMAccountName,
displayName,
mail,
telephoneNumber,
mobile,
physicalDeliveryOfficeName,
department,
division
from ''LDAP://dc=ica,dc=com''
where objectCategory = ''Person''
and
objectClass = ''user''
')
Below, is an alternate syntax which you can use to apply ldap filters with almost universal syntax. The following will get all the users in LDAP but limit the result set to those users who's "given" & "sn" names are not empty. It will also apply a filter to the "division" attribute and exclude any records that match "system" and "generic". declare @ldapFilter nvarchar(1000), @ldapSQL nvarchar(4000)
-- Set the filter to exlude objects that have a division of "System" and "Generic"
set @ldapFilter = '(!division=System*)(!division=Generic)'
-- Create an ldap query to get all users under dc=ica,dc=com
set @ldapSQL = '
select givenName as firstName,
sn as lastName,
displayName,
lower(sAMAccountName) as accountName,
telephoneNumber as phoneNumber,
mobile as cellPhoneNumber,
mail as emailAddress,
department,
physicalDeliveryOfficeName as siteName
from openquery(adsi, ''<LDAP://dc=ica,dc=com>
(&(objectCategory=Person)(objectClass=user)'+ @ldapFilter + ');
givenName,
sn,
sAMAccountName,
displayName,
mail,
telephoneNumber,
mobile,
physicalDeliveryOfficeName,
department,
division;
subtree'')
where givenName is not null
and
sn is not null'
exec sp_executesql @ldapSQL
Some things to note:
"LDAP" is case sensitive, if you try using "ldap", the query will throw an error.
Querying the Active Directory server will work fine from Query Analyzer even if you do not run sp_addlinkedserverlogin. However, if you try to execute the query from the web (with something like ColdFusion, you will get an error similar to:
[Macromedia][SQLServer JDBC Driver][SQLServer]OLE DB provider 'ADSDSOObject' reported an error. The provider indicates that the user did not have the permission to perform the operation.
Links:
[1] mailto:ntaccount@domain.com
[2] mailto:bkostadinov@ica.com
[3] http://blog.tech-cats.com/2007/09/querying-active-directory-through-sql.html