PowerShell:Get-Winevent 不适用于 foreach 循环中的多个对象

PowerShell: Get-Winevent doesn't work with multiple objects in foreach loop

我启用了审核事件组策略,然后将我的测试帐户添加到 Groupname11。

当我尝试 运行 这没有其他组名被注释掉时,我没有从 $Events.

得到任何东西

我不明白我做错了什么?

$Groups = @(
    #"Groupname8"
    #"Groupname9"
    #"AGroupname10"
    "Groupname11"
    #"Groupname12"
    )
    
    Foreach ($Group in $Groups){
    $Events = Get-WinEvent -FilterHashtable @{logname = 'Security'; ID = 4728; } | Where-Object {$_.Properties.Value -like "*$($Groups)*"}
    }
    $Events

您目前在循环的每次迭代中覆盖 $Events

将分配移出循环,以便捕获 所有 组的事件 $Events:

$Groups = @(
    "Groupname8"
    "Groupname9"
    "AGroupname10"
    "Groupname11"
    "Groupname12"
)

$Events = Foreach ($Group in $Groups) {
    Get-WinEvent -FilterHashtable @{logname = 'Security'; ID = 4728; } | Where-Object { $_.Properties.Value -like "*$($Groups)*" }
}

$Events