Windows的事件檢視器可以把大小事都記錄下來,但是卻沒有提供良好的工具去搜尋,所以只好寫程式來輔助。
起始肇因公司要求對某些目錄檔案進行稽核記錄,但是這些檔案使用又非常頻繁,所以當發現需要檢視事件記錄時,常有上萬筆記錄;而事件檢視器並不提供針對檔名或是訊息當中的字串做為篩選條件,只能用事件代碼;要針對某檔案進行稽核時,就成了不可能的任務!
在以下程式中,有幾個參數要設定
strComputer 是指要被搜尋事件檢視器記錄的電腦名稱或 IP Address
strSearchString 是要被搜尋的字串
dtmLogDate 是匯出的檔名,我個人習慣在匯出檔案名稱加上日期,以方便處理;看您個人的習慣囉
strComputer = "DFS1" strSearchString = “SHARE” dtmLogDate = ‘20120213” Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Security)}!\\" & strComputer & "\root\cimv2") Set colLoggedEvents = objWMIService.ExecQuery ("Select * FROM Win32_NTLogEvent WHERE Logfile = 'Security'") Const ForWriting = 2 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objLogFile = objFSO.CreateTextFile("FileLog" & dtmLogDate & ".csv", ForWriting, True) objLogFile.Write "Record Number#" objLogFile.Write "Date Time#" objLogFile.Write "Message" objLogFile.WriteLine For Each objEvent in colLoggedEvents if InStr(objEvent.Message,strSearchString) > 0 then objLogFile.Write objEvent.RecordNumber & "#" objLogFile.Write objEvent.TimeGenerated & "#" objEvent.Message = replace(objEvent.Message,vbCRLF,"") objLogFile.Write objEvent.Message objLogFile.WriteLine end if Next objLogFile.Close msgbox("Finish !") |