Welcome, guest | Sign In | My Account | Store | Cart

Windows Event Log Viewer

Python, 25 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Windows Event Log Viewer
# FB - 201012116
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)
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:
            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

4 comments

Dejan 10 years, 12 months ago  # | flag

Python 2.5 on Win 7:

Traceback (most recent call last): File "windows_log.py", line 24, in <module> print msg UnicodeEncodeError: 'ascii' codec can't encode character u'\u200e' in position 0: ordinal not in range(128)

FB36 (author) 10 years, 11 months ago  # | flag

I using Python 2.6. I never got any error.

FB36 (author) 10 years, 11 months ago  # | flag

I tested this code only on Windows Vista and XP, not w/ 7 though.

Claveau Michel 10 years, 7 months ago  # | flag

Hi!

For solve the problem, change print msg to print msg.encode('cp1252','ignore')

(replace 'cp1252' by the encoding of your computer/nation/locale)