pythonget-eventlog

getting eventlogs from Applications and Services log using python


I am trying to read event logs from Applications and Services log using python. However the output are not as expected. (Actual 10 vs output 838) I am using the following code. Was wondering if there is a mistake with the parameters.

import win32evtlog
server = 'localhost'
logtype = "Microsoft-Windows-Storage-Storport/Operational"
hand = win32evtlog.OpenEventLog(server, logtype)
flags = win32evtlog.EVENTLOG_FORWARDS_READ | win32evtlog.EVENTLOG_SEQUENTIAL_READ
while True:
events = win32evtlog.ReadEventLog(hand, flags,0)
if events:
    for event in events:
       print ('Source Name:', event.SourceName)
       print ('Event ID:', event.EventID)
       print ('Time Generated:', event.TimeGenerated)

Solution

  • Found a method to get the information through the use of powershell using python.

    import subprocess
    getinfo = subprocess.check_output(
            ['powershell.exe', 'get-Winevent Microsoft-Windows-xxx/Operational'])
    

    where xxx is a variable