1
1

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

起動時に、最近のPC使用記録を表示

Last updated at Posted at 2020-06-07

テレワークなどが広がっているが、自分が出社しない日に誰かが自分のPCにログインしたかを確認するスクリプトです。
起動時に自動スタートアップに設定すれば便利です。

checkDirty.ps1
# $ErrorActionPreference = 'Continue';
Param($Arg1);
$td = [DateTime]::Today;
if ($Arg1 -ieq 'ALL') {
    $sk = ([DateTime]::MinValue - $td).Days + 1;
} else {
    switch ($td.DayOfWeek.value__) {
        0 {$sk = -2; break;}
        1 {$sk = -3; break;}
        default {$sk = -1; break;}
    }
}
Get-EventLog System -InstanceId 7001,7002 -After ($td.AddDays($sk)) |
#Select-Object -Property TimeGenerated, EventId, MachineName, ReplacementStrings |
%{
    $ts = $_.TimeGenerated.toString('yyyy/MM/dd(z)HH:mm:ss');
    $outputEid = $_.EventId.toString();
    if (7001 -eq $outputEid) {
        $outputEid = '└' + $outputEid + '(ログオン)';
    } elseif (7002 -eq $outputEid) {
        $outputEid = '┌' + $outputEid + '(ログオフ)';
    } else {
        $outputEid = ' ' + $outputEid;
    }
    $targetHost = $_.MachineName.toString();
    $outputTerm = $_.ReplacementStrings[0];
    $outputUser = $_.ReplacementStrings[1];
    try {
        $outputUser = (New-Object System.Security.Principal.SecurityIdentifier($outputUser)).Translate([System.Security.Principal.NTAccount]).Value;
    } catch {}
    [PSCustomObject]@{タイムスタンプ = $ts; イベントID = $outputEid; 対象ホスト = $targetHost; 対象セッション = $outputTerm; 対象ユーザー = $outputUser};
} |
Out-GridView -Title "PC利用履歴" -Wait;
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?