我想做的事?
我运行该Get-WinEvent
函数时-FilterHashTable
为参数提供了一系列有趣的事件 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.
为什么?
我究竟做错了什么?