Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LiCongMingDeShujuku/d14ad97629e07b4af2d9d8a6150526eb to your computer and use it in GitHub Desktop.
Save LiCongMingDeShujuku/d14ad97629e07b4af2d9d8a6150526eb to your computer and use it in GitHub Desktop.
Query Active Directory With SQL
use master;
set nocount on
-- Create ADSI Linked Server for Active Directory queries
exec master.dbo.sp_addlinkedserver
@server = N'ADSI_LINK'
, @srvproduct = N'Active Directory Services Interfaces'
, @provider = N'ADSDSOObject'
, @datasrc = N'MyDomainController.domain.com'
go
exec master.dbo.sp_addlinkedsrvlogin
@rmtsrvname = N'ADSI_LINK'
, @useself = N'False'
, @locallogin = NULL
, @rmtuser = N'MyDomain\MyUserName'
, @rmtpassword = 'MyPassword'
go
select * from openquery
(
ADSI_LINK
, 'SELECT mail
, displayname FROM ''LDAP://MyDomainController.MyDomain.Com''
WHERE
objectCategory = ''Person''
AND objectClass = ''user''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment