WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

ADSI
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Proper AD Query

 Keywords:  Proper AD ADSI Query dsFindPath objectClass objectCategory Efficient

Question:

I am querying AD to grab some user and computer info and they way I am doing this is
AddExtender("WWADS44I.DLL")
compName = RegQueryValue(@REGMACHINE, "SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName[ComputerName]")
userName = "myUserName"
sAdsiPath = "LDAP://DC=my,DC=domain,DC=com"
lPath1= dsFindPath(sAdsiPath1, StrCat("(&(objectClass=computer)(cn=", compName, "))"))
lPath2= dsFindPath(sAdsiPath2, StrCat("(&(objectClass=user)(sAMAccountName=", userName, "))"))
The script returns the full LDAP path of the object. But if I change the above code (line 5 & 6) to
lPath1= dsFindPath(sAdsiPath, StrCat("cn=", compName))
lPath2= dsFindPath(sAdsiPath, StrCat("sAMAccountName=", userName))
it returns the same thing.

So I am assuming that if I specify "objectClass=" in my script I am narrowing the search to a specific class (like users or computers) and making it more efficient as opposed to not specifying any objectClass (in which case the AD query is done against all AD objects). Am I correct in my assumption?

Answer:

That is correct. Search filters enable you to define search criteria and provide more efficient and effective searches. http://msdn.microsoft.com/en-us/library/windows/desktop/aa746475(v=vs.85).aspx

Pay careful attention to the differences between "objectClass" and "objectCategory".

The object class is a multi-valued string attribute that is not indexed. It contains the entire object class inheritance hierarchy for the object.

The object category is a single-valued string attribute that is indexed. This attribute typically has a value that is the same as the most descriptive value in the object class attribute, but may be set to some other value representing one of the classes that the object inherits from.

When using a search filter, it is always significantly more efficient to use objectCategory in place of objectClass.

For "user" and "computer" objects, the object's effective class and object category are the same, but for other types of objects, that may not necessarily be the case. Because multiple object types might share the same underlying object category, the best thing to do is the following:

(&(objectCategory=computer)(objectClass=user))
or
(&(objectCategory=user)(objectClass=user))
This results in first using the indexed object class value to rapidly identify a result set of objects from AD, and then further refines that result set by examining object class values.
Article ID:   W17536
Filename:   Proper AD Query.txt
File Created: 2012:03:14:12:55:58
Last Updated: 2012:03:14:12:55:58