Get-WinEvent仅获取交互式登录消息

时间:2010-12-08 23:36:29

标签: powershell powershell-v2.0

我试图让这个PS脚本从多台计算机中提取安全日志,只搜索事件ID为4624,并且只显示包含“登录类型:2”或交互式登录的日志。除了仅获取用于交互式登录的日志之外,我还有其他所有工作。这是我的脚本片段,如果有人知道如何实现这一目标,我将不胜感激。如果我从“登录类型”中取出2,它可以工作,我可以获得所有内容,但如果我之后有任何内容,它不会产生任何错误,但它也不会产生任何结果。是的,我已经验证我在过滤的时间范围内有交互式登录事件。感谢。

$服务器; Get-WinEvent -computername $ server -FilterHashTable @ {Logname = $ logname; ID = $ eventid; StartTime = $ starttime; EndTime = $ endtime} |其中{$ _。消息| Select-String“登录类型:2”}

5 个答案:

答案 0 :(得分:2)

EventRecord.properties在列表中有登录类型。要过滤掉今天成功的交互式登录类型的登录事件:

Get-winevent -FilterHashtable @{logname='security'; id=4624; starttime=(get-date).date} | where {$_.properties[8].value -eq 2}

答案 1 :(得分:2)

为获得最佳速度,您应通过Xpath进行如下过滤:

Get-WinEvent -ProviderName 'Microsoft-Windows-Security-Auditing' -FilterXPath "*[System[EventID=4624] and EventData[Data[@Name='LogonType']='2']]" | select -First 1

答案 2 :(得分:1)

如果其他人试图做同样的事情,它会在“登录类型:”之后寻找额外的空格它希望它看起来像在log iteself中那样,“登录类型:2”我不是确定如何在powershell中解决这个问题,但是这样做对我来说是个窍门。

答案 3 :(得分:1)

我研究了几个解决这个问题的方法。我认为它们可能很有用,因为识别登录类型很重要。 -RMF

Get-WinEvent -max 1000 |其中{$ _。消息| findstr / C:“登录类型”} |选择消息| fl * | findstr / C:“登录类型”

登录类型:5   登录类型:7   ...

Get-WinEvent Security -max 1000 |选择ID,级别,消息|其中{$ _。消息| findstr / C:“登录类型”} | ft -auto -wrap |更多

Id级别消息


4624 0帐户已成功登录。

       Subject:
           Security ID:        (deleted)
           Account Name:        (deleted)
           Account Domain:        (deleted)
           Logon ID:        0x3e7

       Logon Type:            5

...

Get-WinEvent -max 10 -FilterHashtable @ {Logname ='security'; ID = 4624} |选择TimeCreated,MachineName,Message | ft -auto -wrap |更多

TimeCreated MachineName消息 ----------- ----------- ------- 6/29/2011 12:36:35 PM(已删除)帐户已成功登录。

                              Subject:
                                  Security ID:        (deleted)
                                  Account Name:        (deleted)
                                  Account Domain:        (deleted)
                                  Logon ID:        0x3e7

                              Logon Type:            5

...

Get-WinEvent -max 10 -FilterHashtable @ {Logname ='security'; ID = 4624} |选择TimeCreated,MachineName,Message |选择字符串“登录类型”|更多

@ {TimeCreated = 06/29/2011 12:36:35;计算机名=(删除);消息=帐户已成功登录。

Subject:
                                  Security ID:        (deleted)
                                  Account Name:        (deleted)
                                  Account Domain:        (deleted)
                                  Logon ID:        0x3e7

                              Logon Type:            5

...

最后一种方法从Message per logon事件中挖掘选择信息,添加TimeCreated字段,并为安全日志中的所有登录尝试(Id = 4624)提供类似数据库格式的内容。结果附加到csv。

$ LogonTypes = Get-WinEvent -FilterHashtable @ {Logname ='security'; Id = 4624}

foreach($ item in $ LogonTypes){($ item | Select TimeCreated,Message | fl * | findstr /G:search.lst)-replace“”,“” - join“,”| out-file -append test3.csv}

where(columnar)search.lst:

TimeCreated   安全ID:   用户名:   帐户域名:   登录ID:   登录类型:   登录GUID:   流程名称:

答案 4 :(得分:1)

在本文顶部的第一个代码示例中,如何匹配分号和数字2之间的空格问题的解决方案是使用像这样\s+编写的PowerShell正则表达式模式。

模式字符区分大小写,通常与“-match”运算符一起使用,但可以与海报原始查询中所写的Select-String命令行开关一起使用。修改后的代码如下所示:

Get-WinEvent -FilterHashTable @{LogName="Security";ID=4624} | where { $_.Message | Select-String "Logon Type:\s+2"} 

此外,如果PowerShell脚本需要查询仍使用经典事件日志的旧操作系统,则Get-EventLog命令行开关同样可以使用与此处所示相同的模式:

Get-EventLog -LogName Security -InstanceID 4624 | Where {$_.Message -match "Logon Type:\s+2"}

PowerShell正则表达式引用:

https://technet.microsoft.com/en-us/magazine/2007.11.powershell.aspx https://www.petri.com/powershell-string-parsing-with-regular-expressions

注意:此答案中引用的正则表达式模式由Microsoft描述为“character class”

Clark Froebe

相关问题