views:

801

answers:

1

Hello, I have written a VBScript to enumerate events from the event log on a particular day.

The first query select from the NT event log events between todays date and yesterdays date,

Set colEvents = objWMIService.ExecQuery _
    ("Select * from Win32_NTLogEvent Where TimeWritten >= '" _ 
        & dtmStartDate & "' and TimeWritten < '" & dtmEndDate & "'")

Then from the query above i want to extract event id's from a log file.

For Each objEvent in colEvents
    If objEvent.Eventcode = EventNu And (objEvent.LogFile = EventLog) Then

I have placed the following into the script and it works, however I want to use arguments instead via command line (i.e. EventLogCheck.vbs EventNumber LogFile )but if i use the arguments secion of the script no items are returned. This is driving me nuts. The full script below uses variables, i have commented out the arguments section, but you can uncomment them and play around with it. What am i doing wrong? Thanks for any help!

Const CONVERT_TO_LOCAL_TIME = True
Dim EventLog

EventNu = 18
EventLog = "System"

'Input from the command line
'If Wscript.Arguments.Count <= 1 Then
'   Wscript.Echo "Usage: EventLogCheck.vbs EventNumber LogFile"
'   Wscript.Quit
'End If

'EventNu = WScript.Arguments.Item(0)
'EventLog = WScript.Arguments.Item(1)

'For Each Computer In Wscript.Arguments

Set dtmStartDate = CreateObject("WbemScripting.SWbemDateTime")
Set dtmEndDate = CreateObject("WbemScripting.SWbemDateTime")
'DateToCheck = CDate("5/18/2009")
DateToCheck = date
dtmStartDate.SetVarDate DateToCheck, CONVERT_TO_LOCAL_TIME
dtmEndDate.SetVarDate DateToCheck + 1, CONVERT_TO_LOCAL_TIME

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colEvents = objWMIService.ExecQuery _
    ("Select * from Win32_NTLogEvent Where TimeWritten >= '" _ 
        & dtmStartDate & "' and TimeWritten < '" & dtmEndDate & "'") 

For Each objEvent in colEvents
 If objEvent.Eventcode = EventNu And (objEvent.LogFile = EventLog) Then
    'Wscript.Echo "Category: " & objEvent.Category
    Wscript.Echo "Computer Name: " & objEvent.ComputerName
    Wscript.Echo "Event Code: " & objEvent.EventCode
    Wscript.Echo "Message: " & objEvent.Message
   ' Wscript.Echo "Record Number: " & objEvent.RecordNumber
   ' Wscript.Echo "Source Name: " & objEvent.SourceName
    Wscript.Echo "Time Written: " & objEvent.TimeWritten
    Wscript.Echo "Event Type: " & objEvent.Type
   ' Wscript.Echo "User: " & objEvent.User
    Wscript.Echo objEvent.LogFile
   End if
Next
'Next
WScript.Echo EventNu
WScript.Echo EventLog
+2  A: 

The arguments passed are treated as being of type string. However, EventNu should be an integer. You therefore have to convert the arguments to the correct type using CInt and CStr:

EventNu = CInt(WScript.Arguments.Item(0))
EventLog = CStr(WScript.Arguments.Item(1))
0xA3
OMG that worked! Many Thanks Mate.
Nasa