【问题标题】:Powershell: Can't get Output for specific Event IDS using Get-EventLogPowershell:无法使用 Get-EventLog 获取特定事件 IDS 的输出
【发布时间】:2019-02-06 14:29:53
【问题描述】:

我是 Powershell 的新手。我正在尝试获取有关帐户管理审计的几个事件 IDS 的信息。我知道我写的脚本效率不够,但我认为这不是问题所在。出于某种原因,即使我生成了一些事件并且它们显示在 EventViewer 中,我也没有得到事件 ID 4781 的输出。对于 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.?)"***
    }

现在,关于如何使用上面显示的 Write-Output 获取信息,例如谁做了更改以及哪个用户,有什么帮助吗?

【问题讨论】:

    标签: powershell audit get-eventlog


    【解决方案1】:

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

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

    【讨论】:

    • 您好,感谢您的回复。当我获得其他事件 ID 的结果时,输出文件似乎工作正常。我运行了您建议的命令,但没有得到任何结果。
    • 但我正在运行它,似乎有这样的事件:Get-EventLog -LogName Security -InstanceId 4781 --> ...95966 Aug 31 14:58 SuccessA... Microsoft-Windows。 .. 4781 帐户名称已更改:...
    【解决方案2】:

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

    因此,最终得到了下面的草稿代码,我将针对所有所需的 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"
        }
    

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 2017-07-28
      • 2017-03-12
      • 2013-05-23
      • 2020-03-25
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多