4
6

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 5 years have passed since last update.

出勤時刻、退勤時刻の一覧を取得する方法

Last updated at Posted at 2019-09-06

月末に勤務表を書く時、出勤時刻や退勤時刻が知りたくなりますよね。
そんな貴方のために、以下のコマンドをPowerShellで実行することをお勧めします。

Get-EventLog System -After "2019/08/01"| ? {$_.InstanceId -eq "7002" -or $_.InstanceId -eq "7001"} | select InstanceID,TimeGenerated | sort -Property TimeGenerated | Out-String | % { $_ -replace "7001","ログオン" } | % { $_ -replace "7002","ログオフ" }

解説

Windowsのイベントの一覧を取得しよう

このコマンドですがWindowsのイベントログのうち、2019/8/1以降のものを取得しています。

Get-EventLog System -After "2019/08/01"

でも、この一覧じゃあ、関係ないログまでいっぱい出力されてよくわからないですよね。

知りたいのはログオンとログオフだけ

次に?(Where-Object)を使用して、インスタンスIDが7001(ログオン)、7002(ログオフ)のものを取得しましょう。

Get-EventLog System -After "2019/08/01" | ? {$_.InstanceId -eq "7002" -or $_.InstanceId -eq "7001"} 

これでログオンとログオフだけになりました。
でもこれ、IndexやらEntryTypeやら、いらない項目があって見にくいし、日時の降順なのもいただけませんよね。

欲しいのは何のイベントなのかと日時だけ

select(Select-Object)を使用して、余計な項目を省きインスタンスIDと日時のみを取得しましょう。

Get-EventLog System -After "2019/08/01"| ? {$_.InstanceId -eq "7002" -or $_.InstanceId -eq "7001"} | select InstanceID,TimeGenerated

うん、大分見やすくなりましたね。

次は日時の昇順にソートしよう

sort(Sort-Object)を使用して、並べ替えてます。

Get-EventLog System -After "2019/08/01"| ? {$_.InstanceId -eq "7002" -or $_.InstanceId -eq "7001"} | select InstanceID,TimeGenerated | sort -Property TimeGenerated

もうこれでもいい気がしますが、7001や7002はちょっと不親切かな。

7001や7002をログオン、ログオフに置換しよう

最後に、Out-Stringで文字列に変換した後、%(ForEach-Object)を使用して、7001、7002の部分をログオン、ログオフに置換

Get-EventLog System -After "2019/08/16"| ? {$_.InstanceId -eq "7002" -or $_.InstanceId -eq "7001"} | select InstanceID,TimeGenerated | sort -Property TimeGenerated | Out-String | % { $_ -replace "7001","ログオン" } | % { $_ -replace "7002","ログオフ" }

これで完璧。

後書き

え、Windows使ってない?
PCつけっぱなしで帰ってる?
すみません。自分で何とかしてください。

4
6
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
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?