Functions/function-Test-DatabaseVersion.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function Test-DatabaseVersion
{
 <#
  .SYNOPSIS
    Check db is a version that can be restored
  .DESCRIPTION
    Given a set of files to restore, checks they can be restored on the specified SQL Server version
    Basic check of not being able to restore backups from more than 2 Major Versions before.
  .EXAMPLE
    Test-DatabaseVersion -Backups $Backups -SQLServer $sqlsvr
  .PARAMETER Backups
    A list of SQL backups, as provided by Get-DBBackupObject
  .PARAMETER RestoreSQLServer
    SMO SQL Server connection objectr
#>

  [CmdletBinding()]
  param
  (
   [Parameter(Mandatory=$True)]
    [object]$BackupObject,
    [Parameter(Mandatory=$True)]
    [Microsoft.SqlServer.Management.Smo.SqlSmoObject]$RestoreSQLServer
  )
   Write-Verbose "Test-DatabaseVersion - Entering " 
   $version = Get-SQLServerMajorVersion -RestoreSQLServer $RestoreSQLServer
  if(($version -gt ($BackupObject[0].SQLVersion+2)) -or ($version -lt ($BackupObject[0].SQLVersion)) ){
        Write-debug "Cannot restore a backup due to SQL Server Version incompatabilities. Backup is version $($BackupObject[1].SQLVersion) and SQL Server is $($RestoreSQLServer.versionMajor)"
        $false 
  }else{
        $true
    }
  Write-Verbose "Test-DatabaseVersion - Leaving" 
}

function Get-SQLServerMajorVersion
{
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory=$True)]
    [Microsoft.SqlServer.Management.Smo.SqlSmoObject]$RestoreSQLServer
  )
  return $RestoreSQLServer.VersionMajor
}