functions/Get-DbaSqlBuildReference.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
Function Get-DbaSqlBuildReference
{
<#
.SYNOPSIS
Returns SQL Server Build infos on a SQL instance
 
.DESCRIPTION
Returns info about the specific build of a SQL instance, including the SP, the CU and the reference KB, wherever possible.
It also includes End Of Support dates as specified on Microsoft Lifecycle Policy
 
.PARAMETER Build
Instead of connecting to a real instance, pass a string identifying the build to get the info back.
  
.PARAMETER SqlInstance
Optionally, an SQL Server SMO object can be passed to the command to be parsed.
 
.PARAMETER Silent
Use this switch to disable any kind of verbose messages
 
.NOTES
Author: niphlod
Tags: SqlBuild
 
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
.LINK
https://dbatools.io/Get-DbaSqlBuildReference
 
.EXAMPLE
Get-DbaSqlBuildReference -Build "12.00.4502"
 
Returns information about a build identified by "12.00.4502" (which is SQL 2014 with SP1 and CU11)
 
.EXAMPLE
Get-DbaSqlBuildReference -Build "12.0.4502","10.50.4260"
 
Returns information builds identified by these versions strings
 
.EXAMPLE
Get-SqlRegisteredServerName -SqlServer sqlserver2014a | Foreach-Object { Connect-DbaSqlServer -SqlServer $_ } | Get-DbaSqlBuildReference
 
Integrate with other commandlets to have builds checked for all your registered servers on sqlserver2014a
 
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    Param (
        [string[]]$Build,
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [Microsoft.SqlServer.Management.Smo.Server[]]$SqlInstance,
        [switch]$Silent
    )

    BEGIN {
        function Find-DbaSqlBuildReferenceIndex {
            [CmdletBinding()]
            Param()
            $orig_idxfile = "$moduledirectory\bin\dbatools-buildref-index.json"
            $DbatoolsData = Get-DbaConfigValue -Name 'Path.DbatoolsData'
            $writable_idxfile = Join-Path $DbatoolsData "dbatools-buildref-index.json"
            if (!(Test-Path $orig_idxfile)) {
                Write-Message -Message "Unable to read local file" -Warning -Silent $Silent
            } else {
                if(!(Test-Path $writable_idxfile)) {
                    Copy-Item $orig_idxfile $writable_idxfile
                }
                return (Get-Content -Raw $writable_idxfile)
            }
        }

        function Get-DbaSqlBuildReferenceIndex {
            [CmdletBinding()]
            Param()
            return Find-DbaSqlBuildReferenceIndex | ConvertFrom-Json
        }

        function Compare-DbaSqlBuildGreater {
            [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
            [CmdletBinding()]
            [OutputType([bool])]
            Param([string]$firstref, [string]$secondref)
            $first = $firstref.split('.') | Foreach-Object { [convert]::ToInt32($_) }
            $second = $secondref.split('.') | Foreach-Object { [convert]::ToInt32($_) }
            $x = 0
            while($true) {
                if($first[$x] -gt $second[$x]) {
                    return $true
                }
                $x += 1
                if($x -gt $first.Length) {
                    return $false
                }
            }
        }

        $moduledirectory = $MyInvocation.MyCommand.Module.ModuleBase

        $IdxRef = Get-DbaSqlBuildReferenceIndex
        $LastUpdated = Get-Date -Date $IdxRef.LastUpdated
        if ($LastUpdated -lt (Get-Date).AddDays(-45)) {
            Write-Message -Message "Index is Stale, Last Update on: $(Get-Date -Date $LastUpdated -f s)" -Warning
        }

        function Resolve-DbaSqlBuild {
            [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
            [CmdletBinding()]
            [OutputType([System.Collections.Hashtable])]
            Param([string]$build)
            $LookFor = ($build.split('.')[0..2] | Foreach-Object { [convert]::ToInt32($_) }) -join '.'
            Write-Message -Message "Looking for $LookFor" -Level 5 -Silent $Silent
            $SqlVersion = $build.split('.')[0..1] -join '.'
            $IdxVersion = $IdxRef.Data | Where-Object Version -like "$SqlVersion.*"
            $Detected = @{}
            $Detected.MatchType = 'Approximate'
            Write-Message -Message "We have $($IdxVersion.Length) in store for this Release" -Level 5 -Silent $Silent
            If ($IdxVersion.Length -eq 0)
            {
                Write-Message -Message "No info in store for this Release" -Warning -Silent $Silent
                $Detected.Warning = "No info in store for this Release"
            }
            else
            {
                $LastVer = $IdxVersion[0]
            }
            foreach($el in $IdxVersion) {
                if($null -ne $el.Name) {
                    $Detected.Name = $el.Name
                }
                if(Compare-DbaSqlBuildGreater -firstref $el.Version -secondref $LookFor) {
                    $Detected.MatchType = 'Approximate'
                    $Detected.Warning = "$LookFor not found, closest build we have is $($LastVer.Version)"
                    break
                }
                $LastVer = $el
                if($null -ne $el.SP) {
                    $Detected.SP = $el.SP
                    $Detected.CU = $null
                }
                if($null -ne $el.CU) {
                    $Detected.CU = $el.CU
                }
                if($null -ne $el.SupportedUntil) {
                    $Detected.SupportedUntil = (Get-Date -date $el.SupportedUntil)
                }
                $Detected.KB = $el.KBList
                if($el.Version -eq $LookFor) {
                    $Detected.MatchType = 'Exact'
                    break
                }
            }
            return $Detected
        }
    }
    PROCESS
    {
        foreach ($instance in $SqlInstance)
        {
            $Detected = Resolve-DbaSqlBuild -Build $instance.Version.ToString()
            
            [PSCustomObject]@{
                SqlInstance = $instance.DomainInstanceName
                Build = $instance.Version.ToString()
                NameLevel = $Detected.Name
                SPLevel = $Detected.SP
                CULevel = $Detected.CU
                KBLevel = $Detected.KB
                SupportedUntil = $Detected.SupportedUntil
                MatchType = $Detected.MatchType
                Warning = $Detected.Warning
            }
        }
        
        foreach($buildstr in $Build) {
            $Detected = Resolve-DbaSqlBuild -Build $buildstr
            
            [PSCustomObject]@{
                    SqlInstance = $null
                    Build = $buildstr
                    NameLevel = $Detected.Name
                    SPLevel = $Detected.SP
                    CULevel = $Detected.CU
                    KBLevel = $Detected.KB
                    SupportedUntil = $Detected.SupportedUntil
                    MatchType = $Detected.MatchType
                    Warning = $Detected.Warning
            } | Select-DefaultView -ExcludeProperty SqlInstance
        }
    }
}