Search code examples
phpldapldap-query

LDAP Search String Exclude


I am rewriting a few PowerShell scripts to work in PHP and this one has me stuck. I need to get a list of computers, but exclude those with -NEW and -OLD. Below is how I would think to do it, but when I try that I get ldap_search(): Search: Bad search filter as an error.

$LDAP_Search_String = "(&(objectClass=computer)(name=NFDW*)(!name=NFDW*-NEW)(!name=NFDW*-OLD))";

If I remove (!name=NFDW*-NEW)(!name=NFDW*-OLD) from the query, it works fine.

Additional Info: I have tried (&(objectClass=computer)(name=NFDW*)(|(!name=NFDW*-NEW)(!name=NFDW*-OLD))) as well as suggested by a deleted answer and the same result of a Bad search filter.


Solution

  • Try adding parenthesis around your ! i.e. (!(name=NFDW*-NEW))(!(name=NFDW*-OLD))