|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Gets the supported database compatibility levels for a SQL Server instance or version. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + This command returns the supported database compatibility levels for a SQL Server |
| 7 | + Database Engine instance or a specific SQL Server version. |
| 8 | +
|
| 9 | + The compatibility levels are determined based on the SQL Server version, following |
| 10 | + the official Microsoft documentation for supported compatibility level ranges. |
| 11 | +
|
| 12 | + .PARAMETER ServerObject |
| 13 | + Specifies the SQL Server connection object to get supported compatibility levels for. |
| 14 | +
|
| 15 | + .PARAMETER Version |
| 16 | + Specifies the SQL Server version to get supported compatibility levels for. |
| 17 | + Only the major version number is used for determining compatibility levels. |
| 18 | +
|
| 19 | + .EXAMPLE |
| 20 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 21 | + Get-SqlDscCompatibilityLevel -ServerObject $serverObject |
| 22 | +
|
| 23 | + Returns all supported compatibility levels for the connected SQL Server instance. |
| 24 | +
|
| 25 | + .EXAMPLE |
| 26 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 27 | + $serverObject | Get-SqlDscCompatibilityLevel |
| 28 | +
|
| 29 | + Returns all supported compatibility levels using pipeline input. |
| 30 | +
|
| 31 | + .EXAMPLE |
| 32 | + Get-SqlDscCompatibilityLevel -Version '16.0.1000.6' |
| 33 | +
|
| 34 | + Returns all supported compatibility levels for SQL Server 2022 (version 16). |
| 35 | +
|
| 36 | + .INPUTS |
| 37 | + Microsoft.SqlServer.Management.Smo.Server |
| 38 | +
|
| 39 | + The server object to get supported compatibility levels for. |
| 40 | +
|
| 41 | + .OUTPUTS |
| 42 | + System.String[] |
| 43 | +
|
| 44 | + Returns an array of supported compatibility level names (e.g., 'Version160', 'Version150'). |
| 45 | +#> |
| 46 | +function Get-SqlDscCompatibilityLevel |
| 47 | +{ |
| 48 | + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')] |
| 49 | + [CmdletBinding(DefaultParameterSetName = 'ServerObject')] |
| 50 | + [OutputType([System.String[]])] |
| 51 | + param |
| 52 | + ( |
| 53 | + [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true, ValueFromPipeline = $true)] |
| 54 | + [Microsoft.SqlServer.Management.Smo.Server] |
| 55 | + $ServerObject, |
| 56 | + |
| 57 | + [Parameter(ParameterSetName = 'Version', Mandatory = $true)] |
| 58 | + [System.Version] |
| 59 | + $Version |
| 60 | + ) |
| 61 | + |
| 62 | + process |
| 63 | + { |
| 64 | + # Get the major version based on parameter set |
| 65 | + $majorVersion = if ($PSCmdlet.ParameterSetName -eq 'ServerObject') |
| 66 | + { |
| 67 | + Write-Verbose -Message ($script:localizedData.GetCompatibilityLevel_GettingForInstance -f $ServerObject.InstanceName, $ServerObject.VersionMajor) |
| 68 | + $ServerObject.VersionMajor |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + Write-Verbose -Message ($script:localizedData.GetCompatibilityLevel_GettingForVersion -f $Version, $Version.Major) |
| 73 | + $Version.Major |
| 74 | + } |
| 75 | + |
| 76 | + # Get all available compatibility levels from the enum |
| 77 | + $allCompatibilityLevels = [System.Enum]::GetNames([Microsoft.SqlServer.Management.Smo.CompatibilityLevel]) |
| 78 | + |
| 79 | + <# |
| 80 | + Determine minimum supported compatibility level based on SQL Server version |
| 81 | + Reference: https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-compatibility-level |
| 82 | + #> |
| 83 | + $minimumCompatLevel = switch ($majorVersion) |
| 84 | + { |
| 85 | + { $_ -ge 12 } |
| 86 | + { |
| 87 | + 100 # SQL 2014 (v12) and later support minimum compat level 100 |
| 88 | + } |
| 89 | + |
| 90 | + 11 |
| 91 | + { |
| 92 | + 90 # SQL 2012 (v11) supports minimum compat level 90 |
| 93 | + } |
| 94 | + |
| 95 | + { $_ -le 10 } |
| 96 | + { |
| 97 | + 80 # SQL 2008 R2 (v10.5) and earlier support minimum compat level 80 |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + <# |
| 102 | + Filter compatibility levels that are supported by this SQL Server version |
| 103 | + CompatibilityLevel enum values are named like "Version80", "Version90", etc. |
| 104 | + SQL Server supports compatibility levels from the minimum up to (version * 10) |
| 105 | + #> |
| 106 | + $supportedCompatibilityLevels = $allCompatibilityLevels | Where-Object -FilterScript { |
| 107 | + if ($_ -match 'Version(\d+)') |
| 108 | + { |
| 109 | + $compatLevelVersion = [System.Int32] $Matches[1] |
| 110 | + ($compatLevelVersion -ge $minimumCompatLevel) -and ($compatLevelVersion -le ($majorVersion * 10)) |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + $false |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + <# |
| 119 | + Warn if SQL Server version is newer than what SMO library supports |
| 120 | + Check if the expected maximum compatibility level is missing from the supported list |
| 121 | + #> |
| 122 | + $expectedMaxCompatLevel = "Version$($majorVersion * 10)" |
| 123 | + if ($expectedMaxCompatLevel -notin $supportedCompatibilityLevels -and $supportedCompatibilityLevels.Count -gt 0) |
| 124 | + { |
| 125 | + # Get the actual maximum from the last element (they're in ascending order) |
| 126 | + $lastCompatLevel = $supportedCompatibilityLevels[-1] |
| 127 | + if ($lastCompatLevel -match 'Version(\d+)') |
| 128 | + { |
| 129 | + $maxCompatLevelInEnum = [System.Int32] $Matches[1] |
| 130 | + Write-Warning -Message ($script:localizedData.GetCompatibilityLevel_SmoTooOld -f $majorVersion, $maxCompatLevelInEnum, ($majorVersion * 10)) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + Write-Debug -Message ($script:localizedData.GetCompatibilityLevel_Found -f $supportedCompatibilityLevels.Count, $majorVersion) |
| 135 | + |
| 136 | + return $supportedCompatibilityLevels |
| 137 | + } |
| 138 | +} |
0 commit comments