Welcome to Office Zealot Sign in | Join | Help

Querying Active Directory from SQL Server

Had a request a few weeks ago to get a list of AD users by writing some custom code.  While I love to write code, I thought there had to be an easier (faster) way to get my data.  There is... t-sql!  Here's what I did:

1. Add a linked server to Active Directory.  You can do this with t-sql (below) or through the Enterprise Manager / Management Studio interface.

sp_addlinkedserver 'ADSI', 'Active Directory Service
Interfaces', 'ADSDSOObject', 'adsdatasource'

 

2. Do a t-sql SELECT.  The t-sql below returns name and email address.

 

SELECT *
FROM OPENQUERY (ADSI,
'SELECT givenName, sn, mail FROM ''LDAP://DC=<domain>,DC=com'' WHERE objectCategory = ''Person'' AND objectClass = ''user''')
 

 

Published Thursday, October 25, 2007 3:19 AM by Mauro
Filed under: ,

Comments

Thursday, October 25, 2007 7:47 AM by FlangeArtist

# re: Querying Active Directory from SQL Server

Hi Maruo,

Is it possible to change information in AD using this method?  Like an Update?  If so, got any examples?

Monday, June 09, 2008 8:54 AM by celeste

# re: Querying Active Directory from SQL Server

hi, mauro. i'm wondering if it's possibleto do the opposite of what you describe. do you know if you can store AD info on SQL Server and present that info using the AD features for searching and editing employee info? sorry if i'm not expressing this correctly. I'm an Access developer and AD is new to me. the place i'm doing work at would like to make data available via AD, but also needs to store add'l ifo about each employee that would be managed via MS Access.

thank you, in advance, if you have time to respond to this question.

celeste

# Calazan.com &raquo; Blog Archive &raquo; How to query Active Directory using SQL Server

Anonymous comments are disabled