Recipe 16.1. Creating an Event


Problem

You want to write an event to an event log. This can be useful if you want to document certain actions you've performed on a workstation.

Solution

Using a command-line interface

The following command writes an event to the Application event log with event ID 999 and source SysAdmin:

> eventcreate /T <EventType> /ID <EventID> /L <LogName> /SO <EventSource>  /D "<EventDescr>"

For example:

> eventcreate /T INFORMATION /ID 999 /L Application /SO SysAdmin /D  "Restarting system after service pack install"

Using VBScript
' This code creates an event in the Application event log. ' ------ SCRIPT CONFIGURATION ------ strComputer = "\\<ComputerName>" ' e.g. wks01  strDescr = "<EventDescr>"    ' e.g. Restarting computer after installing SP2 ' ------ END CONFIGURATION --------- Const EVENT_SUCCESS = 0 Const EVENT_ERROR   = 1  Const EVENT_WARNING = 3 Const EVENT_INFO    = 4 set objWSHShell = Wscript.CreateObject("Wscript.Shell") boolRC = objWSHShell.LogEvent(EVENT_INFO, strDescr, strComputer) if boolRC = TRUE then    WScript.Echo "Successfully created event." else    WScript.Echo "Failed to create event." end if

Discussion

Using a graphical user interface

None of the graphical tools (e.g., Event Viewer snap-in) allow you to create a custom event log message.

Using a command-line interface

To create an event on a remote machine, specify the /S option followed by the host name for eventcreate. Use /U and /P, respectively, to specify an alternate user account and password with eventcreate.

Using VBScript

With WSH, you can create an event only in the Application log. The WMI Event Log classes do not support creating custom event log messages.

See Also

MS KB 324145, "HOW TO: Create Custom Events"



Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

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