Event ViewerHow to script Log Event creation? … By default the vbscript does not allow to log events with custom Source or custom Event ID; however the command line tool eventcreate does it.

 

Please find default and custom way of writing in Event Viewer log.

' ############ Event Types for LogInEV_A
' Ref: http://technet.microsoft.com/en-us/library/ee156617.aspx
Const EVENT_SUCCESS = 0
Const EVENT_ERROR = 1
Const EVENT_WARNING = 2
Const EVENT_INFORMATION = 4
Const EVENT_AUDITSUCCESS = 8
Const EVENT_AUDITFAILURE = 16

' ############ Event Types for LogInEV_B
' Ref: http://technet.microsoft.com/en-us/library/bb490899.aspx
Const EVENT_SUCCESS_B = "SUCCESS"
Const EVENT_ERROR_B = "ERROR"
Const EVENT_WARNING_B = "WARNING"
Const EVENT_INFORMATION_B = "INFORMATION"
Const EVENT_SUCCESSAUDIT_B = "SUCCESSAUDIT"
Const EVENT_FAILUREAUDIT_B = "FAILUREAUDIT"

Dim EVENT_ID
EVENT_ID = 100 ' Custom Event ID number from the range 1 - 1000

' ############ Usage:

LogInEV_A EVENT_INFORMATION,"Test WSH Event" ' Logs an Event ID 4 in Application log with source WSH

LogInEV_B EVENT_INFORMATION_B, EVENT_ID, "Application", "Test Custom Event", "MyApp" ' Logs event in custom log with custom source and custom event_id

' ############ Subs:

Sub LogInEV_A(EventType, strMessage)
Set objWsShell = CreateObject("Wscript.Shell")
objWsShell.LogEvent EventType, strMessage
Set objWsShell = Nothing
End Sub

Sub LogInEV_B(EventType, Event_ID, strEventLogName, strMessage, strSource)
Set objWsShell = CreateObject("Wscript.Shell")
strCommand = "eventcreate /T " & EventType & " /ID " & Event_ID & " /L " & strEventLogName & " /SO " & strSource & " /D " & Chr(34) & strMessage & Chr(34)
objWsShell.Run strcommand

Set objWsShell = Nothing
End Sub

Kind Regards,
Witold Lawacz (Ławacz)