0

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

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

在这个简单的示例中,我想要所有事件 4625 和来自 4624 的事件,但前提是 logontype 为 2。结果表应该只包含给定的字段(现在是所有字段,稍后是选定字段和一个自定义字段)。另外,我想用“本地”或“远程”和网络数据(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 到消息字段中的所有属性字段?

顺便说一句:此代码未按预期工作。对此感到抱歉。

4

1 回答 1

1

你的代码

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"}`

问题

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

使固定

Where-Object {
    ($_.Id -eq 4624 -and $_.properties[8].value -in 2)
    -or
    $_.Id -eq 4625
}
于 2018-01-29T15:44:45.840 回答