2009-05-20 24 views
2

我已經寫了一個VBScript來枚舉特定日期事件日誌中的事件。VBScript - 不知道爲什麼我的參數與變量的使用方式不同

第一個查詢從今天的日期和昨天的日期之間的NT事件日誌的事件選擇,

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

然後從上面我想從日誌文件中提取的事件ID的查詢。

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

我已經把以下到腳本和它的作品,但我想通過,而不是命令行(即EventLogCheck.vbs EventNumber日誌文件)使用的參數,但如果我使用的腳本參數secion沒有項目回。這讓我瘋狂。下面的完整腳本使用變量,我已經將參數部分註釋掉了,但是您可以取消註釋並隨意使用它。我究竟做錯了什麼?謝謝你的幫助!

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 

回答

3

傳遞的參數被視爲字符串類型。但是,EventNu應該是一個整數。因此,必須使用CIntCStr的參數正確的類型轉換:

EventNu = CInt(WScript.Arguments.Item(0)) 
EventLog = CStr(WScript.Arguments.Item(1)) 
+0

OMG奏效!許多感謝伴侶。 – Nasa 2009-05-20 14:04:21

相關問題