Reading Attributes

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

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.

Scripting Steps

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:

  1. 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.

  2. Bind to the user account object by using the GetObject function and the LDAP provider.
  3. 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.

  4. 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.

  5. 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.

  6. Display each single-valued attribute s lDAPDisplayName and value.
  7. Using a For Each loop, display each multivalued attribute s 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 
On Error Resume Next Set objUser = GetObject _     ("LDAP://cn=MyerKen,ou=Management,dc=NA,dc=fabrikam,dc=com") objUser.GetInfo strGivenName = objUser.Get("givenName") strInitials = objUser.Get("initials") strSn = objUser.Get("sn") strDisplayName = objUser.Get("displayName") strPhysicalDeliveryOfficeName = _     objUser.Get("physicalDeliveryOfficeName") strTelephoneNumber = objUser.Get("telephoneNumber") strMail = objUser.Get("mail") strWwwHomePage = objUser.Get("wWWHomePage") strDescription = objUser.GetEx("description") strOtherTelephone = objUser.GetEx("otherTelephone") strUrl = objUser.GetEx("url") Wscript.Echo "givenName: " & strGivenName Wscript.Echo "initials: " & strInitials Wscript.Echo "sn: " & strSn Wscript.Echo "displayName: " & strDisplayName Wscript.Echo "physicalDeliveryOfficeName: " & _    strPhysicalDeliveryOfficeName Wscript.Echo "telephoneNumber: " & strTelephoneNumber Wscript.Echo "mail: " & strMail Wscript.Echo "wWWHomePage: " & strWwwHomePage For Each strValue in strDescription     Wscript.Echo "description: " & strValue Next For Each strValue in strOtherTelephone     Wscript.Echo "otherTelephone: " & strValue Next For Each strValue in strUrl     Wscript.Echo "url: " & strValue Next

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 attribute s name and a message stating that there is no value. Otherwise, display the attribute s 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.

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net