Skip to content

Instantly share code, notes, and snippets.

@mikerodionov
Created May 15, 2019 11:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mikerodionov/cced55743e882f3ee36eaec76a4228a2 to your computer and use it in GitHub Desktop.
#This script retrieves SQL Server version from BAK file and allows you to compare with your SQL Server version
#If your SQL Server version is lower that one which was used to create backup you won't be able to restore BAK file
$BakFile= Read-Host "Provide full path to SQL BAK file (including file name with extension)"
$VerDetectQuery="RESTORE HEADERONLY FROM DISK='$BakFile'"
$BakMajorVer=Invoke-Sqlcmd -Query $VerDetectQuery | Select-Object -ExpandProperty SoftwareVersionMajor
$BakMinorVer=Invoke-Sqlcmd -Query $VerDetectQuery | Select-Object -ExpandProperty SoftwareVersionMinor
$BakBuildVer=Invoke-Sqlcmd -Query $VerDetectQuery | Select-Object -ExpandProperty SoftwareVersionBuild
$BakVer= "$BakMajorVer.$BakMinorVer.$BakBuildVer.0"
$ServerSQLVer=Invoke-Sqlcmd -Query "SELECT SERVERPROPERTY('productversion')" | Select-Object -ExpandProperty Column1
Write-Host "Backup file SQL Server version is: " $BakVer
Write-Host "Your SQL Server version is: " $ServerSQLVer
If ($BakVer -le $ServerSQLVer)
{
Write-Host "You can restore this backup on your server" -BackgroundColor Green
}
Else
{
Write-Host "You cannot restore backup on your server, to be able to do that you need to upgrade SQL Server version" -BackgroundColor Red
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment