PowerShell - 从 security.etvx 个文件中抓取用户

PowerShell - Grabbing user from security.etvx files

我根本不习惯使用 PowerShell,但到目前为止我有以下代码来获取 4625 事件

Get-WinEvent -MaxEvents 1 -FilterHashtable @{Path="C:\Users\ScriptTesting\Desktop\Security.evtx";ProviderName="Microsoft-Windows-Security-Auditing";Id=4625} | Format-List -Property *

这给了我

的输出
Message              : An account failed to log on.
                       
                       Subject:
                        Security ID:        x
                        Account Name:       x
                        Account Domain:     x
                        Logon ID:       0x3E7
                       
                       Logon Type:          2
                       
                       Account For Which Logon Failed:
                        Security ID:        x
                        Account Name:       ScriptTesting
                        Account Domain:     x
                       
                       Failure Information:
                        Failure Reason:     Unknown user name or bad password.
                        Status:         0xC000006D
                        Sub Status:     0xC000006A
                       
                       Process Information:
                        Caller Process ID:  0x21c
                        Caller Process Name:    C:\Windows\System32\svchost.exe
                       
                       Network Information:
                        Workstation Name:   x
                        Source Network Address: 127.0.0.1
                        Source Port:        0
                       
                       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 request fails. It is generated on the computer where access was 
                       attempted.
                       
                       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 was requested. The most common types are 2 
                       (interactive) and 3 (network).
                       
                       The Process Information fields indicate which account and process on the system requested the logon.
                       
                       The Network Information 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.
                        - 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.
Id                   : 4625
Version              : 0
Qualifiers           : 
Level                : 0
Task                 : 12544
Opcode               : 0
Keywords             : -9218868437227405312
RecordId             : 24320
ProviderName         : Microsoft-Windows-Security-Auditing
ProviderId           : x
LogName              : Security
ProcessId            : 544
ThreadId             : 6744
MachineName          : x
UserId               : 
TimeCreated          : 3/4/2022 2:24:13 PM
ActivityId           : x
RelatedActivityId    : 
ContainerLog         : c:\users\scripttesting\desktop\security.evtx
MatchedQueryIds      : {}
Bookmark             : System.Diagnostics.Eventing.Reader.EventBookmark
LevelDisplayName     : Information
OpcodeDisplayName    : Info
TaskDisplayName      : Logon
KeywordsDisplayNames : {Audit Failure}
Properties           : {System.Diagnostics.Eventing.Reader.EventProperty, System.Diagnostics.Eventing.Reader.EventProperty, 
                       System.Diagnostics.Eventing.Reader.EventProperty, 
                       System.Diagnostics.Eventing.Reader.EventProperty...}

我的问题是我需要消息部分中包含的帐户名中的“ScriptTesting”。我得到了

Get-EventLog -LogName Security -Newest 10 | Select @{Name="UserName";Expression={ $_.ReplacementStrings[1] }}

获取帐户名,但我似乎无法为 Get-EventLog 使用文件,那么在声明 evtx 文件的文件路径时获取该帐户名的最简单方法是什么? (由于有一个单独的进程,我需要能够从 evtx 文件中提取它,而不是从机器上运行。)

提前致谢!

我相信这应该可行,不过,可能还有更好的方法。我添加了一个 TimeCreated 属性 所以至少你有一些参考。

$events = Get-WinEvent "C:\Users\ScriptTesting\Desktop\Security.evtx"
foreach($event in $events) {
    if($event.Id -ne 4625) { continue }
    [pscustomobject]@{
        TimeCreated = $event.TimeCreated
        TargetUser  = $event.Properties[5].Value
    }
}