Logging an Event

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

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

ValueEvent Type
0SUCCESS
1ERROR
2WARNING
4INFORMATION
8AUDIT_SUCCESS
16AUDIT_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).


send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net