Query windows event log with python

alex23 wuwei23 at gmail.com
Sat Jan 12 09:34:19 CET 2013

On 12 Jan, 16:09, robey.lawre... at gmail.com wrote:
> Hi,
> I am looking to write a short program to query the windows event log.
> It needs to ask the user for input for The event type (Critical, Error, and Information), and the user needs to be able to specify a date since when they want to view results.
> I understand I will need the pywin32 extension, which i already have installed.
> I found this piece of code to start from,
> <code>
> import win32evtlog # requires pywin32 pre-installed
> server = 'localhost' # name of the target computer to get event logs
> logtype = 'System' # 'Application' # 'Security'
> hand = win32evtlog.OpenEventLog(server,logtype)
> total = win32evtlog.GetNumberOfEventLogRecords(hand)
> while True:
>     events = win32evtlog.ReadEventLog(hand, flags,0)
>     if events:
>         for event in events:
>             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
>             print
> </code>
> Thanks for any help.
> Robey

What would you like us to provide? Pointers to the Python tutorial? Or
all of the code?

Generally, the onus is on you to attempt to come up with solution
yourself and then to ask for assistance where required. If you want
someone to just write it for you, then you might want to mention how
you plan on recompensing them.

More information about the Python-list mailing list