Last active
September 8, 2021 17:19
-
-
Save billinkc/58d638261ecb69f4db418c6857b5d526 to your computer and use it in GitHub Desktop.
Assuming you wanted to grab the info from a SQL Server install log - specifically the Exit Code, here's some terrible PowerShell to do so
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
# Sample data of what Summary.txt looks like | |
<# | |
Overall summary: | |
Final result: Passed | |
Exit code (Decimal): 0 | |
Start time: 2021-02-26 03:03:25 | |
End time: 2021-02-26 03:04:36 | |
Requested action: Patch | |
Instance DEV2019UTF8 overall summary: | |
Final result: Passed | |
Exit code (Decimal): 0 | |
Start time: 2021-02-26 03:03:48 | |
End time: 2021-02-26 03:04:32 | |
Requested action: Patch | |
... | |
#> | |
$logLocation = "C:\Program Files\Microsoft SQL Server\150\Setup Bootstrap\Log\Summary.txt" | |
$regex = "Exit code \(Decimal\):\s+(\d)" | |
$output = Select-String -Path $logLocation -Pattern $regex | |
foreach ($match in $output.Matches) | |
{ | |
#Group 0 is the text before the match code, group 1 is the named match | |
if ($match.Success) | |
{ | |
Write-Output $match.Groups[1].Captures[0].Value | |
break | |
} | |
} | |
# or try and use this godawful mess | |
# Find the line with Exit code (there can be multiples). | |
# Grab the first one and only get the Line property | |
$firstMatch = Select-String -Path $logLocation -Pattern "Exit code (Decimal)" -SimpleMatch | Select-Object -Property Line -First 1 | |
# Split the resulting line on the colon and take the second half and strip off whitespace | |
$finalValue = ($firstMatch.Line -split ":")[1].trim() | |
Write-Output $finalValue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment