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

问题描述 投票:1回答:2

我是Powershell的新人。我正在尝试获取有关帐户管理审核的多个事件IDS的信息。我知道我写的脚本效率不够,但我不认为这是问题所在。出于某种原因,我没有得到事件ID 4781的输出,即使我已经生成了一些事件并且它们在EventViewer中显示。对于像4720,4726,4722等事件ID,我可以使用相同的脚本在输出文件中正常记录它们。任何人都有任何线索的原因?

目前我收到输出:操作:用户创建时间: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.?)"***
    }

现在,有关如何获取信息的任何帮助,例如谁进行了更改以及使用上面的写入输出的用户?

powershell audit get-eventlog
2个回答
0
投票

你似乎错过了Out-File之前的回归。不确定这是否是粘贴中的拼写错误。

验证你是否真正获得任何匹配的一件事就是运行$events | ?{$_.EventID -eq 4781}。您将把所有结果打印到屏幕上。如果您发现没有任何可能是您没有使用EventID 4781的任何日志。


0
投票

一般情况下,我不应该使用“Get-EventLog”而是使用“Get-WinEvent”。每个eventID的值可以使用$ _。属性[...]获取。

因此,最后得到了下面的草案代码,我将重复所有期望的EventID,因为我需要为每个代码提供不同的值

$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"
    }
© www.soinside.com 2019 - 2024. All rights reserved.