Reading Attributes
Microsoft® Windows® 2000 Scripting Guide
As shown in Table 7.8, the General properties page contains both single-valued and multivalued attributes. Use the Get method of IADs to return all single-valued attributes, and use the GetEx method of IADs to return entries assigned to multivalued attributes.
For example, the following lines of code use the Get method to return the givenName and the GetEx method to return the otherTelephone value:
strGivenName = objUser.Get("givenName")
strOtherTelephone = objUser.GetEx("otherTelephone")
Using the correct method is very important. If you use the GetEx method to retrieve a single-valued attribute (such as givenName), you will generate a "Data type mismatch" error. If you use the Get method to retrieve a multivalued attribute (such as otherTelephones), no error will be generated. However, no data will be retrieved, either.
Listing 7.12 contains a script that reads entries assigned to single-valued and multivalued attributes that appear on the General properties page of a user account object. To carry out this task, the script performs the following steps:
Use the On Error Resume Next statement to bypass the ADSI error generated when an attribute cannot be found in the local property cache.
A discussion of this ADSI error follows the script.
Bind to the user account object by using the GetObject function and the LDAP provider.
Use the GetInfo method to initialize the local cache with attributes of the user account object.
This step is not required, but it ensures that the most up-to-date attribute values of the ADSI object are retrieved.
Use the Get method to retrieve values from single-valued attributes.
What happens if no value exists for a specified attribute? For example, suppose no value has been configured for physicalDeliveryOfficeName. When the script calls the Get method on a nonexistent attribute, an error is generated. More information about this behavior follows the script.
Use the GetEx method to retrieve entries assigned to multivalued attributes.
The GetEx method returns an array.
The same type of error generated for the Get method is also generated for the GetEx method for multivalued attributes that are not part of the user account object being evaluated.
Display each single-valued attributes lDAPDisplayName and value.
Using a For Each loop, display each multivalued attributes lDAPDisplayName and value.
Listing 7.12 Reading Attributes on the General Properties Page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
If any of the attributes do not contain values and the On Error Resume Next statement is not present, the script returns the following ADSI error message when the Get or GetEx method attempts to read values of the nonexistent attributes:
Active Directory: The directory property cannot be found in the cache.
If an attribute does not contain a value, the attribute does not exist according to LDAP specifications. This results in an ADSI error with an error code of &h8000500D.
You can avoid this error in the following ways:
Place the On Error Resume Next statement at the top of the script, as shown in line 1 of Listing 7.12.
Use the On Error Resume Next statement to test for the &h8000500D error code. If there is an error, display the attributes name and a message stating that there is no value. Otherwise, display the attributes name and value.
For information about error handling, see "VBScript Primer" and "Creating Enterprise Scripts" in this book.
Note
- You can use the properties of the IADsUser interface to return values and avoid errors generated when an attribute is not in the property cache. However, not all attributes appearing on the General properties page have corresponding properties in the IADsUser interface. Therefore, using the IADs interface and implementing error handling in your scripts is the preferred method of reading and writing attribute values.