When working with Active Directory, the first step
is to connect the instance of Active Directory. The basic syntax to
connect with a Visual Basic (VB) script is
set objdom =getobject("LDAP://dn")
Note
LDAP, which is an acronym for Lightweight Directory Access Protocol, must be in all capital letters.
set objdom = getobject("LDAP://dc=pearson,dc=pub")
2. Creating an OU with a VB Script
The following script creates an OU named Project Team:
set objdom = getobject("LDAP://dc=pearson,dc=pub")
set objou = objdom.create("organizationalunit","ou=Project Team")
objou.setinfo
The following table explains each of these three lines.
Creating an OU | Comments |
---|
set objdom = getobject("LDAP:// dc=pearson,dc=pub")
| This line sets the context to the pearson.pub domain. It creates an object named objdom and uses the getobject method to connect to the pearson.pub domain.
Note
LDAP must be all uppercase.
|
set objou = objdom.create ("organizationalunit", "ou=Project Team")
| This command uses the create method of the objdom object to create an OU named Project Team. However, the OU isn’t created yet.
Note
The case of Project Team can be uppercase, lowercase, or any
combination. However, it is displayed the way it’s included in this
line.
|
objou.setinfo | The setinfo method creates the previously defined OU. |
You can use the following steps to create this script.
Step | Action |
---|
1. | Start a command prompt. |
2. | Type the following text, and then press Enter:
notepad c:\scripts\createou.vbs
|
3. | When prompted to create the file, click Yes. |
4. | Add the following three lines to the script:
set objdom = getobject("LDAP://dc=pearson,dc=pub") set objou = objdom.create("organizationalunit","ou=Project Team") objou.setinfo
|
5. | Press Ctrl+S to save the script. |
6. | Return to the command prompt, type the following text, and then press Enter:
Cscript c:\scripts\createou.vbs
|
7. | Launch Active Directory Users and Computers from the Administrative Tools menu and you’ll see the Project Team OU created. |
3. Creating a User Account with a VB Script
The following script creates a user named Jackie in the Project Team OU:
set objOU = getobject("LDAP://ou=project team,dc=pearson,dc=pub")
set objuser = objou.create("user", "cn=Jackie")
objuser.put "samaccountname", "Jackie"
objuser.setinfo
The following table explains each of these lines.
Creating an OU | Comments |
---|
set objOU = getobject("LDAP:// ou=project team,dc=pearson,dc=pub")
| This line sets the context to the pearson.pub domain. It creates an object named objdom and uses the getobject method to connect to the pearson.pub domain.
Note
LDAP must be all uppercase.
|
set objuser = objou. create("user", "cn=Jackie")
| This command uses the create method of the objdom object to create a user named Jackie. However, the user isn’t created yet.
Note
The case of Jackie can be uppercase, lowercase, or any combination. However, it will be displayed the way it’s included in this line.
|
objuser.put "samaccountname", "Jackie" | You can assign values to any properties with the put method of the user object. This line sets the value of the samaccountname property to Jackie. |
objou.setinfo | The setinfo method creates the previously defined user. |