Installing SQL Server cumulative updates is always a cumbersome, and quite daunting task. Unpacking and running the CU installer is a slow process: you have to click through a lot of screens, and it’s hard to get it to install silently. But here is how!
Imagine you’re installing Cumulative Update Package 13 for SQL Server 2019, or KB5005679. What you need to know is the file you downloaded (SQLServer2019-KB5005679-x64.exe
) is a wrapper around the update’s setup.exe
program. So any setup parameter passed on the command-line shell, is not passed on to setup.exe!
The first step is to unpack the file into a temporary location from where you run the setup.
The following command unpacks SQLServer2019-KB5005679-x64.exe into the location Z:\sqlserver_cu\KB5005679
:
.\SQLServer2019-KB5005679-x64.exe /X:Z:\sqlserver_cu\KB5005679
In the KB5005679 folder you’ll find the SQL Server cumulative update file setup.exe
you so desperately need to install this update silently. Now only for the command line arguments…
Manually failover all databases in an SQL Server Database Mirroring configuration
All relevant command line arguments are in Microsofts online doc Installing Updates from the Command Prompt. For me, the minimal set I go with are:
/action=patch
/instancename=MSSQLSERVER
/quiet
/IAcceptSQLServerLicenseTerms
The /quiet switch runs the update in unattended mode, thus making the command to install SQL Server cumulative updates silent and unattended:
# silent and unattended install of a service pack to a specific SQL Server instance
Z:\sqlserver_cu\KB5005679\setup.exe /action=patch /instancename=MSSQLSERVER /quiet /IAcceptSQLServerLicenseTerms
Install Windows Server Servicing Stack Updates (SSU) using PowerShell
In your deployment scenario, you can add some logic and checks to apply the update only if its ProductVersion property is higher than currently installed. You’ll need Invoke-SqlCmd
for this too:
$sqlserver_version = New-Object System.Version $(Invoke-SqlCmd -Query "SELECT SERVERPROPERTY('ProductVersion') AS BuildNumber").BuildNumber
if($sqlserver_version -ne (New-Object System.Version $(Get-ItemProperty "Z:\sqlserver_cu\KB5005679\setup.exe").VersionInfo.ProductVersion)) {
Z:\sqlserver_cu\KB5005679\setup.exe /action=patch /instancename=MSSQLSERVER /quiet /IAcceptSQLServerLicenseTerms
}
Download cumulative updates in the background using PowerShell
Bonus! Use PowerShell to download cumulatieve updates in the background. This may speed up installing the cumulative updates.
Prerequisites:
- Look the latest CU from Microsoft SQL Server® 2022 for Microsoft® Windows Latest Cumulative Update web page
- When you hover the “Download” button, get the GUID from the download URL
- Add the KB number and GUID in the script below. Unfortunately there is no way to get the GUID and download link in a more unattended way 🙁
$kb = "KB5059390"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ProgressPreference = 'SilentlyContinue'
$url = "https://download.microsoft.com/download/a89001cb-9c99-48d3-9f14-ded054b35fe4/SQLServer2022-${kb}-x64.exe"
$result = Invoke-WebRequest -Method GET -Uri $url -Headers $headers -UseBasicParsing
$tempFolder = $env:temp
# get filename by Content-Disposition header
$contentDisposition = $result.Headers.'Content-Disposition'
$fileName = $contentDisposition.Split('=')[1].Split(';')[0]
$path = Join-Path $tempFolder $fileName
$file = [System.IO.FileStream]::new($path, [System.IO.FileMode]::Create)
$file.write($result.Content, 0, $result.RawContentLength)
$file.close()
# GC, clean up memory
Remove-Variable result
$media_path = $file.name
$params = @("/X:.\${kb}")
Start-Process -FilePath $media_path -ArgumentList $params -Wait
This PowerShell script uses Invoke-Webrequest to download the a file from https://download.microsoft.com. Using its Content-Disposition HTTP header, the file is automatically renamed and placed in $env:temp
. To clean up PowerShell memory usage, the variable result is removed next.
Using Start-Process, the CU container package is unpacked into a newly created folder named after the KB name: .\${kb}
, in the current directory where you ran the script.
As you can see, this code is very similar to my Update SQL Server Management Studio (SSMS) automatically code. Sweet! 🙂
Microsoft SQL Server Versions List
An unofficial build chart lists all of the known Service Packs (SP), Cumulative Updates (CU), patches, hotfixes and other builds of MS SQL Server. You can find it on https://sqlserverbuilds.blogspot.com/. Microsoft maintains “Latest updates and version history for SQL Server” on https://learn.microsoft.com/en-us/troubleshoot/sql/releases/download-and-install-latest-updates. And furthermore you can download the most recent CU from https://www.microsoft.com/en-us/download/details.aspx?id=100809.





Did you like this post?
Please take a second to support Sysadmins of the North and donate!
Your generosity helps pay for the ongoing costs associated with running this website like coffee, hosting services, library mirrors, domain renewals, time for article research, and coffee, just to name a few.