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

get-winevent: Property Field filled with System.String[] after removing descriptional content parts

我正在尝试过滤事件日志,它工作正常,但查询中的消息字段除外。消息字段中有很多描述性文本。我只想要它的第一句话,因为这是重要的,其余的都是垃圾。

消息字段的示例内容:

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.

我只要"An account was successfully logged on."

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

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[]",但它应该是第一句话。

TL;DR

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


更多信息

在你的[PSCustomObject]中,你说"Message"应该是($_.Message).split(".")

这 return 是一个字符串数组,例如

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

左方括号和右方括号很有帮助,小小的提醒

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

$Msg.split(".")

因为我们知道您想要第一个句号之前的部分,我们可以指定我们只想要 return 那部分。

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

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

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

无方括号表示 1 项


您的代码

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