function/Invoke-SqlServerUpdatesScan.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#Requires -Version 4.0
function Invoke-SqlServerUpdatesScan {
    <#
.Synopsis
   Returns information about deficit of installed updates at instance SQL Server.
 
.DESCRIPTION
    This command download information about the newest available updates for instance SQL Server.
    Next, it checks build number instance SQL Server in organization (mandatory parameter) and it will return updates required for installation.
 
    Show-SQLServerUpdatesReport can return report in HTML format.
     
    This function use Get-SQLServerUpdates for download information about availability updates for all edition SQL Server.
    Function Invoke-SqlServerUpdatesScan is a part of the module SQLServerUpdateModule. More about its function in help.
 
.NOTES
    Author: Mateusz Nadobnik, [mnadobnik.pl]
    Requires: sysadmin access on SQL Servers
 
    SQLServerUpdates PowerShell module (http://mnadobnik.pl/sqlserverupdates, mnadobnik@gmail.com)
    Copyright (C) 2017 Mateusz Nadobnik
 
    .LINK
    http://mnadobnik.pl/sqlserverupdates
 
.EXAMPLE
    Invoke-SqlServerUpdatesScan -BuildNumber '14.0.3048.4'
 
    Name :
    Product :
    VersionName : SQL Server 2017
    Edition :
    ProductLevel :
    Build : 14.0.3048.4
    Updates : {14.0.3076.1, 14.0.3049.1}
    ToUpdate : True
 
    Returns information about deficit of installed updates for version builid SQL Server.
 
 .EXAMPLE
    Invoke-SqlServerUpdatesScan -BuildNumber '14.0.3048.4' | Select-Object -ExpandProperty Updates
 
    CumulativeUpdate : <a href="https://support.microsoft.com/en-us/help/4484710/cumulative-update-14-for-sql-server-2017">CU14</a>
    ReleaseDate : 2019/03/25
    Build : 14.0.3076.1
    SupportEnds : N/A
    ServicePack :
 
    CumulativeUpdate : <a href="https://support.microsoft.com/en-us/help/4483666/on-demand-hotfix-update-package-for-sql-server-2017-cu13">Hotfix</a>
    ReleaseDate : 2019/01/07
    Build : 14.0.3049.1
    SupportEnds : N/A
    ServicePack :
    ...
 
   Returns information about deficit of installed updates for build number SQL Server. Expand properties Updates.
 
.EXAMPLE
    Invoke-SqlServerUpdatesScan -ServerInstance IT-MN-M
 
    Name : IT-MN-M
    Product : Microsoft SQL Server
    VersionName : SQL Server 2017
    Edition : Developer Edition (64-bit)
    ProductLevel : RTM
    Build : 14.0.1000.169
    Updates : {14.0.3076.1, 14.0.3049.1, 14.0.3048.4, 14.0.3045.24...}
    ToUpdate : True
 
    Returns information about deficit of installed updates for instance with parameter ServerInstance. This command returns objects.
 
.EXAMPLE
    Invoke-SqlServerUpdatesScan -ServerInstance IT-MN-M -SqlCredential mnadobnik
 
    PowerShell credential request
    Enter your credentials.
    Password for user mnadobnik: ***********
 
 
    Name : IT-MN-M
    Product : Microsoft SQL Server
    VersionName : SQL Server 2017
    Edition : Developer Edition (64-bit)
    ProductLevel : RTM
    Build : 14.0.1000.169
    Updates : {14.0.3162.1, 14.0.3076.1, 14.0.3049.1, 14.0.3048.4…}
    ToUpdate : True
 
    Returns information about deficit of installed updates for instance with parameter ServerInstance. This command returns objects.
 
 
.LINK
   Author: Mateusz Nadobnik
   Link: mnadobnik.pl
   Date: 14.05.2010
   Version: 1.1.0.0
     
   Keywords: SQL Server, Updates, Get, Reports, Show
   Notes:
#>


    [CmdletBinding()]
    [Alias('Invoke-SqlUpdatesScan')]
    [OutputType([string])]
    Param
    (
        #The SQL Server instance
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            Position = 0,
            ParameterSetName = 'Instance')]
        [Alias('SqlInstance')]
        $ServerInstance,
        #Build number SQL Server, example 13.0.4422.0
        [Parameter(Mandatory = $true,
            ValueFromPipelineByPropertyName = $true, 
            Position = 1, 
            ParameterSetName = 'Version')]
        [string]$BuildNumber,
        [PSCredential]$SqlCredential
        #Return report HTML
    )
    Begin {
        $ErrorActionPreference = 'Continue'
        $fnName = '[Invoke-SqlServerUpdatesScan]'
        if ($BuildNumber) {
            #Create new object
            #$BuildNumber = '14.0.1000.16'
            $ServerInstance = [PSCustomObject]@{ 
                Name         = ''
                Product      = ''
                Edition      = ''
                ProductLevel = ''
                VersionMajor = ([version]$BuildNumber).Major
                Build        = $BuildNumber
                VersionName  = Get-SQLServerFullName ([version]$BuildNumber).Major
            }
            try {
                Write-Verbose "$fnName Get update list for $($ServerInstance.VersionName)" 
                $UpdateList = Get-SQLServerUpdates -Version $ServerInstance.VersionName
            }
            catch {
                Write-Output $_.Exception.Message
                exit 1
            }
        }
        else {
            try {
                Write-Verbose "Get update list for all SQL Server"
                $UpdateList = Get-SQLServerUpdates
            }
            catch {
                Write-Warning $_.Exception.Message
                exit 1
            }
        }
    }

    Process {
        foreach ($SqlInstance in $ServerInstance) {
            #Clear variables
            $ServerObj = @()
            $UpdatesObj = @()

            if (-not $BuildNumber) {  
                Write-Debug "[if (-not $BuildNumber)]:true"
                Write-Verbose "Run:Get-SQLServerVersion, Parameters :-ServerInstance $SqlInstance"
                if ($SqlCredential) {
                    $Instance = Get-SQLServerVersion -ServerInstance $SqlInstance -SqlCredential $SqlCredential
                }
                else {
                    $Instance = Get-SQLServerVersion -ServerInstance $SqlInstance
                }

                Write-Verbose "Result:Get-SQLServerVersion $Instance"
            }
            else {
                Write-Debug "[if (-not $BuildNumber)]:false"
                Write-Verbose "$SqlInstance"
                $Instance = $SqlInstance
            }

            if ([int]($Instance.VersionMajor) -le 8) {
                Write-Debug "[if ($($Instance.VersionMajor) -le 8)]:true" 
                Write-Warning "Problem with connect or checked you server with SQL Server 2005 and earlier version"
            } 

            #Create custome object
            $ServerObj = [PSCustomObject]@{
                PSTypeName       = 'SqlServerUpdates.Instance'
                Name             = $Instance.Name
                Product          = $Instance.Product
                VersionName      = $Instance.VersionName
                Edition          = $Instance.Edition
                ProductLevel     = $Instance.ProductLevel
                Build            = $Instance.Build
                LatestUpdate     = ""
                LatestUpdateLink = ""
                Updates          = ""
                ToUpdate         = $false
            }


            # If check updates for SQL Server 2005
            if ([int]($Instance.VersionMajor) -eq 9) {
                Write-Debug "[[int]($($Instance.VersionMajor)) -eq 9]:true" 
                $update = [PSCustomObject]@{
                    PSTypeName       = 'SqlServerUpdates.Update'
                    CumulativeUpdate = ""
                    ReleaseDate      = "2012/10/09"
                    Build            = "9.00.5324"
                    SupportEnds      = "2016/04/12 – out of support"
                    ServicePack      = ""
                }
                Add-Member -InputObject $update -MemberType ScriptMethod  -Name ToString -Force -Value { $this.Build }

                $ServerObj.Updates = $update
                $ServerObj.ToUpdate = $true
            }

            if ([int]($Instance.VersionMajor) -ge 9) {
                Write-Debug "[[int]($($Instance.VersionMajor)) -ge 9]:true" 
                $UpdatesList = $UpdateList | Where-Object Name -eq $Instance.VersionName

                if ($UpdatesList[0].Build -eq "") {
                    $UpdatesList[0].Build = $UpdatesList[1].Build
                }
                
                # if SQL Server is latest Version
                if (([version]$Instance.Build -ge [version]$UpdatesList[0].Build) -and ($UpdatesList[0].Build -ne "various")) {
                    $update = [pscustomobject]@{
                        PSTypeName       = 'SqlServerUpdates.Update'
                        CumulativeUpdate = $UpdatesList[0].CumulativeUpdate
                        ReleaseDate      = $UpdatesList[0].ReleaseDate
                        Build            = $UpdatesList[0].Build
                        SupportEnds      = $UpdatesList[0].SupportEnds
                        ServicePack      = $UpdatesList[0].ServicePack
                    }
                    Add-Member -InputObject $update -MemberType ScriptMethod  -Name ToString -Force -Value { $this.Build }

                    $ServerObj.Updates = $update

                    if ([version]$Instance.Build -ge [version]$UpdatesList[0].Build) {
                        Write-Verbose "Setting property: ToUpdate = false"
                        $ServerObj.ToUpdate = $false
                    }
                    else {
                        Write-Verbose "Setting property: ToUpdate = true"
                        $ServerObj.ToUpdate = $true
                    }
                }
                else {
                    foreach ($Update in $UpdatesList) {
                        $outParse = $null
                        if ([Version]::TryParse($Update.Build, [ref]$outParse)) {
                            if ($Update.Build -ne "various") {
                                Write-Debug "[if ($($Update.Build) -ne 'various')]:true"
                                if ([version]$Instance.Build -lt [version]$Update.Build) {
                                    $update = [PSCustomObject]@{
                                        CumulativeUpdate = $Update.CumulativeUpdate
                                        ReleaseDate      = $Update.ReleaseDate
                                        Build            = $Update.Build
                                        SupportEnds      = $Update.SupportEnds
                                        ServicePack      = $Update.ServicePack 
                                    }
                                    Add-Member -InputObject $update -MemberType ScriptMethod  -Name ToString -Force -Value { $this.Build }

                                    $UpdatesObj += $update
                                }
                            }
                        }
                    }
                    $ServerObj.Updates = $UpdatesObj
                    $ServerObj.ToUpdate = $true
                    $ServerObj.LatestUpdate = $UpdatesObj[0].Build
                    $ServerObj.LatestUpdateLink = $UpdatesObj[0].CumulativeUpdate
                }
                $ServerObj
                #$ObjAllSserversWithUpdates += $ObjServer
            }
        }
    }
    End { }
}