使用 Powershell 过滤从远程日志输出的日期

Filter For Dates Outputted From Remote Logs Using Powershell

请查看下面我当前的代码,代码将继续保持 运行ning 直到被用户停止。最终目标是让脚本 运行 找到某一天并成功停止或找到 2 个日期之间的结果。

我试过使用 -before -after 标志和 (get-date) 的组合。 IE .date .datetime .addday(+/-1) 但在使用它们时它会找到正确的日期并停止但需要 2/3 分钟才能再次启动脚本或移动到列表中的下一台机器。我也试过 { $_.TimeGenerated -lt "00/00/000" }

知道如何做到这一点吗?我尝试了一些其他的东西,但没有快乐。

$ComputerName = 'CSV File'
$UserProperty = @{n="User";e={(New-Object System.Security.Principal.SecurityIdentifier $_.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])}}
$TypeProperty = @{n="Action";e={if($_.EventID -eq 7001) {"Logon"} else {"Logoff"}}}
$TimeProperty = @{n="Time";e={$_.TimeGenerated}}
$MachineNameProperty = @{n="MachineName";e={$_.MachineName}}

foreach ($computer in $ComputerName) {
Get-EventLog System -Source Microsoft-Windows-Winlogon -ComputerName $computer | select $UserProperty,$TypeProperty,$TimeProperty ,$MachineNameProperty 
}

我不确定你遇到了什么问题,但这对我来说很好:

Get-EventLog System -Source Microsoft-Windows-Winlogon -ComputerName $computer -before '2017-11-09' -after '2017-10-27' -InstanceId @(7001, 7002) | select $UserProperty,$TypeProperty,$TimeProperty ,$MachineNameProperty

正如我在评论中所述,获取远程事件日志通常是一个非常缓慢的过程。

这里有一些时间信息:

首先,我 运行 对服务器的请求完全没有时间过滤器:

measure-Command -Expression {$noTimeFilter = Get-EventLog System -Source Microsoft-Windows-Winlogon -ComputerName $computer -InstanceId @(7001, 7002)}

这花费了 292.85 秒,return编辑了 79 个事件。接下来我 运行 它针对过去 24 小时内的同一服务器过滤:

measure-Command -Expression {$withTimeFilter = Get-EventLog System -Source Microsoft-Windows-Winlogon -ComputerName $computer -after (Get-Date).AddDays(-1) -InstanceId @(7001, 7002)}

这花了 243 秒,return编辑了 2 个事件。

所以对于这个公认的有限测试过滤实际上更快。在我的 g运行ted 轶事经验中,过滤并没有真正产生太大的影响。我很确定查询会获取所有事件然后过滤它们。如果您实际上一直看到没有过滤器的更快 return,您当然可以每次都获取所有内容,然后将其通过管道传输到 Where-Object 块以获取您想要的事件。这与我的经验相反,正如 FoxDeploy 指出的那样可能对远程机器的性能有害。