Powershell:无法使用Get-EventLog获取特定事件IDS的输出

时间:2018-08-31 14:20:40

标签: powershell audit get-eventlog

我是Powershell的新手。我正在尝试获取有关帐户管理审核的多个事件IDS的信息。我知道我编写的脚本不够高效,但是我认为这不是问题所在。出于某种原因,即使我生成了一些事件并将其显示在EventViewer中,我也没有获得事件ID 4781的输出。对于事件ID(例如4720、4726、4722等),我可以使用相同的脚本将它们正常记录在输出文件中。有人知道为什么吗?

目前我正在 输出:操作:用户创建 时间:31-08-2018 2:55 谁:管理员 用户:test2

$events = Get-Eventlog -LogName Security -ComputerName $DC.Hostname -after $startDate | where {$e.EventID -eq 4781 -or $e.EventID -eq 4720}
$ActivityOutput=foreach ($e in $events) {
if (($e.EventID -eq 4720)){
Write-Output "Action:User Created","Time:$($e.TimeGenerated.ToString("dd-MM-yyyy h:mm"))","Who:$($e.ReplacementStrings[4])","User:$($e.ReplacementStrings[0])"
Write-Output "===============================================`n"
} 
if (($e.EventID -eq 4781)){
Write-Output "The name of an Object changed", "Time:$($e.TimeGenerated.ToString("dd-MM-yyyy  h:mm"))", "Who:$($e.ReplacementStrings[5])","Old Value:$($e.ReplacementStrings[0])","New Value:$($e.ReplacementStrings[1])"
Write-Output "===============================================`n"
}
} Out-File -Append -FilePath C:\UserTracking.txt  -InputObject $ActivityOutput

========= 更新04/09/2018 因此,似乎Get-EventLog仅对某些EventID起作用,这就是为什么我缺少其中一些诸如4781的原因。我转换为Get-WinEvent后,似乎该事件获取了所有所需的EventID。 编辑代码:

$events=Get-WinEvent -FilterHashtable @{Logname="Security"; StartTime=(get-date).AddDays(-6); ID=4781,4738,4725,4728,4729,4720,4726,4722,4740}
}
  $ActivityOutput=foreach ($e in $events) {
   # user account was created
    if (($e.Id -eq 4720)){
      Write-Output "Action:User Created","Time:$($e.TimeCreated.ToString("dd-MM-yyyy h:mm"))",***"Who:$($e.?)","User:$($e.?)"***
    }

现在,使用上面显示的Write-Output获取有关如何获取信息(如谁进行了更改以及针对哪个用户)的帮助吗?

2 个答案:

答案 0 :(得分:0)

您似乎在Out-File之前缺少回报。不确定粘贴中是否有错字。

要验证您是否确实有任何匹配项,要做的一件事就是只运行$events | ?{$_.EventID -eq 4781}。您将所有结果打印到屏幕上。如果您发现没有任何记录,则可能是您没有任何带有EventID 4781的日志。

答案 1 :(得分:0)

通常,我不应该使用“ Get-EventLog”,而应该使用“ Get-WinEvent”。可以使用$ _。Properties [...]

获取每个eventID的值。

所以,最后得到下面的草稿代码,我将为所有所需的EventID重复该代码,因为我需要为每个ID设置不同的值

$EventID=4781,4738,4725,4728,4729,4720,4726,4722,4740
$events=Get-WinEvent -FilterHashtable @{Logname="Security"; StartTime=(get-date).AddDays(-6); ID=EventID}
}
  $ActivityOutput=foreach ($e in $events) {
    if (($e.Id -eq 4720)){
      Write-Output "Action:User Created","Time:$($e.TimeCreated.ToString("dd-MM-yyyy h:mm"))","Who:"$e.Properties[4],"User:"$e.Properties[0]
      Write-Output "===============================================`n"
    }
相关问题