【ツールの概要】
・イベントログを項目指定して抽出CSVに書き出すスクリプトその②
・内容をもう少し精査しました。
【環境(使っている環境)】
・Windows server 2016(各文献には2008R2以降と書いてあるっぽい)
・Powershell ver2.0以降?(公式が見当たらないけどおそらく)
【ソース(get_eventlog_ver2.ps1)】
$MESSAGES = get-winevent ` -logname security ` -FilterXPath ` "Event [ ` System [ ` Provider [@Name='Microsoft-Windows-Security-Auditing'] ` and ( ` EventID='4624' ` or EventID='4625' ` or EventID='4634' ` or EventID='4648' ` ) ` ] ` ]" $MESSAGES | ForEach { if ( $_.id -eq "4624" ) { $_ | Select-Object timecreated, Id, ` @{Name = "TargetLogonId"; Expression = {$_.properties[5].value}}, ` @{Name = "TargetDomainName"; Expression = {$_.properties[6].value}}, ` @{Name = "TargetUserSid"; Expression = {$_.properties[4].value}}, ` @{Name = "IpAddress"; Expression = {$_.properties[18].value}}, ` @{Name = "Message"; Expression = {$_.Message.Substring(0,18)}} } elseif ( $_.id -eq "4625" ) { $_ | Select-Object timecreated, Id, ` @{Name = "TargetLogonId"; Expression = {$_.properties[5].value}}, ` @{Name = "TargetDomainName"; Expression = {$_.properties[6].value}}, ` @{Name = "TargetUserSid"; Expression = {$_.properties[4].value}}, ` @{Name = "IpAddress"; Expression = {$_.properties[19].value}}, ` @{Name = "Message"; Expression = {$_.Message.Substring(0,18)}} } elseif ( $_.id -eq "4634" ) { $_ | Select-Object timecreated, Id, ` @{Name = "TargetLogonId"; Expression = {$_.properties[1].value}}, ` @{Name = "TargetDomainName"; Expression = {$_.properties[2].value}}, ` @{Name = "TargetUserSid"; Expression = {$_.properties[0].value}} , ` @{Name = "IpAddress"; Expression = "-" }, ` @{Name = "Message"; Expression = {$_.Message.Substring(0,15)}} } elseif ( $_.id -eq "4648" ) { $_ | Select-Object timecreated, Id, ` @{Name = "TargetLogonId"; Expression = {$_.properties[5].value}}, ` @{Name = "TargetDomainName"; Expression = {$_.properties[6].value}}, ` @{Name = "TargetUserSid"; Expression = {$_.properties[4].value}} , ` @{Name = "IpAddress"; Expression = "-" }, ` @{Name = "Message"; Expression = {$_.Message.Substring(0,26)}} } } | Export-Csv -Encoding UTF8 -path c:\syslog.csv
【使用方法】
・ソースをメモ帳なりで記述し拡張子を「.ps1」にして保存します。
・管理者権限でpowershellにて実行します。(イベントログSecurityカテゴリには管理者権限要)
【説明】
パート1と比べて・・
・取得するイベントIDを増やしました。(明示的な云々~(イベントID4648)を追加)
・出力内容を少し便利にしました。(domainname、SID、IP、メッセージ内容を追加)
あと、イベントIDごとに、取得するメッセージの長さを地味に調整しています。
【総評】
FilterXPathのところで期間をとれるようにすれば、タスクで実行できそうですね。
まあ頻度を細かくするならイベント設定しろよ的な話になるので、あくまで統計用ですかね。
【参考リンク】
パート①を参照してください。
【PowerShell】(サーバ保守)イベントログのログオン履歴抽出、CSV化(Get-WinEvent) - うまく動けばいいな!