Skip to content

Instantly share code, notes, and snippets.

@billinkc
Last active September 8, 2021 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billinkc/58d638261ecb69f4db418c6857b5d526 to your computer and use it in GitHub Desktop.
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
# 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