博主写了一个小脚本/工具(Github下载地址包含全部源码及pyinstaller转的exe可执行程序),用来获取系统开关机信息,大家觉得不错就收下吧,欢迎交流提建议。


EventPowerStat.bat
@echo off
cd %~dp0
wevtutil qe system /format:text /q:"Event[System[(EventID=12 or EventID=13)]]" > EvtPower.dat
EventPowerStat.exe EvtPower.dat
del /F EvtPower.dat
EventPowerStat.py
# -- coding:utf-8 --
# Python v2.7.10
# EventPowerStat.py
# Written by Gaearrow

import sys

# EventID to Task Dictionary
eventiddic = {
    '12':'PowerOn',
    '13':'PowerOff',
}

# Process Input
if len(sys.argv) != 2:
   print 'Usage: '
   print 'wevtutil qe system /format:text /q:"Event[System[(EventID=12 or EventID=13)]]" > EvtPower.dat'
   print '%s EvtPower.dat' % sys.argv[0].split('\\')[-1]
   sys.exit(1)
evt = sys.argv[1]
fevt = open(evt,'r')
fpower = open('PowerStat.csv','w')
print >>fpower,'Event No.; Task; Date; Computer'

try:
    # Perform the Statistics
    numevent    = 0
    numpoweron  = 0
    numpoweroff = 0

    for eachline in fevt:
        if eachline.find('Event[') > -1:
            # Reset var
            evtno = ''
            date = ''
            task = ''
            computer = ''
            skip = 1 
            evtno = eachline.split('[')[1].split(']')[0]
            numevent = numevent + 1
        elif eachline.find('Date:') > -1:
            date = eachline[(eachline.find(':')+1):].strip()
        elif eachline.find('Event ID') > -1:
            evtid = eachline.split(':')[1].strip()
            task = eventiddic[evtid]
        elif eachline.find('Level: Information') > -1:
            skip = 0
        elif eachline.find('Computer:') > -1:
            computer = eachline.split(':')[1].strip()
            if skip == 0:
                print >>fpower,evtno+';'+task+';'+date+';'+computer
                if task == 'PowerOn':
                    numpoweron = numpoweron + 1
                else:
                    numpoweroff = numpoweroff + 1



    # Print Summary Infomation
    print >>fpower,'=============================='
    print >>fpower,'Summary Information'
    print >>fpower,'Power On  Event : ',numpoweron
    print >>fpower,'Power Off Event : ',numpoweroff
    print >>fpower,'Total     Event : ',numevent
    print >>fpower,'=============================='
    print 'Event Statistics Success to PowerStat.csv'

except Exception as e:
    print 'Error: %s' % e
    sys.exit(1)

fevt.close()
fpower.close()
PowerStat.csv
Event No.; Task; Date; Computer
0;PowerOff;2009-07-13T22:12:40.878;37L4247D25-07
1;PowerOn;2014-06-24T13:29:12.624;37L4247D25-07
2;PowerOff;2014-06-24T13:35:34.107;WIN-T6I0355NJEA
3;PowerOn;2014-06-24T13:36:04.702;WIN-T6I0355NJEA
4;PowerOff;2014-06-24T07:19:43.162;WIN-T6I0355NJEA
5;PowerOn;2014-06-24T07:19:59.718;WIN-T6I0355NJEA
6;PowerOff;2014-06-24T07:25:54.137;WIN-T6I0355NJEA
...
==============================
Summary Information
Power On  Event :  12
Power Off Event :  12
Total     Event :  27
==============================
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐