2014-02-20 3 views
0

시스템에서 이벤트 로그를 가져와 Excel에 쓸 수있는 아래 스크립트가 있습니다. 'Windows 7'에서는 잘 돌아가고 있지만 'Windows Server 2003'에서는 시스템 로그를 작성하는 데 7 ~ 8 분이 걸리고 초 이내에 응용 프로그램 로그가 기록됩니다. 그러나 시스템 로그에 오류가 거의 없습니다.VB 스크립트 도움말을 작성하여 이벤트 로그를 작성하십시오.

또 다른 문제는 스크립트에서 MyDate = DateAdd ("h", -8, Now())를 사용하고 있지만 12 시간 이상 전에 로그를 가져 오는 것입니다. 이 시간 계산이 올바르게 작동하지 않습니다.

귀하의 도움을 매우 높이 평가할 것입니다.

Option Explicit 

Dim objFSO, objFolder, objFile, objWMI, objItem, objItem1, objItem2 ' Objects 
Dim strComputer, strFileName, strFileOpen, strFolder, strPath, oExcel, oWB, oSheet, oSheet1, oSheet2 
Dim intEvent, intNumberID, intRecordNum, colLoggedEvents, colLoggedEvents2, colLoggedEvents3, MyDate, dtm, row,  row1, row2, Query, ServerTime 
MyDate = DateAdd("h", -8, Now()) 

'--------------------------------------------------------- 

On Error Resume Next 

Set oExcel=CreateObject("Excel.Application") 
oExcel.Visible=true 
Set oWB=oExcel.Workbooks.Open ("D:\EventLogs2.xls") 
Set oSheet=oWB.Worksheets.Add (, oWB.WorkSheets(oWB.WorkSheets.Count)) 
Set oSheet1=oWB.Worksheets.Add (, oWB.WorkSheets(oWB.WorkSheets.Count)) 
Set oSheet2=oWB.Worksheets.Add (, oWB.WorkSheets(oWB.WorkSheets.Count)) 
oSheet.Name="Application" 
oSheet1.Name="Security" 
oSheet2.Name="System" 

strComputer = "." 
ServerTime = Now    

intRecordNum = 0 
row = 0 
row1 = 0 
row2 = 0 

' ---------------------------------------------------------- 
' WMI Core Section 
Set objWMI = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate,(Security)}!\\" _ 
& strComputer & "\root\cimv2") 

Set colLoggedEvents = objWMI.ExecQuery _ 
     ("SELECT * FROM Win32_NTLogEvent where Logfile = 'Application' and " _ 
     & "EventType = '1' and TimeWritten > '" & MyDate & "'") 
Set colLoggedEvents2 = objWMI.ExecQuery _ 
     ("SELECT * FROM Win32_NTLogEvent where Logfile = 'Security' and " _   
     & "EventType = '1' and TimeWritten > '" & MyDate & "'") 
Set colLoggedEvents3 = objWMI.ExecQuery _ 
     ("SELECT * FROM Win32_NTLogEvent where Logfile = 'System' and " _ 
     & "EventType = '1' and TimeWritten > '" & MyDate & "'") 

' ---------------------------------------------------------- 
' Next section loops through ID properties 

For Each objItem in colLoggedEvents          
If objItem.EventType=1 then               
row = row+1 
osheet.Cells(row,1).Value = ("Logfile: " & objItem.Logfile _ 
& " source " & objItem.SourceName) 
osheet.Cells(row,2).Value = ("Message: " & objItem.Message) 
osheet.Cells(row,3).Value = ("TimeGenerated: " & WMIDateStringToDate(objItem.TimeGenerated)) 
osheet.Cells(row,4).Value = ServerTime 

End If 
Next 


For Each objItem1 in colLoggedEvents2 
If objItem1.EventType=1 then 
row1 = row1+1 
osheet1.Cells(row1,1).Value = ("Logfile: " & objItem1.Logfile _ 
& " source " & objItem1.SourceName) 
osheet1.Cells(row1,2).Value = ("Message: " & objItem1.Message) 
osheet1.Cells(row1,3).Value = ("TimeGenerated: " & WMIDateStringToDate(objItem1.TimeGenerated)) 
osheet1.Cells(row1,4).Value = ServerTime 

End If 
Next                


For Each objItem2 in colLoggedEvents3   
If objItem2.EventType=1 then 
row2 = row2+1 
osheet2.Cells(row2,1).Value = ("Logfile: " & objItem2.Logfile _ 
& " source " & objItem2.SourceName) 
osheet2.Cells(row2,2).Value = ("Message: " & objItem2.Message) 
osheet2.Cells(row2,3).Value = ("TimeGenerated: " & WMIDateStringToDate(objItem2.TimeGenerated)) 
osheet2.Cells(row2,4).Value = ServerTime 

intRecordNum = intRecordNum +1 
End If 
Next 

Function WMIDateStringToDate(dtmDate) 
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _ 
Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _ 
& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2)) 
End Function 

oWB.save 
oWB.Application.Quit 
WScript.Quit 
+0

7-8 무엇? 의사록? 시간? 갤런? 스크립트에서 어떤 단계가 너무 많은 시간을 소비하는지 (예 : 각 단계 전후에 타임 스탬프 기록) 결정 했습니까? –

+0

죄송합니다. 7-8 분입니다. –

+0

스크립트의 어느 단계가 너무 많은 시간을 소비하는지 결정 했습니까? –

답변

0

는 그냥 CSV 파일을 쓸 수 없습니다 : 여기

스크립트입니까? 이 같은

뭔가 :

Option Explicit 

Dim objFSO, objFolder, objFile, objWMI, objItem, objItem1, objItem2 ' Objects 
Dim strComputer, strFileName, strFileOpen, strFolder, strPath, oExcel, oWB, oSheet, oSheet1, oSheet2 
Dim intEvent, intNumberID, intRecordNum, colLoggedEvents, colLoggedEvents2, colLoggedEvents3, MyDate, dtm, row,  row1, row2, Query, ServerTime 
MyDate = DateAdd("h", -8, Now()) 

'--------------------------------------------------------- 

On Error Resume Next 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.CreateTextFile("D:\EventLogs2.csv", True) 

strComputer = "." 
ServerTime = Now    

intRecordNum = 0 
row = 0 
row1 = 0 
row2 = 0 

' ---------------------------------------------------------- 
' WMI Core Section 
Set objWMI = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate,(Security)}!\\" _ 
& strComputer & "\root\cimv2") 

Set colLoggedEvents = objWMI.ExecQuery _ 
     ("SELECT * FROM Win32_NTLogEvent where Logfile = 'Application' and " _ 
     & "EventType = '1' and TimeWritten > '" & MyDate & "'") 
Set colLoggedEvents2 = objWMI.ExecQuery _ 
     ("SELECT * FROM Win32_NTLogEvent where Logfile = 'Security' and " _   
     & "EventType = '1' and TimeWritten > '" & MyDate & "'") 
Set colLoggedEvents3 = objWMI.ExecQuery _ 
     ("SELECT * FROM Win32_NTLogEvent where Logfile = 'System' and " _ 
     & "EventType = '1' and TimeWritten > '" & MyDate & "'") 

' ---------------------------------------------------------- 
' Next section loops through ID properties 

For Each objItem in colLoggedEvents          
If objItem.EventType=1 then               
    objFile.WriteLine("Logfile: " & objItem.Logfile & "," & " source " & objItem.SourceName & "," & _ 
     "Message: " & objItem.Message & "," & _ 
     "TimeGenerated: " & WMIDateStringToDate(objItem.TimeGenerated) & "," & _ 
     ServerTime) 
End If 
Next 


For Each objItem1 in colLoggedEvents2 
If objItem1.EventType=1 then 
    objFile.WriteLine("Logfile: " & objItem1.Logfile & "," & " source " & objItem1.SourceName & "," & _ 
     "Message: " & objItem1.Message & "," & _ 
     "TimeGenerated: " & WMIDateStringToDate(objItem1.TimeGenerated) & "," & _ 
     ServerTime) 
End If 
Next                


For Each objItem2 in colLoggedEvents3   
If objItem2.EventType=1 then 
    objFile.WriteLine("Logfile: " & objItem2.Logfile & "," & " source " & objItem2.SourceName & "," & _ 
     "Message: " & objItem2.Message & "," & _ 
     "TimeGenerated: " & WMIDateStringToDate(objItem2.TimeGenerated) & "," & _ 
     ServerTime) 
End If 
Next 

Function WMIDateStringToDate(dtmDate) 
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _ 
Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _ 
& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2)) 
End Function 

objFile.Close 
Set objFile = Nothing 
Set objFSO = Nothing 
+0

나는 이것을 시도하고 당신에게 알려주겠다. –

+0

@IftakharAhmad 확인. 천만에요. – Toothbrush

+0

스크립트가 44, 54, 64 줄에 오류를 던졌습니다. 공명은 '&'가 누락되었습니다. 나는 '&'을 추가했지만 스크립트를 실행해도 아무런 오류가 발생하지 않습니다. –

관련 문제