get-winevent:删除描述内容部分后,属性字段填充System.String []

时间:2018-02-02 15:21:36

标签: powershell get-winevent

我正在尝试过滤事件日志,除了查询中的消息字段外,其工作正常。消息字段中包含大量描述性文本。我只想要它的第一句话,因为这是重要的一个,剩下的就是垃圾。

消息字段的示例内容:

An account was successfully logged on.

Subject:
    Security ID:  SYSTEM
    Account Name:  WIN-R9H529RIO4Y$
    Account Domain:  WORKGROUP
    Logon ID:  0x3e7
Logon Type:10
New Logon:
       Security ID:  WIN-R9H529RIO4Y\Administrator
    Account Name:  Administrator
    Account Domain:  WIN-R9H529RIO4Y
    Logon ID:  0x19f4c
    Logon GUID:  {00000000-0000-0000-0000-000000000000}
Process Information:
   Process ID:  0x4c0
    Process Name:  C:\Windows\System32\winlogon.exe
Network Information:
     Workstation Name: WIN-R9H529RIO4Y
    Source Network Address: 10.42.42.211
    Source Port:  1181
Detailed Authentication Information:
     Logon Process:  User32 
    Authentication Package: Negotiate
    Transited Services: -
   Package Name (NTLM only): -
   Key Length:  0

This event is generated when a logon session is created. It is generated on the computer that was accessed.

The subject fields indicate the account on the local system which requested the logon. This is most commonly a service such as the Server service, or a local process such as Winlogon.exe or Services.exe.

The logon type field indicates the kind of logon that occurred. The most common types are 2 (interactive) and 3 (network).

The New Logon fields indicate the account for whom the new logon was created, i.e. the account that was logged on.

The network fields indicate where a remote logon request originated. Workstation name is not always available and may be left blank in some cases.

The authentication information fields provide detailed information about this specific logon request.


The authentication information fields provide detailed information about this specific logon request.
•Logon GUID is a unique identifier that can be used to correlate this event with a KDC event.
•Transited services indicate which intermediate services have participated in this logon request.
•Package name indicates which sub-protocol was used among the NTLM protocols.
•Key length indicates the length of the generated session key. This will be 0 if no session key was requested.

我只想要“帐户已成功登录。”

我已经尝试过(并且失败了):

Get-WinEvent -FilterHashtable @{Path="c:\temp\export.evtx";} |
    Where-Object {($_.id -eq "4624" -and $_.properties[8].value -in 2,3,10) -or ($_.id -eq "4625") -or ($_.id -eq "4800")} |
        ForEach-Object{
            $SelectorStrings = [string[]]@(
            'Event/EventData/Data[@Name="TargetUserName"]',
            'Event/EventData/Data[@Name="TargetDomainName"]',
            'Event/EventData/Data[@Name="TargetLogonId"]',
            'Event/EventData/Data[@Name="LogonType"]',
            'Event/EventData/Data[@Name="WorkstationName"]',
            'Event/EventData/Data[@Name="IpAddress"]',
            'Event/EventData/Data[@Name="IpPort"]'
            )
            $PropertySelector = [System.Diagnostics.Eventing.Reader.EventLogPropertySelector]::new($SelectorStrings)

            $UserName,$Domain,$LogonId,$LogonType,$ComputerName,$IPAddress,$Port = $_.GetPropertyValues($PropertySelector)

            [PSCustomObject]@{
            TimeCreated  = $_.TimeCreated
            UserName     = $UserName
            Domain       = $Domain
            LogonId      = $LogonId
            LogonType    = $LogonType
            ComputerName = $ComputerName
            IPAddres     = $IPAddres
            Port         = $Port
            Message      = ($_.Message).split(".")       
            }|Export-Csv -NoTypeInformation -Force -Encoding UTF8 -Path 'c:\temp\exportencoding2.csv' -Append      
        }

结果是:

"TimeCreated","UserName","Domain","LogonId","LogonType","ComputerName","IPAddres","Port","Message"
"04.12.2017 13:56:34","Testuser","lab.internal",,"7","AssetWin7PC","127.0.0.1","0","System.String[]"

输出是“System.String []”然后它应该是第一句话。

1 个答案:

答案 0 :(得分:1)

TL; DR

($_.Message).split(".")返回一个字符串数组。如果您只想要第一个,那么我会使用Message = ($_.Message).split(".")[0]

更多信息

[PSCustomObject]中,您说“消息”应为($_.Message).split(".")

这会返回一个字符串数组,例如

$Msg = "An account was successfully logged on.`r`nSubject blah blah blah"
($Msg.split(".")).GetType.FullName

enter image description here

打开和关闭方括号是一个有用的小提醒

我们可以通过运行命令并看到我们的单行现在是3行来仔细检查。

$Msg.split(".")

enter image description here

由于我们知道您希望第一个完整停止之前的部分,我们可以指定我们只想返回该部分。

$Msg.split(".")[0]

enter image description here

这应该对我们有用,因为我们返回一个单独的字符串对象(我们可以检查

($Msg.split(".")[0]).GetType().FullName

enter image description here

没有方括号表示1项

您的代码

[PSCustomObject]@{
  TimeCreated  = $_.TimeCreated
  UserName     = $UserName
  Domain       = $Domain
  LogonId      = $LogonId
  LogonType    = $LogonType
  ComputerName = $ComputerName
  IPAddres     = $IPAddres
  Port         = $Port
  Message      = ($_.Message).split(".")[0]       
}