Previous Page
Next Page

Deleting Users Step-by-Step Exercises

In this section, you will practice deleting users. You begin with a starter file that is used to create the user. This is a good practice because you can ensure that all created users get deleted when the time comes. While working on your script, if you need to run the script several times, you can use the \My Documents\Microsoft Press\VBScriptSBS\ch12\StepByStep\sbsStarter.vbs file to create your user prior to deleting the user. If the user isn't present when you try deletion, you get an error.

1.
Open Notepad or your favorite script editor.

2.
Open sbsStarter.vbs and save it as YourNameDeleteUser.vbs.

3.
Delete the declaration for the variable objUser.

4.
Delete three of the four lines that call objUser in the Worker information section of the script. These lines look like the following:

objUser.Put "sAMAccountName", oUname
objUser.Put "DisplayName", oUname

objUser.SetInfo

5.
Locate the Set objUser line initially used to create the user so that the line now deletes the user instead. The original line looks like the following:

Set objUser = objDomain.create(oClass, oCn & oUname)

6.
Remove the Set objUser portion of the line. It will now look like the following:

objDomain.create(oClass, oCn & oUname)

7.
Change the method called in the preceding line from Create to Delete. The line will now look like the following:

objDomain.Delete(oClass, oCn & oUname)

8.
Save your work. If you try to run the script now, you'll get an error because you need to remove the parentheses. Once removed, the code looks like the following:

objDomain.Delete oClass, oCn & oUname

9.
Change the output message so that it says deleted instead of created. It looks like the following once the change is implemented:

WScript.Echo("User " & oUname & " was deleted")

10.
Save your work.

11.
Open Active Directory Users And Computers to verify that LabUser was deleted.

12.
Run the script. If it fails, run the starter script to ensure there is a user on the server. After this is done, run the script to see whether it works. When it does, run the sbsStarter.vbs script again, because you'll need the user for the next exercise. If it does not run correctly, compare your script with the DeleteUser.vbs script in the Chapter 12 Step-by-Step folder.


Previous Page
Next Page