Next, the major part of the script processes each row of spreadsheet data and updates the AD objects as necessary. (Web Listing 1 at http://www.winscriptingsolutions.com, InstantDoc ID 39117, shows a sample of this part of the script.) For performance reasons, the script updates only those AD object attribute values that differ from existing values.
First, the script uses the Range object's Cell property to access the Excel worksheet and retrieve the first AD object's DN value. Next, the script tries to get a reference to that object by making a GetObject call for that DN and storing the reference in the ADObject variable. If the call is successful, the script uses the Range object's Cell property to read in the updated values for each attribute in the attributeNames array, using the i (row) and j (column) loop variables to specify the offset of each cell. To retrieve each attribute's existing value from AD, the script uses the Get function of the IADs object that the ADObject variable references. For each set of values, the script uses the LCase function to convert both strings to lowercase letters and compares the resulting strings to determine whether they're identical. If the values differ, the script uses the IADs object's Put method to update the attribute's value, passing the attribute name and attribute value from the worksheet as parameters. After the script updates an AD object, the script uses the SetInfo method to commit the changes, as the following code exemplifies.
If (changeMade = True) Then
ADObject.SetInfo
End If
After the script processes each row of data in the spreadsheet, it outputs a summary of the number of rows processed, AD objects updated, attributes updated, and errors encountered. The script then closes all the input and output files and quits the instance of the Excel Application object. Listing 4 shows this cleanup code.
Updateattribs.vbs is fairly straightforward. However, successful execution relies on the following tips:
- The Put method updates only the first value of a multivalued attribute, so this script might not work well if many of the attributes you need to update are multivalued. Before you execute the script, examine the attributes you plan to update and determine whether this limitation will pose a problem.
- Updateattribs.vbs contains string comparisons that might not make sense for binary values, so you can't use the script to update these values. Furthermore, most (if not all) of the binary values available in AD (e.g., globally unique identifiersGUIDs, passwords) shouldn't be updated in this fashion.
- Exit all running instances of Excel before you execute the script, and don't open Excel while the script is running.
- Use CScript to run the script.
- Specify the full path of your input file and enclose the path in quotation marks (") if it contains any spaces.
- Make sure the DN values in the input file are correct. To retrieve these values from AD, you can use LDAP browser tools such as LDP, ADSI Edit, or PADLE-LDAP Explorer (http://www.scripthorizon.com).
- If possible, run the script in a test environment before executing it in production to verify that it updates the specified attributes as you expected.
The Power of Scripting
Updating AD objects' attributes doesn't need to be a tedious and error-prone task. The next time your boss hands you a spreadsheet with 1000 employee addresses and phone numbers and asks you to update the information in AD, you can confidently say that you'll be finished in no time.
End of Article
Prev. page
1
[2]
next page -->