In this section, you are going to practice creating users. You'll place the user in the OU created in the previous step-by-step exercise.
The result of this One Step Further exercise will eventually become a subroutine that you can employ in other scripts when you need to use Users.
1. | Open Notepad or your favorite script editor.
|
2. | On the first line, type Option Explicit.
|
3. | Declare the following variables: provider, ou, domain, oClass, oCN, objDomain, objUser, oUname, and oDescription.
|
4. | Assign the LDAP provider to the variable provider. It will look like the following:
|
5. | Assign the Lab22 OU to the OU variable. It will look like the following:
|
6. | Assign the domain used in step 5 of the Step-by-Step exercise to the domain variable. This domain should be the one on your local network. Your code will look something like the following:
domain = "dc=nwtraders,dc=msft"
|
7. | Assign the User class to the oClass variable. It will look like the following:
|
8. | Assign the " CN=" value to the oCN variable, as shown here:
|
9. | Assign to the oUname variable the name of the user to be created. For this exercise, we will call the user labUser.
|
10. | Assign an appropriate description for the new user. This entails assigning a value to the oDescription variable:
oDescription = "created for lab22 use"
|
11. | Use the Set command to set the variable objDomain equal to the handle that comes back from using the GetObject function when fed the provider variable, OU variable, and domain variable. The code looks like the following:
Set objDomain = GetObject(provider & OU & domain)
|
12. | Use the Set command to set the variable objUser equal to the handle that comes back from using the Create method when fed the oClass, oCN, and oUname variables. The code will look like the following:
Set objUser = objDomain.Create(oClass, oCN & oUname)
|
13. | Use the Put method to put the data contained in the oUname variable into the field designated as sAMAccountName. Separate the variable from the field name with a comma. The code looks like the following:
objUser.Put "sAMAccountName", oUname
|
14. | Use the Put method to put the data contained in the oUname variable into the field designated as DisplayName. Separate the variable from the field name with a comma. The code looks like the following:
objUser.Put "DisplayName", oUname
|
15. | 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 looks like the following:
objUser.Put "description", oDescription
|
16. | Use the SetInfo method to commit the changes to Active Directory. The code will look like the following:
|
17. | Conclude your script by using WScript.Echo to echo out the name of oUname and an appropriate description of the action that was taken. I used the following code to do this:
WScript.Echo("User " & oUname & " was created")
|
18. | Save the script as YourNameCreateMultiValuedUser.vbs.
|
19. | Run the script. It doesn't matter whether you run this script in CScript or from WScript. It's probably easier to just double-click the script and let it run in WScript.
|
20. | Open Active Directory Users And Computers to verify the presence of the new user. The user will be contained in the Lab22 OU.
|
21. | Right-click the new user and choose Properties from the Action menu. On the General tab, verify that the display name and description you assigned earlier are present.
|
22. | Close everything out.
|