Get-EventLog 添加过滤器(服务器端),只返回最新的 N 条记录
Add Filter to Get-EventLog (server side), returning only newest N records
有没有什么方法可以在检索之前使用 PowerShell 过滤事件日志条目?
即
而不是:
[string[]]$IgnoredSources = 'SomeValue','SomeOtherValue'
Get-Eventlog -LogName $MyLog -ComputerName $MyComputer `
| ?{$IgnoredSources -notcontains $_.Source} `
| Sort-Object TimeGenerated -Descending `
| Select-Object -First 10
类似于:
Get-Eventlog -LogName $MyLog -ComputerName $MyComputer `
-Filter {(Source -ne 'SomeValue') -and (Source -ne 'SomeOtherValue')} `
-Newest 10
更多信息
我知道我可以添加一个 where-object
语句来过滤拉回的结果;但这比在服务器端过滤效率低,并且意味着像 -Newest 100
这样的命令在过滤后不一定会 return 100 个结果(即我必须拉回整个事件日志以确保我会得到最新的
我还知道对于日期,这可以通过 -After
和 -Before
属性实现,并且可以提供 -Username
和 -Source
限制为那些。但是,如果我想排除一个来源,或者过滤一系列事件ID,目前似乎没有办法。
我研究过使用 Get-WmiObject
而不是 Get-EventLog
,但是虽然这允许在服务器端进行过滤,但我无法确定限制结果数量的方法 returned(即在排序之前 returned 到我的机器,然后使用 select-object
的 -first
过滤结果)。
Get-WmiObject Win32_NTLogEvent -ComputerName $MyComputer `
-filter "(logfile='$MyLog') and (sourcename != 'SomeValue') and (sourcename != 'SomeOtherValue') " `
| Sort-Object TimeGenerated -Descending `
| Select-Object -First 10
Get-WinEvent怎么样?像这样:
Get-WinEvent -ComputerName $MyComputer -MaxEvents 100 -FilterHashtable @{
LogName=$MyLog;
ID=$MyID;
<# etc. #>
}
有没有什么方法可以在检索之前使用 PowerShell 过滤事件日志条目?
即
而不是:
[string[]]$IgnoredSources = 'SomeValue','SomeOtherValue'
Get-Eventlog -LogName $MyLog -ComputerName $MyComputer `
| ?{$IgnoredSources -notcontains $_.Source} `
| Sort-Object TimeGenerated -Descending `
| Select-Object -First 10
类似于:
Get-Eventlog -LogName $MyLog -ComputerName $MyComputer `
-Filter {(Source -ne 'SomeValue') -and (Source -ne 'SomeOtherValue')} `
-Newest 10
更多信息
我知道我可以添加一个 where-object
语句来过滤拉回的结果;但这比在服务器端过滤效率低,并且意味着像 -Newest 100
这样的命令在过滤后不一定会 return 100 个结果(即我必须拉回整个事件日志以确保我会得到最新的
我还知道对于日期,这可以通过 -After
和 -Before
属性实现,并且可以提供 -Username
和 -Source
限制为那些。但是,如果我想排除一个来源,或者过滤一系列事件ID,目前似乎没有办法。
我研究过使用 Get-WmiObject
而不是 Get-EventLog
,但是虽然这允许在服务器端进行过滤,但我无法确定限制结果数量的方法 returned(即在排序之前 returned 到我的机器,然后使用 select-object
的 -first
过滤结果)。
Get-WmiObject Win32_NTLogEvent -ComputerName $MyComputer `
-filter "(logfile='$MyLog') and (sourcename != 'SomeValue') and (sourcename != 'SomeOtherValue') " `
| Sort-Object TimeGenerated -Descending `
| Select-Object -First 10
Get-WinEvent怎么样?像这样:
Get-WinEvent -ComputerName $MyComputer -MaxEvents 100 -FilterHashtable @{
LogName=$MyLog;
ID=$MyID;
<# etc. #>
}