Sep 22, 2008

VB applet that create event logs on Windows 2000 servers

Recently I've been working on a monitoring solution so I need a tool that can generate event logs to trigger the monitoring agent. There are a few tools from MS Support Tools or Resource Kit Tools that can do this but they all have some limitations. For example, some can create logs for existing sources, the other can't create log for existing source/ID.

What I need is a tool that can create whatever event log I want - whether it's new or existing.

At first, I found System.Diagnostics.EventLog can be used in PowerShell for Windows 2003 servers. However I still have some windows 2000 servers that don't support PowerShell.

I ended up writing a small VB program myself. Compile it in Visual Studio, it will run happily on w2k servers - I just need to install .Net Framework prior.

'Needs Visual Studio to compile the file.
'One is ready for use in "Mom monitoring folder"
'Usage: newLog machineName\NodeName[System|Application|Security|...]\Source\Type[Error|Warning|Information]\EventID\Desc.

Option Explicit On
'Option Strict On

Imports System
Imports System.Diagnostics
Imports System.Threading

Module Module1
Class MySample
Public Shared Sub Main()
Dim parameters As String
Dim arOptions As Array
Dim eventID As Integer
Dim errMsg As String
Dim errType As EventLogEntryType

parameters = Command$()
arOptions = Split(parameters, "\")


' Create an EventLog instance and assign its source.
Dim myLog As New EventLog()
myLog.Log = arOptions(1)
myLog.Source = arOptions(2)
myLog.MachineName = arOptions(0)
eventID = arOptions(4)
errMsg = arOptions(5)
Select Case LCase(arOptions(3))
Case "error"
errType = EventLogEntryType.Error
Case "Warning"
errType = EventLogEntryType.Warning
Case "Information"
errType = EventLogEntryType.Information

End Select


' Write an informational entry to the event log.
myLog.WriteEntry(errMsg, EventLogEntryType.Error, eventID)
End Sub 'Main
End Class 'MySample

End Module