function/Get-SQLServerUpdates.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
#Requires -Version 4.0
# https://github.com/AngleSharp/AngleSharp
# https://github.com/AngleSharp/AngleSharp/blob/master/doc/Basics.md

function Get-SQLServerUpdates {
    <#
.Synopsis
    Returns available update list for SQl Server
.DESCRIPTION
    Returns available updates list (cumulatvie update, service pack) for SQL Server. Source for updates list - http://sqlserverupdates.com/
 
    Additional information.
    When I wrote this script I had a problem with Exception 0x800A01B6,
    some tips for me were at this site: https://www.sepago.com/blog/2016/05/03/powershell-exception-0x800a01b6-while-using-getelementsbytagname-getelementsbyname.
    In the script I use IHTMLDocument3_getElementsByTagName instead of method getElementsByTagName.
 
    I recommend to use the script at client stations, but I encourage to make a test at servers alternatively. Please send me information about errors and problems by email mnadobnik+blog@gmail.com
 
.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
   Get-SQLServerUpdateList
 
   Update list for all SQL Server from version 2008 to 2016
.EXAMPLE
   Get-SQLServerUpdateList -Version 2012
 
   Update list only for SQL Server 2012
 
.LINK
   Author: Mateusz Nadobnik
   Link: mnadobnik.pl
   Date: 09.12.2017
   Version: 1.0.1.1
     
   Keywords: SQL Server, Updates, Get
   Notes: 1.0.0.4 - Added new object (Link) with links without marks HTML.
          1.0.0.5 - Repaired error with TLS 1.2 and added SQL Server 2017
          1.0.0.7 - Repaired error Cannot index into a null array.
          1.0.1.0 - Repaired error with SQL Server 2017 and refactoring of code.
          1.0.1.1 - Repaired error with SQL Server 2008 R2
          1.0.1.2 - Bad property outerHTML instead of innerText in the function Get-SQLServerUpdates
          1.1.0.2 - Added SqlCredential
#>

    [CmdletBinding()]
    [Alias()]
    [OutputType([string])]
    Param
    (
        #Version SQL Sever
        [ValidateSet('SQL Server 2008', 
            'SQL Server 2008 R2', 
            'SQL Server 2012', 
            'SQL Server 2014', 
            'SQL Server 2016', 
            'SQL Server 2017')]
        [string]$Version
    )

    Begin {
        $linkRegex = '"[^"]*"'
        $ObjReturn = @()
        $parser = New-Object AngleSharp.Html.Parser.HtmlParser

        try {
            #Enable TLS 1.2
            [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
            $html = Invoke-WebRequest -Uri 'http://sqlserverupdates.com/'
            $content = $parser.ParseDocument($html);
        }
        catch {
            Write-Output "Check connection..."

            Write-Warning $_.Exception.Message
            Break
        }

        # setting for count of column in table on website
        $ColumnSetting = [ordered]@{
            'SQL Server 2008'    = 4
            'SQL Server 2008 R2' = 4
            'SQL Server 2012'    = 5
            'SQL Server 2014'    = 5
            'SQL Server 2016'    = 5
            'SQL Server 2017'    = 4
        }
        
        $VersionSQL = [ordered]@{
            'SQL Server 2008'    = ($content.Links | Where-Object TextContent -like "SQL*2008?U*")
            'SQL Server 2008 R2' = ($content.Links | Where-Object TextContent -like "SQL*2008?R2*")
            'SQL Server 2012'    = ($content.Links | Where-Object TextContent -like "SQL*2012*")
            'SQL Server 2014'    = ($content.Links | Where-Object TextContent -like "SQL*2014*")
            'SQL Server 2016'    = ($content.Links | Where-Object TextContent -like "SQL*2016*")
            'SQL Server 2017'    = ($content.Links | Where-Object TextContent -like "SQL*2017*")
        }
        # if set parameter -Version
        if ($Version) {
            $VersionSQL = [ordered]@{
                $Version = $VersionSQL.$Version
            }
        }
    }
    Process {
        foreach ($SQL in $VersionSQL.Keys) {
            #$SQL = 'SQL Server 2008'
            try {
                $webHtml = Invoke-WebRequest -Uri $VersionSQL.$SQL.href
                $ListUpdates = $parser.ParseDocument($webHtml);
            }
            catch {
                Write-Warning $_.Exception.Message
                Break
            }
                
            try {
                $tableUpdateTR = ($ListUpdates).GetElementsByTagName("tr")
                $tableUpdateTD = $tableUpdateTR | ForEach-Object { ($_.children) }
            }
            catch {                     
                Write-Warning $_.Exception.Message
                Break
            }

            for ($i = $ColumnSetting.$SQL; $i -lt $tableUpdateTD.Count; $i++) { 
                # new object
                $update = @{ } | Select-Object Name, ServicePack, CumulativeUpdate, ReleaseDate, Link, Build, SupportEnds
                $update.Name = ($VersionSQL.$SQL.Text).Replace(" Updates", "")

                if ($ColumnSetting.$SQL -eq 5) {
                    $update.ServicePack = (($tableUpdateTD[$i].innerHTML) -Replace "( &nbsp;|&nbsp;|^\s)", ""); $i++
                    $update.CumulativeUpdate = (($tableUpdateTD[$i].innerHTML) -Replace "( &nbsp;|&nbsp;|^\s)", ""); $i++
                    $update.ReleaseDate = (($tableUpdateTD[$i].innerHTML) -Replace "( &nbsp;|&nbsp;|^\s|\?\?)", "").Trim(); $i++
                    $update.Build = (($tableUpdateTD[$i].innerHTML) -Replace "( &nbsp;|&nbsp;|^\s)", ""); $i++
                    $update.SupportEnds = (($tableUpdateTD[$i].innerHTML) -Replace "( &nbsp;|&nbsp;|^\s|\?\?)", "").Trim()
                }
                elseif ($ColumnSetting.$SQL -eq 4) {
                    $update.ServicePack = ''
                    $update.CumulativeUpdate = (($tableUpdateTD[$i].innerHTML) -Replace "( &nbsp;|&nbsp;|^\s)", ""); $i++
                    $update.ReleaseDate = (($tableUpdateTD[$i].innerHTML) -Replace "( &nbsp;|&nbsp;|^\s|\?\?)", "").Trim(); $i++
                    $update.Build = (($tableUpdateTD[$i].innerHTML) -Replace "( &nbsp;|&nbsp;|^\s)", ""); $i++
                    $update.SupportEnds = (($tableUpdateTD[$i].innerHTML) -Replace "( &nbsp;|&nbsp;|^\s|\?\?)", "").Trim()
                }

                try {

                    if ([regex]::Matches($update.CumulativeUpdate, $linkRegex) -ne "") {
                        $update.Link = ([regex]::Matches($update.CumulativeUpdate, $linkRegex)[0].Value).Replace('"', '')
                    }
                    elseif ([regex]::Matches($object.ServicePack, $linkRegex) -ne "") {
                        $update.Link = ([regex]::Matches($update.ServicePack, $linkRegex)[0].Value).Replace('"', '')
                    }
                }
                catch {
                    $update.Link = $null
                }

                $ObjReturn += $update
            }

        }
    }
    End {
        return $ObjReturn
    }
}