Created
January 5, 2020 15:19
-
-
Save LiCongMingDeShujuku/d14ad97629e07b4af2d9d8a6150526eb to your computer and use it in GitHub Desktop.
Query Active Directory With SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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