使用 powershell 提取 logon/logoff 个事件
Extracting logon/logoff events using powershell
我需要从 Windows 7 工作站中提取本地 logons/logoffs 的列表。我有一份 evtx 格式的安全事件日志的保存副本,但我遇到了一些问题。
以下 powershell 提取 ID 为 4624
或 4634
的所有事件:
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
为空白。
两个问题是:
- 为什么 ID 为 4634 的事件在传送到
{$_.properties[8].value -eq 2}
的位置时会丢失?
- 为什么
userid
是空的?我怎样才能得到 userid
?
注意 8 不是一个神奇的数字。它是 4624 事件定义的 XML 中的第 9 个 属性(索引从 0 开始)。如果您打开详细信息选项卡并切换到 XML 视图,您可以在事件查看器中看到它。查看 4634 事件时,您可以看到登录类型 属性 现在是第 5 种 - 因此您可能需要将查询修改为:
其中 {{$.Id -eq 4624 -和 $.properties[8] -eq 2} - 或 {$.Id -eq 4634 - 和 $.properties[4] -eq 2}}
userid 只是没有为这些事件定义。您可能想查看 XML(第 6 个 属性)中定义的 TargetUserName 属性。
好的,这就是我最终要做的。它打印出逗号分隔值:
$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)
}
我需要从 Windows 7 工作站中提取本地 logons/logoffs 的列表。我有一份 evtx 格式的安全事件日志的保存副本,但我遇到了一些问题。
以下 powershell 提取 ID 为 4624
或 4634
的所有事件:
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
为空白。
两个问题是:
- 为什么 ID 为 4634 的事件在传送到
{$_.properties[8].value -eq 2}
的位置时会丢失? - 为什么
userid
是空的?我怎样才能得到userid
?
注意 8 不是一个神奇的数字。它是 4624 事件定义的 XML 中的第 9 个 属性(索引从 0 开始)。如果您打开详细信息选项卡并切换到 XML 视图,您可以在事件查看器中看到它。查看 4634 事件时,您可以看到登录类型 属性 现在是第 5 种 - 因此您可能需要将查询修改为:
其中 {{$.Id -eq 4624 -和 $.properties[8] -eq 2} - 或 {$.Id -eq 4634 - 和 $.properties[4] -eq 2}}
userid 只是没有为这些事件定义。您可能想查看 XML(第 6 个 属性)中定义的 TargetUserName 属性。
好的,这就是我最终要做的。它打印出逗号分隔值:
$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)
}