Script Tips

An Array of Attributes

Simple attributes are old school; do more with multi-valued attributes in your scripts.

Ready for your Scripting Answers? This time, let me show you how to work with the complex, multi-valued attributes in Active Directory. Unlike nice, simple attributes like name, which contains one and only one value for each user, multi-valued attributes act like arrays, and can contain multiple values. Let's take the otherHomePhone property as an example. Reading its values works like this:

Set objUser = GetObject _
  ("LDAP://cn=donj,ou=Legal, dc=company,dc=com")
  strOtherPhone = objUser.GetEx("otherHomePhone")
For Each strValue In strOtherPhone
  WScript.Echo strValue
Next

Notice the use of GetEx(), which returns an array of values that the For Each loop walks through one at a time. Need to add a new phone number? Do so as follows:

Const ADS_PROPERTY_APPEND = 3
  Set objUser = GetObject _
("LDAP://cn=donj,ou=Legal, dc=company,dc=com")
objUser.PutEx ADS_PROPERTY_APPEND, _
  "otherHomePhone", Array("(123) 555-1212")
objUser.SetInfo

You just use PutEX and specify the special code for appending (3), the attribute name, and an array of values (just one in this example) to append. To delete a value you do something similar:

Const ADS_PROPERTY_DELETE = 4
Set objUser = GetObject _
  ("LDAP://cn=donj,ou=research,dc=company,dc=com")
objUser.PutEx ADS_PROPERTY_DELETE, _
  "otherHomePhone", Array("(123) 555-1212")
objUser.SetInfo

If you just want to delete everything, use this:

Const ADS_PROPERTY_CLEAR = 1
Set objUser = GetObject _
  ("LDAP://cn=donj,ou=Legal, dc=company,dc=com")
objUser.PutEx ADS_PROPERTY_CLEAR, "otherHomePhone")
objUser.SetInfo

In each case, what changes is the special code for the action you're performing: This tells PutEx to append, delete, or clear the attribute entirely. Using PutEx and GetEx() to work with multi-valued attributes is easy once you know how.

About the Author

Don Jones is a multiple-year recipient of Microsoft’s MVP Award, and is an Author/Evangelist for video training company Pluralsight. Don is also a co-founder and President of PowerShell.org, a community dedicated to Microsoft’s Windows PowerShell technology. Don has more than two decades of experience in the IT industry, and specializes in the Microsoft business technology platform. He’s the author of more than 50 technology books, an accomplished IT journalist, and a sought-after speaker and instructor at conferences worldwide. Reach Don on Twitter at @concentratedDon, or on Facebook at Facebook.com/ConcentratedDon.

comments powered by Disqus
Most   Popular

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.