Query Activer Directory in Sql Server 2008

I am trying to query Active Directory.

I have:
1. Configured for distributed queries
sp_configure 'Ad Hoc Distributed Queries', 1
reconfigure
2. Created Linked server for Active Directory
sp_addlinkedserver 'ADSI', 'Active Directory Service Interfces', 'ADSDSOObject', 'adsdatasource'
3. Tried to Query:
SELECT * FROM OpenQuery(ADSI, 'SELECT * FROM ''LDAP://DC={domainname},DC=com'' WHERE objectCategory=''user'' ')
    Where {domainname} is our domain.

After executing the query:
Msg 7330, Level 16, State 2, Line 1
Cannot fetch a row from OLE DB provider "ADSDSOObject" for linked server "ADSI".

One repsonse to this issues was:
You must have Adsldp.dll Adsldpc.dll Adsmsext.dll Activeds.dll installed on your client computer to work with the provider

After searching the server, the dll's exist in the Windows system folders.

Environment:
Running Sql Server 2008 SP2 64 bit
Windows Server 2008

Thank you for any help!
bwkogerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
Try using this format for your AD Query:
...OpenQuery(ADSI, '<LDAP://DC={domainname},DC=COM>;(objectCategory=user)')...
0
bwkogerAuthor Commented:
I entered the statement:

SELECT * FROM OpenQuery(ADSI, '<LDAP://DC=imerys,DC=COM>;(objectCategory=user)')

Returned:

Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query "<LDAP://DC=domainname,DC=COM>;(objectCategory=user)" for execution against OLE DB provider "ADSDSOObject" for linked server "ADSI".
0
Shaun KlineLead Software EngineerCommented:
OK. Went back to my notes on these queries. The LDAP part requires four values separated by semi-colons. The first part is the server connection, which you've already defined (<LDAP://DC=imerys,DC=COM>). The second part is the query component, which you have also defined (objectCategory=user). The third part is the values you wish to return. An asterisk (*) will only bring back the ADsPath for the user. If you want specific fields from AD, you need to provide those field values (which can be found at http://msdn.microsoft.com/en-us/library/ms675085%28v=vs.85%29.aspx). The fourth part is how you want to search active directory, which can be base, one, or subtree.

For your purposes, try:
SELECT * FROM OpenQuery(ADSI, '<LDAP://DC=imerys,DC=COM>;(objectCategory=user);*;subtree')

to start.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

bwkogerAuthor Commented:
I simply copied / pasted the line above......

Msg 7330, Level 16, State 2, Line 2
Cannot fetch a row from OLE DB provider "ADSDSOObject" for linked server "ADSI".
0
Shaun KlineLead Software EngineerCommented:
Does the account you are using to run the query have read permissions on Active Directory? Are you running the query on your local machine or directly on the server?
0
Shaun KlineLead Software EngineerCommented:
You may also want to review the forum (http://www.sqlservercentral.com/Forums/Topic695128-359-1.aspx) as there are some posts there that may point to a solution.
0
qf3l3kCommented:
You should set following parameters on Linked Server:

Provider: OLE DB Provider for Microsoft Directory Services
Product name: Active Directory Services 2.5
Data source: ADSDSOObject
Provider string: (name of the domain controller)

Please refer to attached screenshot. Those settings work for me without any issues.
ADSI-on-SQL-2008.jpg
0
bwkogerAuthor Commented:
I am running the query directly on the Sql Server.  Logged in using a username the network guys have assured me has read access toi the domain AD.

I re-created ADSI with the domain name in the provider string.   no change.
I also checked the referenced forum link.

This shouldn't be this difficult.  :(
0
qf3l3kCommented:
In ADSI provider properties in Security section try to type username with domain.
Remote login: DOMAIN\user

Other than that try to login using this user account to computer and see if you can access AD and search from Windows perspective.
Normal user account with Domain User permissions should be able to access AD from SQL.
I'm using setting I sent on screenshot for reporting and data consolidation and it works.
So, either there is something with account or between SQL and domain.
0
bwkogerAuthor Commented:
I tried adding the reomte user.
The network guys reset the delagation for the user id to ensure I havce read access.
Please see the attached for queries and AD network query / access.

I have a new error message for any of the queries in the attached Word doc.
Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query "SELECT * FROM 'LDAP://DC=doma,DC=com' WHERE objectCategory='user' " for execution against OLE DB provider "ADSDSOObject" for linked server "ADSI".



SQL-AD-Queries.doc
0
Shaun KlineLead Software EngineerCommented:
If your network domain is doma.imgln.net, the LDAP connection information is not correct.
Try LDAP://DC=doma,DC=imgln,DC=net.
0
bwkogerAuthor Commented:
Thank you!  Almost there!!!  except....

Using this query:
SELECT * FROM OpenQuery(ADSI, '<LDAP://DC=doma,DC=imgln,DC=net>;(objectCategory=user);*;subtree')

It DOES return rows, but then erors with the following:

Msg 7330, Level 16, State 2, Line 1
Cannot fetch a row from OLE DB provider "ADSDSOObject" for linked server "ADSI".

So, I'm guessing there is still an area in the AD structure for which I do not have proper access.
Is there a way to filter in the "where"  clause to limit the search to part of the structure?

Thank you so much for sticking with this.   Obviously, I'm a SQL hack.
0
bwkogerAuthor Commented:
I can list up to 901 rows after which it aborts with the above error.
0
Shaun KlineLead Software EngineerCommented:
You can add to the LDAP portion to reduce the area that you are searching. Using the document you attached earlier, you could change it to:
<LDAP://OU=Admin Accounts,DC=doma,DC=imgln,DC=net>
if you wanted to just search your Admin Accounts area.
0
bwkogerAuthor Commented:
The query still aborts after 901 rows are returned.  Perhaps it has to do with some additional secutiry in the structure.
0
btrieveCommented:
Did you ever resolve your last issue about the query aborting after returning 901 rows? I'm currently having the same issue with 997 rows before it crashes. Please let me know if you ever found a solution. Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.