0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WindowsイベントからPCのON/OFF情報を取得したい

Posted at

WindowsイベントからPCのON/OFF情報を取得したい

結論

;; 起動時刻
wevtutil qe System /f:Text /rd:true /c:1 "/q:*[System[(EventID=7001 or EventID=6005)]]"

;; 停止時刻
wevtutil qe System /f:Text /rd:true /c:1 "/q:*[System[(EventID=7002 or EventID=6006)]]"

Windowsイベントについて

PCのOnOffに関するWindowsイベントは主に四種類あります。

ID イベント
6005 PC起動(完全シャットダウンからの復帰)
7001 PC起動
6006 完全シャットダウン
7002 シャットダウン

Windows10からは起動を早くするため、(意図的な操作をしない限り)完全シャットダウンをしません。そのため、Windows10以降でPCの起動/停止のログを取るためにはイベントID[7001][7002]を指定する必要があります。

イベントを取得するためのコマンド

Windowsイベントを取得するため、wevtutilコマンドを利用します。
取得するデータは「最新のPC起動時刻」と「最新のPC停止時刻」です。上記で説明したイベントIDを全て取得した上で、現在時刻から近いものを一件取得することとします。

PC起動時刻
wevtutil qe System /f:Text /rd:true /c:1 "/q:*[System[(EventID=7001 or EventID=6005)]]"
PC停止時刻
wevtutil qe System /f:Text /rd:true /c:1 "/q:*[System[(EventID=7002 or EventID=6006)]]"
オプション 概要
/f 出力形式をテキストに変更。XML形式も指定できる。
/rd データの読み込み方向を指定。「true」で新しい順に取得できる。
/c 取得する件数を指定。今回は一件だけ読めればよいので、「1」を指定。
/q 取得するデータのクエリを指定。今回はイベントIDを指定。

おわりに

オプションの「/q」がどうにも腑に落ちなかったので調べてみたら、Xpathクエリと呼ばれる書き方のようです。データの検索にSQLしか使ってこなかった私は、名前すら聞いたことなかったです。まだまだ学習しないといけない事は沢山ありそうですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?