读取特定的Windows事件日志事件

Zac*_*own 11 python windows event-log

我正在研究一个程序,需要知道如何根据记录号读取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
Run Code Online (Sandbox Code Playgroud)

Dou*_* R. 11

我意识到这是一个老问题,但我遇到过它,如果我这样做了,其他人也可能.

您还可以编写自定义查询,这些查询允许您通过脚本(包括事件ID)查询任何WMI参数.它还有一个好处,就是让你拉出并清除所有那些VBS WMI查询.实际上,我比其他任何人更频繁地使用此功能.例如,请参阅:

以下是在应用程序日志中查询特定事件的示例.我没有充实它,但您也可以构建WMI时间字符串并查询特定日期/时间之间或之后的事件.

#! py -3

import wmi

def main():
    rval = 0  # Default: Check passes.

    # Initialize WMI objects and query.
    wmi_o = wmi.WMI('.')
    wql = ("SELECT * FROM Win32_NTLogEvent WHERE Logfile="
           "'Application' AND EventCode='3036'")

    # Query WMI object.
    wql_r = wmi_o.query(wql)

    if len(wql_r):
        rval = -1  # Check fails.

    return rval



if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)


pyf*_*unc 7

没有!没有可用的功能允许您根据事件ID获取事件.

参考:事件记录功能

GetNumberOfEventLogRecords  Retrieves the number of records in the specified event log.
GetOldestEventLogRecord     Retrieves the absolute record number of the oldest record 
                            in the specified event log.
NotifyChangeEventLog        Enables an application to receive notification when an event
                            is written to the specified event log.

ReadEventLog                Reads a whole number of entries from the specified event log.
RegisterEventSource         Retrieves a registered handle to the specified event log.
Run Code Online (Sandbox Code Playgroud)

只有其他感兴趣的方法是阅读最古老的事件.

你将不得不以任何方式迭代结果,你的方法是正确的:)

您只能更改下面的方法形式,但这是不必要的.

events = win32evtlog.ReadEventLog(hand, flags,0)
events_list = [event for event in events if event.EventID == "27035"]
if event_list:
    print 'Event Category:', events_list[0].EventCategory
Run Code Online (Sandbox Code Playgroud)

这和你做的一样,但更简洁

  • 我不知道发布此答案时的状态是什么,但这不再正确(请参阅下面我的答案 - 如果您可以编写 WMI 查询,则可以查询它)。 (2认同)

小智 7

现在有一个python库(python 3及以上版本)可以执行你所谓的winevt.您正在寻找的内容可以通过以下方式完成:

from winevt import EventLog
query = EventLog.Query("System","Event/System[EventID=27035]")
event = next(query)
Run Code Online (Sandbox Code Playgroud)