Writing to Event Logs

Microsoft® Windows® 2000 Scripting Guide

The LogEvent method in WSH allows you to write events to the Application Log. When writing these events, you can specify the information shown in Table 12.6.

Table 12.6 LogEvent Method Parameters

Specification

Description

Event type

Event types include the following:

0 - Success

1 - Error

2 - Warning

4 - Information

8 - Audit success (generally not used in the Application Log)

16 - Audit failure (generally not used in the Application Log)

Event description

Any text message describing the event.

Name of the computer to which the event will be recorded

If not specified, the event will be recorded to the Application Log on the computer on which the script is being run. If you prefer to record the event in the Application Log on a remote computer, enclose the Universal Naming Convention (UNC) name of the computer in quotation marks (for example, "\\PrimaryServer").

Before you attempt to record events to a remote computer, make sure the user running the script has permission to write events to the remote event log.

Scripting Steps

There are multiple ways to write an event to an event log. You can do this by:

  • Writing an event to an event log on the local computer.

  • Writing an event to an event log on a remote computer.

Writing an event to an event log on the local computer

Listing 12.13 contains a script that writes a Success event (event type 0) to the Application event log on the local computer. To carry out this task, the script must perform the following steps:

  1. Create a constant EVENT_SUCCESS and set the value to 0.

  2. Create an instance of the WSH Shell object.

  3. Use the LogEvent method to write the record to the event log, specifying two parameters:

    • EVENT_SUCCESS - Constant that indicates the type of event.

    • "Payroll application successfully installed." - The event description.

    Because this event will be written to the local computer, the computer name (an optional, third parameter) does not have to be specified.

Listing 12.13 Writing an Event to the Application Log

  
1
2
3
4
Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, _
"Payroll application successfully installed."

Writing an event to an event log on a remote computer

To record the event to the Application Log on a remote computer, add the UNC computer name as an optional, third, parameter to the LogEvent method. Listing 12.14 contains a script that writes the same event to the Application Log on the remote computer \\PrimaryServer.

Listing 12.14 Writing an Event to the Application Log on a Remote Computer

  
1
2
3
4
Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, _
"Payroll application successfully installed." , "\\PrimaryServer"