使用PowerShell提取登录/注销事件

时间:2016-07-26 06:30:35

标签: powershell event-log get-winevent

我需要从Windows 7工作站中提取本地登录/注销列表。我已经以evtx格式保存了安全事件日志的副本,并且我遇到了一些问题。

以下powershell提取标识为46244634的所有事件:

Get-WinEvent -Path 'C:\path\to\securitylog.evtx' | where {$_.Id -eq 4624 -or $_.Id -eq 4634}

我想再过滤logon type = 2 (local logon)。管道这个:

 | where {$_.properties[8].value -eq 2}

但似乎放弃了所有id=4634(注销)事件。

即使是事件id = 4624事件,也没有用户ID存在。例如管道:

 | select-object -property Timecreated,TaskDisplayName,MachineName,userid

或以Export-Csv方式管道,userid为空。

有两个问题:

  1. 为什么在传送到{$_.properties[8].value -eq 2}
  2. 的地方时,ID为4634的事件会丢失
  3. 为什么userid为空?如何获得userid

2 个答案:

答案 0 :(得分:0)

  1. 请注意,8不是幻数。它是4624事件定义的XML中的第9个属性(从0开始的索引)。如果打开“详细信息”选项卡并切换到XML视图,则可以在事件查看器中看到它。查看4634事件时,您可以看到Logon Type属性现在是第5个 - 因此您可能希望将查询修改为:

    其中{{$ .Id -eq 4624 -and $ .properties [8] -eq 2} -or {$ .Id -eq 4634 -and $ .properties [4] -eq 2}}

  2. 仅为这些事件定义了userid。您可能希望查看XML中定义的TargetUserName属性(第6个属性)。

答案 1 :(得分:0)

好的,这就是我最终的目标。它打印出逗号分隔值:

$evts = Get-WinEvent -Path 'C:\path\to\securitylog.evtx' | where {($_.Id -eq 4624 -and $_.properties[8].value -eq 2) -or ($_.Id -eq 4634 -and $_.properties[4].value -eq 2) }
foreach ($e in $evts)
{
    # get the attributes
    $ds = $e.TimeCreated
    $tdn = $e.TaskDisplayName
    $mn = $e.MachineName

    # userid will vary depending on event type:
    if($e.Id -eq 4624) { $userid = $e.properties[5].value }
    if($e.Id -eq 4634) { $userid = $e.properties[1].value }

    write-host ("{0},{1},{2},{3}" -f [string]$ds,[string]$tdn,[string]$mn,[string]$userid)
}