Forked from IISResetMe/ConvertFrom-EventLogRecord.ps1
Created
November 11, 2018 21:46
-
-
Save wikijm/518bf8901752a50cee5a89df1c3fc5c1 to your computer and use it in GitHub Desktop.
Convert EventData fields from windows event log records to objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ConvertFrom-EventLogRecord | |
{ | |
param( | |
[Parameter(Mandatory=$true,ValueFromPipeline=$true)] | |
[System.Diagnostics.Eventing.Reader.EventLogRecord[]] | |
$InputEvent, | |
[Parameter(Mandatory=$true,Position=1)] | |
[ValidateNotNullOrEmpty()] | |
[string[]] | |
$Property | |
) | |
begin { | |
[string[]]$xPathSelectorStrings = $Property |ForEach-Object { | |
if($_ -like '*/*') { | |
$_ | |
} | |
else { | |
'Event/EventData/Data[@Name="{0}"]' -f $_ | |
} | |
} | |
$propertySelector = [System.Diagnostics.Eventing.Reader.EventLogPropertySelector]::new($xPathSelectorStrings) | |
} | |
process { | |
foreach($event in $InputEvent){ | |
$propertyValues = $event.GetPropertyValues($propertySelector) | |
$properties = [ordered]@{} | |
for($i = 0; $i -lt $propertyValues.Count; $i++){ | |
$properties[$Property[$i]-replace'^(?:.*\/)?([^\/]+)$','$1'] = $propertyValues[$i] | |
} | |
[pscustomobject]$properties | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment