In this exercise, you modify the delete user script from the previous step-by-step exercise and write the resulting output to the event log instead of to a pop-up dialog box. This results in an enterprise type of solution because the script could be scheduled, or the script might delete a large number of users, in which case writing output to a dialog box or even to a command prompt would be impractical. The event log always exists, so it is a convenient place to log information. Only three lines of code are required to implement writing to the event log.
1. | Open Notepad or your favorite script editor.
|
2. | Open \My Documents\Microsoft Press\VBScriptSBS\ch12\OneStepFurther\DeleteUser.vbs file and save it as YourNameDeleteUserLogged.vbs. This will ensure you have a fresh working copy of the script and will give you a fallback option if required.
|
3. | If MyNewUser does not exist in the MrEd OU, then run the CreateUser.vbs script to create the user you will be deleting.
|
4. | Delete the WScript.Echo line that is at the bottom of the script. This line looks like the following:
WScript.Echo("User " & oUname & " was deleted")
|
5. | Add two new variables. The first variable is objShell and is used to hold the connection to the scripting shell object. The second variable is oMessage and holds the text of the message you write to the event log. These two declarations look like the following:
Dim objShell 'holds connection to scripting shell
Dim oMessage 'holds text of the message we write
|
6. | Assign the value oUname & "was deleted" to the oMessage variable.
oMessage = oUname & " was deleted"
|
7. | Now define a constant called EVENT_SUCCESS and set it equal to 0. The code to do this looks like the following:
|
8. | |
9. | At the bottom of the script where the WScript.Echo command used to reside, use the CreateObject method to create an instance of the scripting shell. Set the handle equal to objShell. The code to do this looks like the following:
Set objShell = CreateObject("WScript.Shell")
|
10. | Use the LogEvent method to write your message to the event log. You're interested in only a return code of 0, which indicates a success. (Complete information on the LogEvent method is available in the WSH 5.6 help file, script56.chm, in \My Documents \Microsoft Press\VBScriptSBS\Resources.) The code looks like the following:
objShell.LogEvent EVENT_SUCCESS, oMessage
|
11. | Save the script and run it.
|
12. | Notice that there is no feedback. However, if you open the application log on the machine running the script, you see the event message. This is quite useful because the event message allows you to log updates as well as to audit them. The log looks like the one in Figure 12-7.

|
13. | Open Active Directory Users And Computers to verify the user was deleted.
|
14. | If your script does not perform as expected, compare your script with the DeleteUserLogged.vbs script in the Chapter 12 One Step Further folder.
|
To | Do This |
---|
Easily delete users | Modify the script you used to create the user and change the Create method to Delete |
Commit changes to Active Directory when deleting a user | Nothing special is required; changes take place when deleted |
Find country codes used in Active Directory Users And Computers | Use ISO 3166 |
Modify a user's first name via ADSI | Add a value to the GivenName attribute; use the SetInfo method to write the change to Active Directory |
Overwrite a field that is already populated in Active Directory | Use the Put method |
Modify terminal server profile settings in Active Directory | Use the IADsTSUserEx object |
Assign a value to a terminal server profile attribute after making a connection into Active Directory | Assign the value to the property; no need to use the Put method |
Delete the value of an attribute in Active Directory | Use the Delete method |