Link to home
Start Free TrialLog in
Avatar of QPR
QPRFlag for New Zealand

asked on

Querying AD

Hi,
I have some code that is VB and is used to connect to active directory using LDAP
strSql = "Select sn, givenName, name"
strSql = strSql & "From 'LDAP://DC = Local,DC=OurPlace,DC= govt, DC=NZ' " 'rob
strSql = strSql & "WHERE objectCategory='Person' "
strSql = strSql & "AND objectClass = 'User' "

Is it possible to have this inside a stored procedure?
I tried in QA but it spews on the "FROM" line.
ASKER CERTIFIED SOLUTION
Avatar of nmcdermaid
nmcdermaid

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of QPR

ASKER

Thanks, I'll check the link.
I did cut the string stuff in QA but I kept getting syntax errors for...

From LDAP://DC = Local,DC=OurPlace,DC= govt, DC=NZ
Avatar of QPR

ASKER

That almost works great.
If I just select name from etc I get that column.
Not knowing the other column names I tried a select * but got a ton of cryptic non-SQL errors.
Any idea how to retrieve the column names?
If not, I'll get the network admin to tell me.
Avatar of nmcdermaid
nmcdermaid

FYI when a name has spaces or special characters you surround it with square brackets.

Like this:


From [LDAP://DC = Local,DC=OurPlace,DC= govt, DC=NZ]


That particular thing won't actually work, but if you ever have a strangely named table or field, just surround it with square brackets.


For PAQ reference in case the above link goes down, here is the syntax:




SELECT distinguishedName, name
FROM OPENROWSET('ADSDSOObject', 'adsdatasource'
, 'SELECT distinguishedName, name, memberOf
FROM ''LDAP://microsoft.com/DC=microsoft,DC=com''
WHERE objectClass = ''user''
'
)



The final link on that page:

http://msdn.microsoft.com/library/de...ce_mapping.asp

indicates that these are all valid field names:


givenname
sn
initials
displayname
description
mail

userprincipalname
samaccountname

memberof
primarygroupid

profilepath
scriptpath

department
company
manager



There are a couple more on that particular link.


You may have to use a process of elimination if some of them won't return.
Avatar of QPR

ASKER

thanks, stupidly I wasn't reading correctly and was trying to select fields from the outer select that weren't "selected in the inner if you get my drift. The network admin has shown me the AD schema so I now know which columns to selct.