get-winevent:使用属性

get-winevent: working with properties

我尝试学习 powershell 来自动执行一些日常任务。

我尝试从 get-winevent 函数中找出所有字段名,以了解当我想从具有多种条件的大量 eventid 中过滤结果时我需要做什么。

在这个简单的示例中,我想要所有事件 4625 和来自 4624 的事件,但前提是登录类型为 2。结果表应该只包含给定的字段(现在是所有字段,稍后是选定字段和一个自定义字段)。 此外,我想在特定列中用 "local" 或 "remote" 和网络数据(IP、用户名、主机名)标记本地登录和远程登录。

Get-winevent -FilterHashtable @{Path="c:\temp\test.evtx";} |
Where-Object {$_.Id -eq 4624 -and $_.properties[8].value -in 2} 
-or
{$_.Id -eq 4625}| export-csv ($EventlogTempFolder+$_.basename + ".csv") -encoding UTF8 -NoTypeInformation -force

如何获得所有字段的列表?从 ID 到消息字段中的所有 属性 字段?

顺便说一句:这段代码没有按预期工作。对不起。

您的代码

Where-Object {$_.Id -eq 4624 -and $_.properties[8].value -in 2} 
-or
{$_.Id -eq 4625}

来自Get-Help Where-Object

Where-Object [-FilterScript] <ScriptBlock> [-InputObject <PSObject>] [<CommonParameters>]

...

Starting in Windows PowerShell 3.0, there are two different ways to construct a Where-Object 
command. Script block . You can use a script block to specify the property name, a comparison 
operator, and a property value. Where-Object returns all objects for which the script block 
statement is true.

For example, the following command gets processes in the Normal priority class, that is, 
processes where the value of the PriorityClass property equals Normal.

`Get-Process | Where-Object {$_.PriorityClass -eq "Normal"}`

问题

Where-Object CmdLet 仅接受单个 脚本块({} 大括号中的位)

修复

Where-Object {
    ($_.Id -eq 4624 -and $_.properties[8].value -in 2)
    -or
    $_.Id -eq 4625
}