PCの起動停止時刻
PCの起動停止時刻をイベントログから取得してくれるフリーソフトを使っていたのですが、月が変わっちゃうと、以前のログは取得してくれない仕様になっています。メンドくさいので、pythonでスクリプトを書きました。
startstop.py
wevtutil.exeを実行して、起動/停止イベント(ID=12,13)を取得します。
# -*- coding: sjis -*- import sys import re import popen2 import time p_stop = re.compile( "<Data Name='StopTime'>.*Z</Data>") p_start = re.compile("<Data Name='StartTime'>.*Z</Data>") def exec_wevtutil(id, count): """wevtutil.exeを実行してイベントログ(の標準出力ハンドル)を取得""" cmd = "wevtutil qe System /q:*[System/EventID=" + str(id) + "] /rd:true /c:" + str(count) stdout, stdin, stderr = popen2.popen3(cmd) return stdout def localtime_str(s): """UTCの時刻文字列をローカル時間に変換する""" utc = time.strptime(s, '%Y-%m-%dT%H:%M:%S') local_time = time.localtime(time.mktime(utc)-time.timezone) return time.strftime("%Y/%m/%d %H:%M:%S", local_time) def get_start_time(event): """1イベントから起動時刻を取得する""" return localtime_str(p_start.search(event).group()[23:42]) def get_stop_time(event): """1イベントから停止時刻を取得する""" return localtime_str(p_stop.search(event).group()[22:41]) def print_time(count): """起動/停止時刻を印字する""" rslt = [] #起動イベント(ID=12)を取得 stdout = exec_wevtutil(12, count) for i in stdout: rslt.append(get_start_time(i) + ", start") stdout = exec_wevtutil(13, count) #停止イベント(ID=13)を取得 for i in stdout: rslt.append(get_stop_time(i) + ", stop") #時刻の昇順にソートして印字 rslt.sort() for i in rslt: print i def main(): count = 16 #デフォはイベント16組を表示 if len(sys.argv) > 1: count = int(sys.argv[1]) print_time(count) main()
バッチファイル
startstop.pyを実行して、結果をエディタで開きます。
set EDITOR=C:\Program Files (x86)\Hidemaru\Hidemaru.exe startstop.py > tmp.txt start /B "%EDITOR%" tmp.txt