dunnry:The filter looks right. If it is not working, it might be because you have the DN of the group wrong in the filter. The group itself does not need to be located in the same OU - it can be anywhere. I just tested this myself and it does work. Again, just check your DN carefully for the group and make sure it is exact. If you need to, find the group in a tool like ldp.exe and copy the DN to make sure you have it right.
I found the group using ldp.exe and copied the DN so that the spelling was correct.
One of the results looked like:
"CN=ExcludeFromWebApp,OU=DEPARTMENT,DC=company,DC=org"
So I copied that and continued to use ldp.exe to create a new query with the following:
Base Dn: OU=TESTDEPARTMENT,OU=DEPARTMENT,dc=company,dc=org
Filter: (&(objectClass=user)(objectCategory=person)(!(memberOf=CN=ExcludeFromWebApp,OU=DEPARTMENT,DC=company,DC=org)))
Here's a sample of the results, it still included a user that should have been filtered out:
-----------
***Searching...
ldap_search_s(ld, "OU=TESTDEPARTMENT,OU=DEPARTMENT,dc=company,dc=org", 2, "(&(objectClass=user)(objectCategory=person)(!(memberOf=CN=ExcludeFromWebApp,OU=DEPARTMENT,DC=company,DC=org)))", attrList, 0, &msg)
Result <0>: (null)
Matched DNs:
Getting 183 entries:
>> Dn: CN=testuser,OU=TESTDEPARTMENT,OU=DEPARTMENT,dc=company,dc=org
7> memberOf: CN=ExcludeFromWebApp,OU=DEPARTMENT,DC=company,DC=org; CN=Another Group,OU=DEPARTMENT,DC=company,DC=org;
1> canonicalName: company.org/DEPARTMENT/TESTDEPARTMENT/testuser;
1> cn: testuser;
1> distinguishedName: CN=testuser,OU=TESTDEPARTMENT,OU=DEPARTMENT,dc=company,dc=org;
4> objectClass: top; person; organizationalPerson; user;
1> name: testuser;
*
*
*
-----------
I also tried to reverse the logic to find users that are in the group with the following...
Filter: (&(objectClass=user)(objectCategory=person)(memberOf=CN=ExcludeFromWebApp,OU=DEPARTMENT,DC=company,DC=org))
It returns no results, even though there are two users in this group.