【发布时间】:2019-03-04 13:16:03
【问题描述】:
我想做什么?
我使用-FilterHashTable 运行Get-WinEvent 函数,为ID 参数提供一系列有趣的事件ID。
$IDS = 4720,4722,4723,4724,4725,4726,4727,4728,4729,4730,4731,4732,4733,4734,4735,4737,4738,4740,4741,4742,4743,4744,4745,4746,4747,4748,4749,4750,4751,4752,4753,4754,4755,4756,4757,4758,4759,4760,4761,4762,4763,4764,4767,4781
Get-WinEvent -ComputerName DC -FilterHashTable @{ LogName='Security'; ID=$IDS; }
这个返回错误:
# Get-WinEvent : No events were found that match the specified selection criteria.
(我知道匹配的事件确实存在)
我注意到,对于较小的数组,该函数返回了积极的结果,因此经过几次尝试,我已经断言:
- 数组计数
-le 23的直接调用工作正常; - 使用数组计数
-gt 23直接调用会导致错误。
看似正确的解决方案...
我假设 23 是 Get-WinEvent 的底层机制可以处理的未记录的参数限制,然后决定将调用拆分为具有较小数组的多个调用:
$MaxCount = 23
For ( $i = 0; $i -lt $IDS.count; $i += $MaxCount ) {
$IDSChunks += ,@( $IDS[ $i..($i+$MaxCount-1) ] )
}
这样我们就将数组一分为二,每个都包含-le 23 个元素:
$IDSChunks | %{ $_ -join "," }
4720,4722,4723,4724,4725,4726,4727,4728,4729,4730,4731,4732,4733,4734,4735,4737,4738,4740,4741,4742,4743,4744,4745
4746,4747,4748,4749,4750,4751,4752,4753,4754,4755,4756,4757,4758,4759,4760,4761,4762,4763,4764,4767,4781
手动检查,结果按预期工作:
Get-WinEvent -ComputerName DC -FilterHashTable @{ LogName='Security'; ID=$IDSChunks[0]; }
Get-WinEvent -ComputerName DC -FilterHashTable @{ LogName='Security'; ID=$IDSChunks[1]; }
但是……
然而,这不是:
$IDSChunks | %{ Get-WinEvent -ComputerName DC -FilterHashTable @{ LogName='Security'; ID=$_; } }
结果是已经熟悉的错误:
# Get-WinEvent : No events were found that match the specified selection criteria.
# Get-WinEvent : No events were found that match the specified selection criteria.
为什么?
我做错了什么?
【问题讨论】:
标签: arrays windows powershell get-winevent