Skip to content

Commit

Permalink
Update and refactor SQL cumulative updates
Browse files Browse the repository at this point in the history
  • Loading branch information
flcdrg committed Dec 15, 2024
1 parent e98f741 commit a982ec3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 186 deletions.
72 changes: 72 additions & 0 deletions _scripts/SqlServerCumulativeUpdates.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
. ..\_scripts\Submit-VirusTotal.ps1


function SearchReplace($MajorVersion, $Latest) {
@{
# softwareName = 'Hotfix 3026 for Microsoft SQL Server*(KB4229789)*'
'tools\chocolateyInstall.ps1' = @{
"(^[$]url\s*=\s*)('.*')" = "`$1'$($Latest.URL64)'"
"(^[$]checksum\s*=\s*)('.*')" = "`$1'$($Latest.Checksum64)'"
"(^[$]softwareName\s*=\s*)('.*')" = "`$1'Hotfix $($Latest.Build) for SQL Server $MajorVersion*(KB$($Latest.KB))*'"
}
}
}

function GetLatest($downloadId, $MajorVersion) {
$response = Invoke-WebRequest -Uri "https://www.microsoft.com/en-us/download/details.aspx?id=$downloadId" -ErrorAction Ignore

$url = $response.Content |
Select-String -AllMatches -Pattern "(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?" |
ForEach-Object { $_.Matches.Value } |
Select-string "\.exe$" |
Select-Object -First 1 -ExpandProperty Line

# <meta name="description" content="Cumulative Update Package 16 for SQL Server 2022 - KB5048033"/>
if ($response.Content -match "<meta name=`"description`" content=`"Cumulative Update Package (\d+) for SQL Server $MajorVersion - KB(\d+)`"\s*\/\>") {
$cu = $Matches[1]
$kb = $Matches[2]
} else {
return @{}
}

# Find full version number
if ($response.Content -match "\d+\.\d+\.\d+\.\d+") {
$version = $Matches[0]
} else {
return @{}
}

$v = [Version] $version
$Latest = @{
URL64 = $url
Version = $version
KB = $kb
CU = $cu
Build = $v.Build
}

# Check for a partial page update (has happened in the past) where the download URL doesn't change.
# We can't do this from global:au_BeforeUpdate, because the checksum stuff has already been run (and updated the chocolateyinstall.ps1 script)
$toolsContent = Get-Content .\tools\chocolateyinstall.ps1 -Encoding utf8

$matched = ($toolsContent -match "(^[$]url\s*=\s*)('.*')") | Select-Object -First 1

if ($matched -match "(^[$]url\s*=\s*)'(.*)'") {
$script:previousUrl = $Matches[2]
}
return $Latest
}

function AfterUpdate ($Package, $Latest, $MajorVersion) {

if (($Package.RemoteVersion -ne $Package.NuspecVersion) -and ($script:previousUrl -eq $Latest.URL64)) {
# URL didn't change, game over!
throw "New version $($Package.NuspecVersion) but URL ($($script:previousUrl)) didn't change"
}

$Package.NuspecXml.package.metadata.releaseNotes = "https://support.microsoft.com/help/$($Latest.KB)"
$Package.NuspecXml.package.metadata.title = "Microsoft SQL Server $MajorVersion Cumulative Update $($Latest.CU)"
$Package.SaveNuspec()

VirusTotal_AfterUpdate $Package
}
66 changes: 4 additions & 62 deletions sql-server-2017-cumulative-update/update.ps1
Original file line number Diff line number Diff line change
@@ -1,76 +1,18 @@
Import-Module chocolatey-au

. ..\_scripts\Submit-VirusTotal.ps1
. ..\_scripts\SqlServerCumulativeUpdates.ps1

function global:au_SearchReplace {
@{
# softwareName = 'Hotfix 3026 for Microsoft SQL Server*(KB4229789)*'
'tools\chocolateyInstall.ps1' = @{
"(^[$]url\s*=\s*)('.*')" = "`$1'$($Latest.URL64)'"
"(^[$]checksum\s*=\s*)('.*')" = "`$1'$($Latest.Checksum64)'"
"(^[$]softwareName\s*=\s*)('.*')" = "`$1'Hotfix $($Latest.Build) for SQL Server 2017*(KB$($Latest.KB))*'"
}
}
return SearchReplace 2022 $Latest
}

function global:au_GetLatest {
$downloadId = "56128"
$response = Invoke-WebRequest -Uri "https://www.microsoft.com/en-us/download/confirmation.aspx?id=$downloadId" -ErrorAction Ignore

$url = $response.Content |
Select-String -AllMatches -Pattern "(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?" |
ForEach-Object { $_.Matches.Value } |
Select-string "\.exe$" |
Select-Object -First 1 -ExpandProperty Line

# <meta name="Description" content="Cumulative Update Package 7 for SQL Server 2017 - KB4229789" />
if ($response.Content -match "<meta name=`"Description`" content=`"Cumulative Update Package (\d+) for SQL Server 2017 - KB(\d+)`" \/\>") {
$cu = $Matches[1]
$kb = $Matches[2]
} else {
return @{}
}

$response = Invoke-WebRequest -Uri "https://www.microsoft.com/en-us/download/details.aspx?id=$downloadId"

if ($response.Content -match "\d+\.\d+\.\d+\.\d+") {
$version = $Matches[0]
} else {
return @{}
}

$v = [Version] $version
$Latest = @{
URL64 = $url
Version = $version
KB = $kb
CU = $cu
Build = $v.Build
}

# We can't do this from global:au_BeforeUpdate, because the checksum stuff has already been run (and updated the chocolateyinstall.ps1 script)
$toolsContent = Get-Content .\tools\chocolateyinstall.ps1 -Encoding utf8

$matched = ($toolsContent -match "(^[$]url\s*=\s*)('.*')") | Select-Object -First 1

if ($matched -match "(^[$]url\s*=\s*)'(.*)'") {
$script:previousUrl = $Matches[2]
}
return $Latest
return GetLatest 56128 2017
}

function global:au_AfterUpdate ($Package) {

if (($Package.RemoteVersion -ne $Package.NuspecVersion) -and ($script:previousUrl -eq $Latest.URL64)) {
# URL didn't change, game over!
throw "New version $($Package.NuspecVersion) but URL ($($script:previousUrl)) didn't change"
}

$Package.NuspecXml.package.metadata.releaseNotes = "https://support.microsoft.com/help/$($Latest.KB)"
$Package.NuspecXml.package.metadata.title = "Microsoft SQL Server 2017 Cumulative Update $($Latest.CU)"
$Package.SaveNuspec()

VirusTotal_AfterUpdate $Package
AfterUpdate $Package $Latest 2017
}

update -ChecksumFor 64
66 changes: 4 additions & 62 deletions sql-server-2019-cumulative-update/update.ps1
Original file line number Diff line number Diff line change
@@ -1,76 +1,18 @@
Import-Module chocolatey-au

. ..\_scripts\Submit-VirusTotal.ps1
. ..\_scripts\SqlServerCumulativeUpdates.ps1

function global:au_SearchReplace {
@{
# softwareName = 'Hotfix 3026 for Microsoft SQL Server*(KB4229789)*'
'tools\chocolateyInstall.ps1' = @{
"(^[$]url\s*=\s*)('.*')" = "`$1'$($Latest.URL64)'"
"(^[$]checksum\s*=\s*)('.*')" = "`$1'$($Latest.Checksum64)'"
"(^[$]softwareName\s*=\s*)('.*')" = "`$1'Hotfix $($Latest.Build) for SQL Server 2019*(KB$($Latest.KB))*'"
}
}
return SearchReplace 2022 $Latest
}

function global:au_GetLatest {
$downloadId = "100809"
$response = Invoke-WebRequest -Uri "https://www.microsoft.com/en-us/download/confirmation.aspx?id=$downloadId" -ErrorAction Ignore

$url = $response.Content |
Select-String -AllMatches -Pattern "(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?" |
ForEach-Object { $_.Matches.Value } |
Select-string "\.exe$" |
Select-Object -First 1 -ExpandProperty Line

# <meta name="Description" content="Cumulative Update Package 7 for SQL Server 2017 - KB4229789" />
if ($response.Content -match "<meta name=`"Description`" content=`"Cumulative Update Package (\d+) for SQL Server 2019 - KB(\d+)`" \/\>") {
$cu = $Matches[1]
$kb = $Matches[2]
} else {
return @{}
}

$response = Invoke-WebRequest -Uri "https://www.microsoft.com/en-us/download/details.aspx?id=$downloadId"

if ($response.Content -match "\d+\.\d+\.\d+\.\d+") {
$version = $Matches[0]
} else {
return @{}
}

$v = [Version] $version
$Latest = @{
URL64 = $url
Version = $version
KB = $kb
CU = $cu
Build = $v.Build
}

# We can't do this from global:au_BeforeUpdate, because the checksum stuff has already been run (and updated the chocolateyinstall.ps1 script)
$toolsContent = Get-Content .\tools\chocolateyinstall.ps1 -Encoding utf8

$matched = ($toolsContent -match "(^[$]url\s*=\s*)('.*')") | Select-Object -First 1

if ($matched -match "(^[$]url\s*=\s*)'(.*)'") {
$script:previousUrl = $Matches[2]
}
return $Latest
return GetLatest 100809 2019
}

function global:au_AfterUpdate ($Package) {

if (($Package.RemoteVersion -ne $Package.NuspecVersion) -and ($script:previousUrl -eq $Latest.URL64)) {
# URL didn't change, game over!
throw "New version $($Package.NuspecVersion) but URL ($($script:previousUrl)) didn't change"
}

$Package.NuspecXml.package.metadata.releaseNotes = "https://support.microsoft.com/help/$($Latest.KB)"
$Package.NuspecXml.package.metadata.title = "Microsoft SQL Server 2019 Cumulative Update $($Latest.CU)"
$Package.SaveNuspec()

VirusTotal_AfterUpdate $Package
AfterUpdate $Package $Latest 2019
}

update -ChecksumFor 64
66 changes: 4 additions & 62 deletions sql-server-2022-cumulative-update/update.ps1
Original file line number Diff line number Diff line change
@@ -1,76 +1,18 @@
Import-Module chocolatey-au

. ..\_scripts\Submit-VirusTotal.ps1
. ..\_scripts\SqlServerCumulativeUpdates.ps1

function global:au_SearchReplace {
@{
# softwareName = 'Hotfix 3026 for Microsoft SQL Server*(KB4229789)*'
'tools\chocolateyInstall.ps1' = @{
"(^[$]url\s*=\s*)('.*')" = "`$1'$($Latest.URL64)'"
"(^[$]checksum\s*=\s*)('.*')" = "`$1'$($Latest.Checksum64)'"
"(^[$]softwareName\s*=\s*)('.*')" = "`$1'Hotfix $($Latest.Build) for SQL Server 2022*(KB$($Latest.KB))*'"
}
}
return SearchReplace 2022 $Latest
}

function global:au_GetLatest {
$downloadId = "105013"
$response = Invoke-WebRequest -Uri "https://www.microsoft.com/en-us/download/confirmation.aspx?id=$downloadId" -ErrorAction Ignore

$url = $response.Content |
Select-String -AllMatches -Pattern "(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?" |
ForEach-Object { $_.Matches.Value } |
Select-string "\.exe$" |
Select-Object -First 1 -ExpandProperty Line

# <meta name="Description" content="Cumulative Update Package 7 for SQL Server 2017 - KB4229789" />
if ($response.Content -match "<meta name=`"Description`" content=`"Cumulative Update Package (\d+) for SQL Server 2022 - KB(\d+)`" \/\>") {
$cu = $Matches[1]
$kb = $Matches[2]
} else {
return @{}
}

$response = Invoke-WebRequest -Uri "https://www.microsoft.com/en-us/download/details.aspx?id=$downloadId"

if ($response.Content -match "\d+\.\d+\.\d+\.\d+") {
$version = $Matches[0]
} else {
return @{}
}

$v = [Version] $version
$Latest = @{
URL64 = $url
Version = $version
KB = $kb
CU = $cu
Build = $v.Build
}

# We can't do this from global:au_BeforeUpdate, because the checksum stuff has already been run (and updated the chocolateyinstall.ps1 script)
$toolsContent = Get-Content .\tools\chocolateyinstall.ps1 -Encoding utf8

$matched = ($toolsContent -match "(^[$]url\s*=\s*)('.*')") | Select-Object -First 1

if ($matched -match "(^[$]url\s*=\s*)'(.*)'") {
$script:previousUrl = $Matches[2]
}
return $Latest
return GetLatest 105013 2022
}

function global:au_AfterUpdate ($Package) {

if (($Package.RemoteVersion -ne $Package.NuspecVersion) -and ($script:previousUrl -eq $Latest.URL64)) {
# URL didn't change, game over!
throw "New version $($Package.NuspecVersion) but URL ($($script:previousUrl)) didn't change"
}

$Package.NuspecXml.package.metadata.releaseNotes = "https://support.microsoft.com/help/$($Latest.KB)"
$Package.NuspecXml.package.metadata.title = "Microsoft SQL Server 2022 Cumulative Update $($Latest.CU)"
$Package.SaveNuspec()

VirusTotal_AfterUpdate $Package
AfterUpdate $Package $Latest 2022
}

update -ChecksumFor 64

0 comments on commit a982ec3

Please sign in to comment.