8.1. Creating an Event


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 server.


Using a command-line interface

The following command is available on Windows Server 2003:

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

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

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

The logevent tool, which is available in the Windows 2000 Resource Kit, can also be used to create events:

> logevent -s <EventType> -e <EventID> -r <EventSource> "<EventDescr>"

This example is equivalent to the eventcreate example where I created an event in the Application log:

> logevent -s I -e 999 -r SysAdmin "Restarting server after service pack install"

Using VBScript

' This code creates an event in the Application event log.
strServer = "\\<ServerName>" ' e.g., fs01 
strDescr = "<EventDescr>" ' e.g., Restarting server after service pack install ' ------ 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, strServer) if boolRC = TRUE then WScript.Echo "Successfully created event." else WScript.Echo "Failed to create ...

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.