Last active
January 10, 2024 11:09
-
-
Save winkler-winsen/6d3eaaf6510ad8947b816aefc01c98fd to your computer and use it in GitHub Desktop.
PowerShell export RegEx matches with named capture groups to CSV
This file contains hidden or 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
$LogPath='C:\Temp' | |
$LogPath+='\' | |
$LogFilename='Filename.log' | |
# Use RegEx named capture groups for automatic PropertyName e.g. (?<GroupName>.+) | |
$LogPattern='^(?<date>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2},\d{3})\s(?<level>[A-Z]+)\s(?<component>\[[\w.]+\])\s\((?<object>\S+)\)\s(?<exception>[\w\s:.\(\)]+)\s\[(?<eceptionid>\d+)\]\s(?<error>\w+)\s(?<errorid>[-\d]+)' | |
$A=Select-String -Pattern $LogPattern -Path $LogPath$LogFilename | |
$Output = @() | |
foreach ($M in $A.Matches) { | |
$data = New-Object PSCustomObject | |
for ($i=1; $i -lt $M.Groups.Count; $i++) { | |
$M.Groups[$i] | ForEach-Object { | |
$data | Add-Member -NotePropertyName $_.Name -NotePropertyValue $_.Value | |
#Write-Host $_.Name $_.Value | |
} | |
} | |
$Output+=$data | |
} | |
$Output | Export-Csv -Delimiter ';' -NoTypeInformation -Encoding UTF8 -Path "$LogFilename.csv" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment