I have a app that uses win32evtlog to get and display different events and I would like to limit the display to events of a specific level but win32evtlog doesn't return this. It seems that you can convert an event to XML and then pull this info but I can't work out how you get the event from a loop to XML.
I can get up to the following and use it to display data the LogObject has such as LogObject.TimeGenerated
Log = win32evtlog.OpenEventLog('localhost', 'Application')
while 1:
LogObjects = winev32tlog.ReadEventLog(Log, win32evtlog.EVENTLOG_BACKWARDS_READ|wine32vtlog.EVENTLOG_SEQUENTIAL_READ, 0)
if not LogObjects:
break
for LogObject in LogObjects:
I tried the convert using
LogObjectXML = win32evtlog.EvtRender(LogObject, 1)
This unfortunately returns
TypeError: The object is not a PyHANDLE object
So I know I need to get some sort of handle object that I can use to point the EvtRender at the correct event but can't work out how I do that.
This question is quite similar to How retrieve from Python win32evtlog rest of info? but the solution there didn't answer the critical bit of how we convert the object to XML.
--== Edited with information about the XML for CristiFati ==--
Below is an example of an Application event where the event message reads:-
Updated Windows Defender status successfully to SECURITY_PRODUCT_STATE_ON.
The XML as per event viewer is as below
- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
<Provider Name="SecurityCenter" />
<EventID Qualifiers="0">15</EventID>
<Level>4</Level>
<Task>0</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2017-05-23T07:36:27.627108000Z" />
<EventRecordID>49419</EventRecordID>
<Channel>Application</Channel>
<Computer>Name.domain.here</Computer>
<Security />
</System>
- <EventData>
<Data>Windows Defender</Data>
<Data>SECURITY_PRODUCT_STATE_ON</Data>
</EventData>
</Event>
ReadEventLog returns PyEventLogRecords (wrapper over [MS.Docs]: _EVENTLOGRECORD structure), while EvtRender expects (you need to work with) PyHANDLEs (PyEVT_HANDLEs (wrapper over EVT_HANDLE ([MS.Docs]: Windows Event Log Data Types) to be more precise)).
So, for getting XML data, you need to use the functions family that works with this type: e.g. EvtQuery, EvtNext.
code.py:
Notes:
try
/except
clauses (I didn't run into errors, so I'm not sure what are the situations where exception could be raised)pywintypes.datetime(2017, 3, 11, 3, 46, 47)
)win32evtlog.ReadEventLog
. Check [SourceForge.hg]: mhammond/pywin32 - Add buffer size parameter for ReadEventLog (patch #143 from cristi fati) for more details. By default, there was a limitation so that the buffer size was hardcoded to 1K. Since every ReadEventLog was accessing the disk, with the new buffer size I got a 10X speed improvement (for ~180K events)@EDIT0: I couldn't find a way to get all the required info with the Evt* functions family, so I'm getting it from both sources (I enhanced the script that I've previously posted):
@EDIT1: According to [MS.Docs]: OpenEventLogW function:
[MS.Docs]: Eventlog Key lists the 3 standard ones. So, that's why it opens the Application log. I've done some small changes to the script to test the sources. I don't know where mmc gets the Setup events from.