我正在研究一個程序,並且需要知道如何根據記錄編號讀取Windows事件日誌的特定條目,該腳本已具有此記錄編號。以下是我一直在使用的代碼,但我不想循環所有事件,直到找到我正在尋找的代碼。有任何想法嗎?閱讀特定的Windows事件日誌事件
import win32evtlog
server = 'localhost' # name of the target computer to get event logs
logtype = 'System'
hand = win32evtlog.OpenEventLog(server,logtype)
flags = win32evtlog.EVENTLOG_BACKWARDS_READ|win32evtlog.EVENTLOG_SEQUENTIAL_READ
total = win32evtlog.GetNumberOfEventLogRecords(hand)
while True:
events = win32evtlog.ReadEventLog(hand, flags,0)
if events:
for event in events:
if event.EventID == "27035":
print 'Event Category:', event.EventCategory
print 'Time Generated:', event.TimeGenerated
print 'Source Name:', event.SourceName
print 'Event ID:', event.EventID
print 'Event Type:', event.EventType
data = event.StringInserts
if data:
print 'Event Data:'
for msg in data:
print msg
break
記得撥打'win32evtlog.CloseEventLog(手)'時,即可大功告成。 – twasbrillig