[Tools]获取系统开关机信息(附源码及程序)

来源:互联网 发布:淘宝店邮费一般多少 编辑:程序博客网 时间:2024/06/18 04:05

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


EventPowerStat.bat

@echo offcd %~dp0wevtutil qe system /format:text /q:"Event[System[(EventID=12 or EventID=13)]]" > EvtPower.datEventPowerStat.exe EvtPower.datdel /F EvtPower.dat

EventPowerStat.py

# -- coding:utf-8 --# Python v2.7.10# EventPowerStat.py# Written by Gaearrowimport sys# EventID to Task Dictionaryeventiddic = {    '12':'PowerOn',    '13':'PowerOff',}# Process Inputif 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; Computer0;PowerOff;2009-07-13T22:12:40.878;37L4247D25-071;PowerOn;2014-06-24T13:29:12.624;37L4247D25-072;PowerOff;2014-06-24T13:35:34.107;WIN-T6I0355NJEA3;PowerOn;2014-06-24T13:36:04.702;WIN-T6I0355NJEA4;PowerOff;2014-06-24T07:19:43.162;WIN-T6I0355NJEA5;PowerOn;2014-06-24T07:19:59.718;WIN-T6I0355NJEA6;PowerOff;2014-06-24T07:25:54.137;WIN-T6I0355NJEA...==============================Summary InformationPower On  Event :  12Power Off Event :  12Total     Event :  27==============================
0 0