Reading Multivalued Attributes

Microsoft® Windows® 2000 Scripting Guide

Because a multivalued attribute of an object can contain more than one value, you use the GetEx method to retrieve all values of the attribute. The information retrieved from GetEx is returned in the form of a variant array. An array is a data structure containing one or more values, and a variant is a data structure containing any data type, such as a string or an integer. Thus a variant array is a data structure containing one or more values of any type.

Important

  • The Get method can also return entries in a multivalued attribute, but as a general rule, use Get for single-valued attributes only. For more information, see the section "Data Caching."

To display the values in an array, you must add script code that is able to iterate through the array of values. Use the VBScript For Each statement to accomplish this task.

Even with the additional scripting code (that is, the For Each statement) that you must add to read a multivalued attribute, reading this type of attribute involves the same two steps outlined in "Reading Attributes of Directory Service Objects" earlier in this chapter:

  1. Connect to the Active Directory object you want to read.

  2. Read one or more of the object's attributes.

    The difference in this step is that you use the GetEx method instead of the Get method to complete the read operation and you use a For Each statement to display each value contained in the attribute.

The goal of the scripts in this section is to read and display the member attribute of the Atl-Users group and the url and otherTelephone attributes of the MyerKen user account. These attributes were modified in "Modifying Multivalued Attributes" earlier in this chapter.

Reading a Multivalued Attribute of a Group

The script in Listing 5.19 reads and displays the entries in the member attribute of the Atl-Users group. If you ran the scripts in the preceding section, the member attribute is now empty. Therefore, rerun the script in Listing 5.14 before running this script.

  1. Connect to the Atl-Users group in the HR OU of the na.fabrikam.com domain.

  2. Use a For Each statement to read and display the object's member attribute.

Listing 5.19 Reading the member Attribute of an OU

  
1
2
3
4
5
6
Set objGroup = GetObject _
 ("LDAP://cn=Atl-Users,ou=HR,dc=NA,dc=fabrikam,dc=com")
For Each Member in objGroup.GetEx("member")
 Wscript.Echo Member
Next

When this script runs in the na.fabrikam.com domain, it echoes the members of the Atl-Users group to the command window, as shown:

CN=LewJudy,OU=Sales,dc=NA,DC=fabrikam,DC=com
CN=MyerKen,OU=HR,dc=NA,DC=fabrikam,DC=com

Reading Multivalued Attributes of a User Account

The script in Listing 5.20 reads and displays the entries in the url and otherTelephone attributes of the MyerKen user account.

  1. Connect to the MyerKen user account in the HR OU of the na.fabrikam.com domain.

  2. Use a For Each statement to read and display the object's url attribute.

  3. Use another For Each statement to read and display the object's otherTelephone attribute.

Listing 5.20 Reading the url and otherTelephone Attributes of a User Account

  
1
2
3
4
5
6
7
8
9
10
Set objGroup = GetObject _
 ("LDAP://cn=MyerKen,ou=HR,dc=NA,dc=fabrikam,dc=com")
For Each url in objGroup.GetEx("url")
 Wscript.Echo url
Next
For Each otherTelephone in objGroup.GetEx("otherTelephone")
 Wscript.Echo otherTelephone
Next

When this script runs in the na.fabrikam.com domain, it echoes the url and otherTelephone attributes of the MyerKen user account to the command window, as shown:

https://www.contoso.com
https://www.fabrikam.com/na
https://www.microsoft.com
555-0183
555-0182
555-0180

Important observations about the scripts in this section are:

  • They perform the same basic steps: they bind to an Active Directory object and use a For Each statement to read the entries contained in multivalued attributes.

  • They use the same method (GetEx) without regard to the class of object being read.

    The GetEx method is similar to the Get method except that you must use a For Each statement to read and display an attribute's values.