Hack 19 Modify All Objects in the OU

figs/moderate.gif figs/hack19.gif

Use this script to quickly change specific properties of all objects within an organizational unit.

Using GUI tools such as Active Directory Users and Computers to modify the properties of objects stored in Active Directory is a slow process. In Windows 2000, you have to open the properties sheet for each object, switch to the appropriate tab, and make the change; then, you must do it over and over again for other objects. In Windows Server 2003, you can open the properties of multiple objects simultaneously, but not all tabs are available when you do this and only a small number of settings can be modified in this way. It would be nice if there were a faster way of doing this. Using VBScript, this is indeed possible.

The sample script in this hack shows how you can modify the properties of all objects in a specific OU. This particular script modifies the state, address, postal code, and city for all User objects in the Boston OU in the mtit.com domain, but it can easily be customized to modify other properties of objects. This script is particularly useful if you've planned your implementation of Active Directory so that users in the same OU have certain sets of similar properties, such as their business address information.

The Code

Type the following script into Notepad (with Word Wrap disabled) and save it with a .vbs extension as ModifyUsers.vbs. Be sure to customize the second line to specify the OU and domain for your own environment, and customize the Put statements to use the address information appropriate for users in your OU.

Dim oContainer 

Set oContainer=GetObject("LDAP://OU=Boston,DC=mtit,DC=com")

ModifyUsers oContainer


Set oContainer = Nothing

WScript.Echo "Finished"

Sub ModifyUsers(oObject)

Dim oUser

oObject.Filter = Array("user")

For Each oUser in oObject

oUser.Put "st","Your State"

oUser.Put "streetAddress","Your Address"

oUser.Put "postalCode","Your Zip"

oUser.Put "l","Your City"



End Sub

Running the Hack

To run the script, simply create a shortcut to it and double-click on the shortcut. A dialog box will appear, indicating that the script ran successfully. Figure 2-5 shows what the Address tab of the properties sheet for user Bob Smith (who is in the Boston OU) looks like after running the script.

Figure 2-5. Result of running the ModifyUsers.vbs script

?Rod Trent