Search code examples
sql-server-2008active-directoryopenrowset

Get AD Email Address From SSMS


I am Domain Admin and am trying to use OpenRowset to query AD from Sql Server 2008 this is my syntax (and it is probably a syntax error) but I get this error:

Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query
"SELECT name, displayname, givenname, distinguishedName, SAMAccountName
FROM 'LDAP://Domainname.company.com/ou=northamerica, dc=domainname,dc=company,dc=com'
WHERE Name = 'Red*'"
for execution against OLE DB provider "ADSDSOObject" for linked server "(null)".

And this is my syntax I try to run

SELECT *
FROM OPENROWSET('ADSDSOObject', 'adsdatasource',
'SELECT name, displayname, givenname, distinguishedName, SAMAccountName
FROM ''LDAP://Domainname.company.com/ou=northamerica,
  dc=domainname,dc=company,dc=com'' 
WHERE Name = ''Red*''')
GO

Solution

  • I did this a while back and I was getting results. See if this gets you in a right path.

    SELECT cn, objectGUID
    FROM  'LDAP://xxx.local/OU=xxx Users,DC=xxx,DC=xxx'
    WHERE objectClass = 'User' AND cn = 'John Smith'