Last active
May 28, 2024 08:19
-
-
Save kmpoppe/38ad8dccbba388b58cfdee8374951776 to your computer and use it in GitHub Desktop.
Show SQL Server Backup Version with PowerShell
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
param( | |
$FileToProcess | |
) | |
# Imports | |
Add-Type -Assembly System.Web | |
Import-Module "sqlps" -DisableNameChecking | |
# Get RESTORE HEADERONLY | |
$sqlServer = "<...ENTER YOUR SQL SERVER HERE...>" # may also include \INSTANCE | |
# Force DataSet to be an array so that backupsets with 1 and n backups work | |
$dataSet = @(Invoke-Sqlcmd -Query "RESTORE HEADERONLY FROM DISK='$FileToProcess'" -ServerInstance $sqlServer -As DataSet) | |
$dataRow = $dataSet.Tables.Rows[0] | |
$backupVersion = | |
"$($dataRow.SoftwareVersionMajor)." ` | |
+ "$($dataRow.SoftwareVersionMinor)." ` | |
+ "$($dataRow.SoftwareVersionBuild)" | |
# Query sqlserverbuilds.blogspot's Google Sheet for "Build" column LIKE $backupVersion% | |
# because that column also contains the Revision that we don't get from the backup header. | |
$Query = "select * where C like '" + $backupVersion + "%'" | |
$URL = | |
"https://docs.google.com/spreadsheets/d/16Ymdz80xlCzb6CwRFVokwo0onkofVYFoSkc7mYe6pgw/gviz/tq?tq=" ` | |
+ [System.Web.HttpUtility]::UrlEncode($Query) ` | |
+ "&tqx=out:json" | |
# Headers needed to unf**k Google's JSON output | |
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" | |
$headers.Add('X-DataSource-Auth','true') | |
$webReturn = (Invoke-WebRequest $URL -Headers $headers).Content | |
# Even more unf**king needed | |
$realJson = $webReturn.Substring(5) | |
# Finally, a JSON object | |
$jsonObj = $realJson | ConvertFrom-Json | |
# Make PowerShell display what we found out | |
$message = "File $($FileToProcess)`nwas created with Version $($backupVersion) ($($jsonObj.table.rows[0].c[4].v))" | |
$message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment