1
+ Function Get-WinEventData {
2
+ <#
3
+ . SYNOPSIS
4
+ Get custom event data from an event log record
5
+
6
+ . DESCRIPTION
7
+ Get custom event data from an event log record
8
+
9
+ Takes in Event Log entries from Get-WinEvent, converts each to XML, extracts all properties from Event.EventData.Data
10
+
11
+ Notes:
12
+ To avoid overwriting existing properties or skipping event data properties,
13
+ we append a prefix (default: e_) to these extracted properties.
14
+
15
+ Some events store custom data in other XML nodes.
16
+ For example, AppLocker uses Event.UserData.RuleAndFileData
17
+
18
+ . PARAMETER Event
19
+ One or more event.
20
+
21
+ Accepts data from Get-WinEvent or any System.Diagnostics.Eventing.Reader.EventLogRecord object
22
+
23
+ . PARAMETER Prefix
24
+ Append this to EventData keys to ensure uniqueness. Defaults to e_
25
+
26
+ . INPUTS
27
+ System.Diagnostics.Eventing.Reader.EventLogRecord
28
+
29
+ . OUTPUTS
30
+ System.Diagnostics.Eventing.Reader.EventLogRecord
31
+
32
+ . EXAMPLE
33
+ Get-WinEvent -LogName system -max 1 | Get-WinEventData | Select -Property MachineName, TimeCreated, e_*
34
+
35
+ # Simple example showing the computer an event was generated on, the time, and any custom event data
36
+
37
+ . EXAMPLE
38
+ Get-WinEvent -ComputerName DomainController1 -FilterHashtable @{Logname='security';id=4740} -MaxEvents 10 | Get-WinEventData | Select TimeCreated, e_TargetUserName, e_TargetDomainName
39
+
40
+ # Find lockout events on a domain controller
41
+ # ideally you have log forwarding, audit collection services, or a product from a t-shirt company for this...
42
+
43
+ . NOTES
44
+ Concept and most code borrowed from Ashley McGlone
45
+ http://blogs.technet.com/b/ashleymcglone/archive/2013/08/28/powershell-get-winevent-xml-madness-getting-details-from-event-logs.aspx
46
+
47
+ . FUNCTIONALITY
48
+ Computers
49
+ #>
50
+ [cmdletbinding ()]
51
+ param (
52
+ [Parameter (Mandatory = $true ,
53
+ ValueFromPipeline = $true ,
54
+ ValueFromPipelineByPropertyName = $true ,
55
+ ValueFromRemainingArguments = $false ,
56
+ Position = 0 )]
57
+ [System.Diagnostics.Eventing.Reader.EventLogRecord []]
58
+ $Event ,
59
+
60
+ [string ]$Prefix = ' e_'
61
+ )
62
+
63
+ Process
64
+ {
65
+ # Loop through provided events
66
+ foreach ($entry in $event )
67
+ {
68
+ # Get the XML...
69
+ $XML = [xml ]$entry.ToXml ()
70
+
71
+ # Some events use other nodes, like 'UserData' on Applocker events...
72
+ $XMLData = $null
73
+ if ( $XMLData = @ ( $XML.Event.EventData.Data ) )
74
+ {
75
+ For ( $i = 0 ; $i -lt $XMLData.count ; $i ++ )
76
+ {
77
+ # We don't want to overwrite properties that might be on the original object, or in another event node.
78
+ $Entry = Add-Member - InputObject $entry - MemberType NoteProperty - Name " $Prefix $ ( $XMLData [$i ].name) " - Value $XMLData [$i ].' #text' - Force - Passthru
79
+ }
80
+ }
81
+ $Entry
82
+ }
83
+ }
84
+ }
0 commit comments