Previous Page
Next Page

Creating OUs Step-by-Step Exercises

In this section, you are going to practice creating OUs. The result will eventually become a subroutine that can be employed in other scripts to create OUs.

Important

To successfully complete this section, you must have access to a Microsoft Windows Server 2003 or later Active Directory Domain Controller. You must know the name of the domain, and you must have rights to create objects in that domain.


These step-by-step instructions do not apply to Microsoft Windows Vista or Microsoft Windows XP workstations.

1.
Open Notepad or your favorite script editor.

2.
On the first line, type Option Explicit.

3.
Declare the following variables: provider, domain, oClass, oOU, objDomain, objOU, oOUname, and oDescription.

4.
Assign the LDAP provider to the variable called provider. Your code will look like the following:

provider = "LDAP://"

5.
Assign the name of a domain that is accessible on your network, such as nwtraders.msft, to the domain variable. Split each section of the domain name into domain components. This will look like the following:

domain = "dc=nwtraders,dc=msft"

6.
Assign the variable to the organizationalUnit class. Make sure you encase the class name in quotation marks, as shown here:

oClass = "organizationalUnit"

7.
Assign the value ou= to the variable oOU, as seen below:

oOU = "ou="

8.
Assign a value to the variable used to hold the OU name. In this case, the variable is oOUname and the value is Lab22. The code will look like the following:

oOUname = "Lab22"

9.
Assign an appropriate description to the oDescription variable. It will look something like the following:

oDescription = "For Lab 22 Use"

10.
Use the Set command to set the variable objDomain equal to the handle that comes back from using the GetObject method when using the provider variable and the domain variable. The code will look like the following:

Set objDomain = GetObject(provider & domain)

11.
Use the Set command to set the variable objOU equal to the handle that comes back from using the Create method when given the oClass, oOU, and oOUname variables. The code will look like the following:

Set objOU = objDomain.create(oClass, oOU & oOUname)

12.
Use the Put method to put the data contained in the oDescription variable into the field designated as Description. Separate the variable from the field name with a comma. The code will look like the following:

objOU.Put "description", oDescription

13.
Use the SetInfo method to commit the changes to Active Directory. The code will look like the following:

objOU.SetInfo

14.
Conclude your script by using WScript.Echo to echo out the name of oOUname and an appropriate description of the action that was taken. I used the following code to do this:

WScript.Echo("OU " & oOUname & " was created")

15.
Save the script as YourNameCreateOU.vbs.

16.
Run the script. For this script, it doesn't matter whether you run it in CScript or from WScript. It's probably easier to just double-click the script and let it run in WScript.

17.
Open Active Directory Users And Computers to verify the presence of the Lab22 OU.

18.
Right-click the Lab22 OU and choose Properties from the Action menu. On the General tab, verify that the description you assigned in step 11 is present in the Description field.

19.
Close everything out. Do not delete the Lab22 OU because you'll use it in the next exercise.


Previous Page
Next Page