Created
May 7, 2018 08:24
-
-
Save EitanBlumin/a518bd1c2dd87e83722418fba84add99 to your computer and use it in GitHub Desktop.
Check For SQL Server Updates Using SQLServerUpdatesModule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[version] $BuildNumber = "9.00.5324" | |
) | |
#Run this script with -ExecutionPolicy Bypass | |
Install-Module -Name SQLServerUpdatesModule | |
Import-Module SQLServerUpdatesModule | |
$ErrorActionPreference = "SilentlyContinue" | |
$VersionName = switch($BuildNumber.Major) | |
{ | |
{($_ -eq 10)} { switch($BuildNumber.Minor) { {($_ -eq 0)} {"SQL Server 2008"} default { "SQL Server 2008 R2" } } } | |
{($_ -eq 11)} {"SQL Server 2012"} | |
{($_ -eq 12)} {"SQL Server 2014"} | |
{($_ -eq 13)} {"SQL Server 2016"} | |
{($_ -eq 14)} {"SQL Server 2017"} | |
default { "" } | |
} | |
# Current Build Details | |
"<Updates CurrentBuild=""$BuildNumber"">" | |
if ($VersionName -eq "") | |
{ | |
"<Message>Version $BuildNumber is unrecognized. Your SQL Server version may be unsupported and should be upgraded.</Message>" | |
} | |
else | |
{ | |
$things = Get-SQLServerUpdates -Version $VersionName | |
if ($things -ne "") | |
{ | |
if ($things -eq $null) | |
{ | |
"<Message>Build Number was not found. Probably means you have a very old SQL Server version which is unsupported. Please upgrade!</Message>" | |
} | |
else | |
{ | |
# Latest Cumulative Update | |
$LastCU = ($things | Where-Object {!($_.CumulativeUpdate -eq $null -or $_.CumulativeUpdate -eq "")}) | |
if ($LastCU -ne $null) | |
{ | |
$LastCU = $LastCU[0] | |
"<LatestCU Build=""" + $LastCU.Build + """ ReleaseDate=""" + $LastCU.ReleaseDate + """>" + $LastCU.CumulativeUpdate + "</LatestCU>" | |
} | |
# Latest Service Pack | |
$LastSP = ($things | Where-Object {!($_.ServicePack -eq $null -or $_.ServicePack -eq "")}) | |
if ($LastSP -ne $null) | |
{ | |
$LastSP = $LastSP[0] | |
"<LatestSP Build=""" + $LastSP.Build + """ ReleaseDate=""" + $LastSP.ReleaseDate + """>" + $LastSP.ServicePack + "</LatestSP>" | |
} | |
if (($LastCU.ReleaseDate -lt $LastSP.ReleaseDate -and $BuildNumber -eq $LastSP.Build) -or ($LastCU.ReleaseDate -gt $LastSP.ReleaseDate -and $BuildNumber -eq $LastCU.Build)) | |
{ | |
"<Message>Your SQL Server instance is up to date.</Message>" | |
} | |
else | |
{ | |
"<Message>Your SQL Server instance needs to be updated.</Message>" | |
} | |
} | |
} | |
} | |
"</Updates>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment