1. Create a linked server
EXEC master.dbo.sp_addlinkedserver @server = N'ADSI', @srvproduct=N'Active Directory Services 2.5', @provider=N'ADsDSOObject', @datasrc=N'adsdatasource'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'ADSI', @useself=N'True', @locallogin=NULL, @rmtuser=NULL, @rmtpassword=NULL
2. Query your active directory
SELECT * FROM OPENQUERY (ADSI, 'SELECT userPrincipalName, displayName, distinguishedName,
sAMAccountName, mail, company, accountExpires, whenCreated
FROM ''LDAP://
WHERE objectCategory = ''Person'' AND objectClass = ''user''
AND NOT accountExpires=0
AND NOT company = ''a company name''
ORDER BY company')
There is one drawback however, by default you can only get the first 1000 records from your active directory, but no worries, there is a workaraound! Follow this link to see the solution
Have fun!
No comments:
Post a Comment