具有匹配 TimeCreated 的 Get-WinEvent

Get-WinEvent with match TimeCreated

我想知道如何检查整个 Windows 日志中是否有与特定日期匹配的事件,例如: 目前停止在“系统”日志上:(

Get-WinEvent System | where {$_.TimeCreated -eq "24.03.2021 20:50:37"}

但结果什么也没告诉我。 我想使用脚本来搜索所有事件,而不仅仅是 System. 我需要将日期与事件相关联。 我的脚本如下所示:

(Get-WinEvent –ListLog * -ErrorAction SilentlyContinue).LogName | ForEach-Object {Get-WinEvent | where $_.TimeCreated -eq "24.03.2021 20:50:37"}}

但出现语法错误

TimeCreated 是 DateTime 类型 属性,因此兼容的字符串效果更好。 (与过滤器哈希表的开始时间不同吗?)

Get-WinEvent system | select timecreated | select -first 1 | % timecreated | 
  % gettype

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DateTime                                 System.ValueType


[datetime]'03/24/2021 20:50:37'

Wednesday, March 24, 2021 8:50:37 PM

# timecreated must have 0 milliseconds
get-winevent system | where timecreated -eq '03/24/2021 20:50:37'

毫秒问题的一个思路:

get-winevent system | where { $_.timecreated.tostring() -eq '3/24/2021 8:50:37 PM' }

我很惊讶这样的事情不起作用。

get-winevent system | where '3/24/2021 8:50:37 PM' -eq timecreated


'3/24/2021 8:50:37 PM' -eq [datetime]'3/24/2021 8:50:37 PM'

False

明白了。 Tostring() 不会以这种方式格式化时间。

[string][datetime]'3/24/2021 8:50:37 PM'

03/24/2021 20:50:37


'03/24/2021 20:50:37' -eq [datetime]'3/24/2021 8:50:37 PM'

True

所以 -eq 的左边参数决定了一个字符串比较:

get-winevent system | where '03/24/2021 20:50:37' -eq timecreated

我认为问题在于您所追求的准确性。 (日期包含小时、分钟和秒,但 没有 毫秒)

当您使用

创建比较 DateTime 对象时
$time = [datetime]'03/24/2021 20:50:37'

你会发现它的 .MilliSecond 属性 设置为 0.

当您将此与事件 TimeCreated 属性 进行比较时,那个日期的可能性 非常 实际上有一个 MiliSecond 的确切时间也值 0..

这就是为什么您需要从事件的 TimeCreated 属性 中去除毫秒(以及这些毫秒的小数部分)以便能够与确切日期进行比较,但没有毫秒:

$time = [datetime]'03/24/2021 20:50:37'
(Get-WinEvent -LogName System) | 
    Where-Object { ($_.TimeCreated.AddTicks(-$_.TimeCreated.Ticks % [timespan]::TicksPerSecond)) -eq $time } 

如果你愿意,你当然可以把它放在一个循环中以扫描不同的日志名称


根据您的评论,Get-WinEvent cmdlet returns 对象具有很多属性。 PowerShell 的标准方式是在屏幕上输出这些属性的 子集 ,在本例中为 TimeCreated, Id, LevelDisplayName and Message.

如果您还想要此输出中的事件日志名称,请将 Select-Object 添加到命令中,例如:

$time = [datetime]'04/19/2021 08:38:20' 
(Get-WinEvent –ListLog * -ErrorAction SilentlyContinue).LogName | ForEach-Object {
    Get-WinEvent -LogName $_ | 
        Where-Object { ($_.TimeCreated.AddTicks(-$_.TimeCreated.Ticks % [timespan]::TicksPerSecond)) -eq $time} |
        # output the properties you are interested in
        Select-Object LogName, TimeCreated, Id, LevelDisplayName, Message
}

为了使其更加灵活,捕获 变量中的结果,这样您既可以在屏幕上显示,也可以将结果保存到 Csv 文件以供以后检查:

$time   = [datetime]'04/19/2021 08:38:20' 
$result = (Get-WinEvent –ListLog * -ErrorAction SilentlyContinue).LogName | ForEach-Object {
    Get-WinEvent -LogName $_ | 
        Where-Object { ($_.TimeCreated.AddTicks(-$_.TimeCreated.Ticks % [timespan]::TicksPerSecond)) -eq $time} |
        # output the properties you are interested in
        Select-Object LogName, TimeCreated, Id, LevelDisplayName, Message
}

# output on screen
$result

# save to Csv File
$result | Export-Csv -Path 'Path\To\The\Output.csv' -NoTypeInformation -UseCulture

要查看返回的所有属性的名称,您可以这样做
Get-WinEvent -LogName System | Select-Object -First 1 | fl *