【问题标题】:get-winevent: working with propertiesget-winevent:使用属性
【发布时间】:2018-01-29 23:44:45
【问题描述】:

我尝试学习 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 到消息字段中的所有属性字段?

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

【问题讨论】:

  • 应该是 Where-Object {($_.Id -eq 4624 -and $_.properties[8].value -in 2) -or ($_.Id -eq 4625)} | export-csv ($EventlogTempFolder+$_.basename + ".csv") -encoding UTF8 -NoTypeInformation -force 如果你使用 - 或者你没有用 {} 分隔两个状态

标签: powershell get-winevent


【解决方案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"}`

问题

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

修复

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

【讨论】:

  • 感谢您的代码更正@gvee。我怎样才能得到所有 $_ 的列表。参数,例如$_.id 是其中之一(我在某处找到)以及来自消息字段的所有$_.properties[8]?我的意思是,我怎么知道哪个属性是 Logontype-Field?
  • @PeterCore Get-WinEvent -LogName "Windows PowerShell" | Get-Member
  • @PeterCore $_.Properties.GetEnumerator() 也应该有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多