Microsoft KB Archive/269181

= How to query Active Directory by using a bitwise filter =

Article ID: 269181

Article Last Modified on 2/8/2006

-

APPLIES TO


 * Microsoft Active Directory Service Interfaces 2.5
 * Microsoft Active Directory Service Interfaces 2.5
 * Microsoft Active Directory Service Interfaces 2.5

-



This article was previously published under Q269181



SUMMARY
Some attributes on Active Directory objects are composed of bitwise flags. You may need to query for objects using a bitwise operator to return only objects that match a particular bit being set. Use the Lightweight Directory Access Protocol (LDAP) Matching Rule controls to do this.



MORE INFORMATION
The format of the LDAP Matching Rule has the following syntax:

attributename:ruleOID:=value

where attributename is the LDAPDisplayName of the attribute, ruleOID is the object ID (OID) for the matching rule control, and value is the decimal value you want to use for comparison. You need to convert from hexadecimal to decimal.

The value of ruleOID can be one of the following:
 * 1.2.840.113556.1.4.803 - This is the LDAP_MATCHING_RULE_BIT_AND rule. The matching rule is true only if all bits from the property match the value. This rule is like the bitwise AND operator.
 * 1.2.840.113556.1.4.804 - This is the LDAP_MATCHING_RULE_BIT_OR rule. The matching rule is true if any bits from the property match the value. This rule is like the bitwise OR operator.

An example is when you want to query Active Directory for user class objects that are disabled. The attribute that holds this information is the userAccountControl attribute. This attribute is composed of a combination of different flags. The flag for setting the object that you want to disable is UF_ACCOUNTDISABLE, which has a value of 0x02 (2 decimal). The bitwise comparison filter that specifies userAccountControl with the UF_ACCOUNTDISABLED bit set would resemble this: (UserAccountControl:1.2.840.113556.1.4.803:=2) The following Microsoft Visual Basic sample script uses the above bitwise comparison filter: Set oNSP = GetObject(&quot;LDAP://Win2000Server/rootdse&quot;) Set oConfig = GetObject(&quot;LDAP://Win2000Server/&quot; & oNSP.get(&quot;DefaultNamingContext&quot;))

Set oConn = CreateObject(&quot;ADODB.Connection&quot;) oConn.Provider = &quot;ADSDSOObject&quot; oConn.Open &quot;&quot;

strQuery = &quot;<&quot; & oConfig.ADsPath & &quot;>;(&(objectCategory=person)(objectClass=User)(userAccountControl:1.2.840.113556.1.4.803:=2));name,objectClass;subtree&quot;

Set oRS = oConn.Execute(strQuery) While Not oRS.EOF MsgBox oRS.Fields(&quot;name&quot;) oRS.MoveNext Wend

MsgBox &quot;done&quot;

Set oConn = Nothing Set oRS = Nothing Set oConfig = Nothing Set oNSP = Nothing

