• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

Query Active Directory from SQL 2008

I set up a  linked server to the active directory using the following code:

EXEC master.dbo.sp_addlinkedserver @server = N'ADSI',
@srvproduct=N'Active Directory Services', @provider=N'ADsDSOObject',
@datasrc=N'ServerName.Domain'

Open in new window


I get a linked server. I can right click and test connection with an affirmative response. Then I click through to try and see the tables and I get the message in the attachment. When I try to run a query using this:

select  * 
from  openquery(ADSI, ' 
select  title, sAMAccountName, displayName, userAccountControl
from    ''LDAP://DC=ServerName.Domain,DC=COM'' ') 

Open in new window


I get the following message:

Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query "SELECT sAMAccountname,givenname,sn,displayname,useraccountcontrol,CN
FROM 'LDAP://DC=ServerName.Domain,DC=COM'
WHERE objectCategory='person' AND objectClass='user'
ORDER BY CN" for execution against OLE DB provider "ADsDSOObject" for linked server "ADSI".

The "ServerName.Domain" is the name of the server on which active directory resides and the domain is the name of the network domain.

I added the network administrator as the remote login. I'm out of ideas as to why this is not working.
Capture.JPG
0
jczander
Asked:
jczander
1 Solution
 
Vadim RappCommented:
If your domain is, say, inter.mydomain.com, then the 4th line of your code should be

from    ''LDAP://dc=inter,dc=mydomain,dc=com'' ')

ServerName shouldn't be in the picture.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now