Export (0) Print
Expand All

Logging an Event

Microsoft® Windows® 2000 Scripting Guide

Troubleshooting applications and services is simplified if these applications and services log important events to event logs. Your scripts will also be easier to troubleshoot if they do the same.

The WshShell object provides the LogEvent method for logging events to the Application event log. The LogEvent method enables you to write to the event log from within your scripts.

Note

  • If you want to read and process event log information from within your scripts, you need to use WMI. For more information about using WMI to work with event logs, see "Logs" in this book.

LogEvent has two required parameters. The first parameter of the LogEvent method is an integer that specifies the type of event you would like your script to log. Table 3.14 lists the available event types and their corresponding integer values.

Table 3.14 Event Types and Integer Values

Value

Event Type

0

SUCCESS

1

ERROR

2

WARNING

4

INFORMATION

8

AUDIT_SUCCESS

16

AUDIT_FAILURE

The second parameter your scripts need to supply to the LogEvent method is a string containing the message to log. You also have the option to supply a computer name as the third parameter, in which case the event will be logged in the Application log on that computer instead of the computer on which the script is running. (This parameter is ignored on Windows 95- and Windows 98-based computers.) Remote logging is useful for scripts designed to run on a number of different computers: Instead of being logged locally, all events generated by these scripts can be logged to a central computer.

The script in Listing 3.26 logs an event of each of the types listed in Table 3.14 along with a corresponding description.

Listing 3.26 Logging Events to the Application Log

  
1
2
3
4
5
6
7


Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.LogEvent 0,"Test Success Event"
objShell.LogEvent 1,"Test Error Event"
objShell.LogEvent 2,"Test Warning Event"
objShell.LogEvent 4, "Test Information Event"
objShell.LogEvent 8, "Test Success Audit Event"
objShell.LogEvent 16, "Test Failure Audit Event"

The LogEvent method can record events only in the Application event log. In addition, you cannot specify a unique source or a unique event code: All events are automatically given the source Windows Script Host and an event code corresponding to the event type (for example, all Success events will have an event code of 0).

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft