Amend my LDAP query

Jay Paterson 96 Reputation points
2020-11-17T02:41:51.547+00:00

I have this openquery that I use to extract data from AD and it works fine. However I need to add an additional criteria but am struggling to do so.
Currently the query looks like this
OpenQuery (
ADSI,
'SELECT mail, mobile, telephoneNumber, title, sn, givenName, SAMAccountName, department FROM ''LDAP://xxxxx'
WHERE objectClass = ''user'' AND givenName='''' AND sn='''' AND title=''*'' AND ''userAccountControl:1.2.840.113556.1.4.803:''<>2 AND ''userAccountControl:1.2.840.113556.1.4.803:''<>65536 AND Mail = ''*domain1'' OR description =''xxxxx''OR description = ''xxxxx''') AS tblADS

You'll see that I am specifying that an email must be in the format *domain1 (obviously the real values are masked).
I now need to add the criteria mail = ''*domain2''
In SQL In would use IN ('x','y') but see no equivalent syntax in LDAP.
To be clear the domain1 OR domain2 must be separate from the other OR criteria as domain2 must satisfy all the other AND criteria in the select statement.

Any ideas?

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,579 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,640 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sunny Qi 11,046 Reputation points Microsoft Vendor
    2020-11-18T02:47:38.387+00:00

    Hi,

    Thanks for posting in Q&A platform.

    We noticed that you issue was regarding of OpenQuery and LDAP query, and please understanding we are not familiar with how to write these queries. Based on discussed with our SQL and AD DS colleagues, we would like suggest you could try to contact Script Forum for further help:
    https://social.technet.microsoft.com/Forums/en-us/home?forum=ITCG&filter=alltypes&sort=lastpostdesc

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Best Regards,
    Sunny

    ----------

    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.