From 34493ea3abdea4d0718b6bd6ec0d55c8f29e8168 Mon Sep 17 00:00:00 2001 From: Dor Breger Date: Thu, 12 Dec 2024 22:36:32 +0200 Subject: [PATCH 01/11] Added a command to get the version of an sql server instance --- source/Public/Get-SqlDscServerVersion.ps1 | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 source/Public/Get-SqlDscServerVersion.ps1 diff --git a/source/Public/Get-SqlDscServerVersion.ps1 b/source/Public/Get-SqlDscServerVersion.ps1 new file mode 100644 index 0000000000..70cc989808 --- /dev/null +++ b/source/Public/Get-SqlDscServerVersion.ps1 @@ -0,0 +1,27 @@ +<# + .SYNOPSIS + Get server version. + .DESCRIPTION + This command gets the version from a SQL Server Database Engine instance. + .PARAMETER ServerObject + Specifies current server connection object. + .EXAMPLE + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' + $serverObject | Get-SqlDscServerVersion + Get the version of the SQL Server instance. + .OUTPUTS + `[Microsoft.SqlServer.Management.Common.ServerVersion]` + + #> + +function Get-SqlDscServerVersion +{ + [OutputType([Microsoft.SqlServer.Management.Common.ServerVersion])] + [CmdletBinding()] + param ( + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [Microsoft.SqlServer.Management.Smo.Server] + $ServerObject + ) + return $ServerObject.ServerVersion +} From 4d161ea3b8738881c3e8d3e84807f79524169c20 Mon Sep 17 00:00:00 2001 From: Dor Breger Date: Thu, 12 Dec 2024 22:36:57 +0200 Subject: [PATCH 02/11] Added a command to get the minor version of a file in a path. --- .../SqlServerDsc.Common/SqlServerDsc.Common.psm1 | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 index 12b01ce988..13d57fbe0f 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1 @@ -2270,6 +2270,19 @@ function Get-FilePathMajorVersion (Get-Item -Path $Path).VersionInfo.ProductVersion.Split('.')[0] } +function Get-FilePathMinorVersion +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [System.String] + $Path + ) + + (Get-Item -Path $Path).VersionInfo.ProductVersion.Split('.')[2] +} + <# .SYNOPSIS Test if the specific feature flag should be enabled. From 5b61d7778ef8785adc4a3aba607890a8c07f6fa4 Mon Sep 17 00:00:00 2001 From: Dor Breger Date: Fri, 13 Dec 2024 22:49:39 +0200 Subject: [PATCH 03/11] Export Get-FilePathMinorVersion --- source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 | 1 + 1 file changed, 1 insertion(+) diff --git a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 index e73d52d1b3..8ec1b4b3fb 100644 --- a/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 +++ b/source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psd1 @@ -52,6 +52,7 @@ 'Import-Assembly' 'ConvertTo-ServerInstanceName' 'Get-FilePathMajorVersion' + 'Get-FilePathMinorVersion' 'Test-FeatureFlag' ) From 14f3ff2253a7eb50eb037fbe18a95948f649dab2 Mon Sep 17 00:00:00 2001 From: Dor Breger Date: Wed, 18 Dec 2024 22:43:45 +0200 Subject: [PATCH 04/11] (feat) added commands to find CUs in folder and patch the instance --- source/Public/Find-SqlDscLatestCu.ps1 | 37 ++++++++++++++++++++ source/Public/Update-SqlDscServer.ps1 | 49 +++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 source/Public/Find-SqlDscLatestCu.ps1 create mode 100644 source/Public/Update-SqlDscServer.ps1 diff --git a/source/Public/Find-SqlDscLatestCu.ps1 b/source/Public/Find-SqlDscLatestCu.ps1 new file mode 100644 index 0000000000..1e849770f1 --- /dev/null +++ b/source/Public/Find-SqlDscLatestCu.ps1 @@ -0,0 +1,37 @@ +<# + .SYNOPSIS + Find the latest Cumulative Update for SQL Server In folder + .DESCRIPTION + Find the latest Cumulative Update for SQL Server In folder + This function will find the latest Cumulative Update for SQL Server in the supplied path. + .PARAMETER MediaPath + Specifies the path to look CU files. + .OUTPUTS + System.String +#> + +function Find-SqlDscLatestCu { + + param ( + [Parameter(Mandatory = $true)] + [System.String] + $MediaPath, + [Parameter(Mandatory = $true)] + [System.String] + $MajorVersion + ) + $highestMinorVersion = -1 + $selectedExe = $null + $exeFiles = Get-ChildItem -Path $MediaPath -Filter *.exe + foreach ($exeFile in $exeFiles) { + $fileMajorVersion = Get-FilePathMajorVersion -Path $exeFile.FullName + if ($fileMajorVersion -eq $MajorVersion) { + $fileMinorVersion = Get-FilePathMinorVersion -Path $exeFile.FullName + if ($fileMinorVersion -gt $highestMinorVersion) { + $highestMinorVersion = $fileMinorVersion + $selectedExe = $exeFile.FullName + } + } + } + return $selectedExe +} diff --git a/source/Public/Update-SqlDscServer.ps1 b/source/Public/Update-SqlDscServer.ps1 new file mode 100644 index 0000000000..5f57cc2d94 --- /dev/null +++ b/source/Public/Update-SqlDscServer.ps1 @@ -0,0 +1,49 @@ +<# + .SYNOPSIS + Perform minor version updates to the SQL Server instance. + .DESCRIPTION + Perform minor version updates to the SQL Server instance. + This function will update the SQL Server instance to the latest minor version + available in the supplied path. + .PARAMETER MediaPath + Specifies the path to look CU or SP files. + .OUTPUTS + None. +#> +function Update-SqlDscServer +{ + param + ( + [Parameter(Mandatory = $true)] + [System.String] + $MediaPath, + [Parameter(ValueFromPipeline = $true, Mandatory = $true)] + [Microsoft.SqlServer.Management.Smo.Server] + $ServerObject + ) + $sqlMajorVersion = $ServerObject | Get-SqlDscServerVersion | Select-Object -ExpandProperty Major + $sqlMinorVersion = $ServerObject | Get-SqlDscServerVersion | Select-Object -ExpandProperty BuildNumber + $selectedExe = Find-SqlDscLatestCu -MediaPath $MediaPath -MajorVersion $sqlMajorVersion + + if (-not $selectedExe) { + Write-Error -Message "No update found for SQL Server version $sqlMajorVersion In the folder" + throw "Could not determine the update file to use" + } + + $exeMinorVersion = Get-FilePathMinorVersion -Path $selectedExe + if ($exeMinorVersion -le $sqlMinorVersion) { + return + } + + $patchSplat = @( + "/Action=Patch" + "/Quiet" + "/IAcceptSQLServerLicenseTerms" + "/InstanceId=$(($ServerObject.ServiceInstanceId -split '\.')[1])" + ) + $process = Start-Process -FilePath $selectedExe -ArgumentList $patchSplat -NoNewWindow -Wait -PassThru + if ($process.ExitCode -ne 0) { + Write-Error "The executable encountered an error." + throw "$($selectedExe) returned an error code of $($process.ExitCode) with message: $($process.StandardOutput)" + } +} From 7da6bcf32f66133299bc315393d705190015dc70 Mon Sep 17 00:00:00 2001 From: Dor Breger Date: Fri, 20 Dec 2024 16:54:38 +0200 Subject: [PATCH 05/11] Enable installtion of minor version upgrades in SqlSEtup --- .../DSC_SqlSetup/DSC_SqlSetup.psm1 | 172 +++++++++++------- .../DSC_SqlSetup/DSC_SqlSetup.schema.mof | 1 + 2 files changed, 106 insertions(+), 67 deletions(-) diff --git a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 index 24056b34f2..20109bcd09 100644 --- a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 +++ b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 @@ -176,6 +176,7 @@ function Get-TargetResource UseEnglish = $UseEnglish ServerName = $ServerName SqlVersion = $null + SqlMinorVersion = $null ProductCoveredBySA = $null } @@ -224,6 +225,13 @@ function Get-TargetResource $getTargetResourceReturnValue.SqlVersion = $SqlVersion + if ($Action -eq 'Upgrade') { + $sqlMinorVersion = (Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' ).ServerVersion.BuildNumber + $getTargetResourceReturnValue.SqlMinorVersion = $sqlMinorVersion + } else { + $getTargetResourceReturnValue.SqlMinorVersion = 0 + } + if ($SourceCredential) { Disconnect-UncPath -RemotePath $SourcePath @@ -1660,93 +1668,108 @@ function Set-TargetResource Write-Verbose -Message ($script:localizedData.UsingPath -f $pathToSetupExecutable) - try + $installerSqlVersion = Get-FilePathMajorVersion -Path (Join-Path -Path $sourcePath -ChildPath 'setup.exe') + $instanceSqlVersion = Get-SQLInstanceMajorVersion -InstanceName $InstanceName + if ($installerSqlVersion -eq $instanceSqlVersion -and $Action -eq 'Upgrade') { - <# - This handles when PsDscRunAsCredential is set, or running as the SYSTEM account (when - PsDscRunAsCredential is not set). - #> - - $startProcessParameters = @{ - FilePath = $pathToSetupExecutable - ArgumentList = $arguments - Timeout = $SetupProcessTimeout + try { + Connect-SQL -InstanceName $InstanceName | Update-SqlDscServer -MediaPath $UpdateSource + } catch + { + throw $_ } - $processExitCode = Start-SqlSetupProcess @startProcessParameters - - $setupExitMessage = ($script:localizedData.SetupExitMessage -f $processExitCode) - - $setupEndedInError = $false + } else { - if ($processExitCode -eq 3010 -and -not $SuppressReboot) + try { - $setupExitMessageRebootRequired = ('{0} {1}' -f $setupExitMessage, ($script:localizedData.SetupSuccessfulRebootRequired)) + <# + This handles when PsDscRunAsCredential is set, or running as the SYSTEM account (when + PsDscRunAsCredential is not set). + #> - Write-Verbose -Message $setupExitMessageRebootRequired + $startProcessParameters = @{ + FilePath = $pathToSetupExecutable + ArgumentList = $arguments + Timeout = $SetupProcessTimeout + } - # Setup ended with error code 3010 which means reboot is required. - $global:DSCMachineStatus = 1 - } - elseif ($processExitCode -ne 0) - { - $setupExitMessageError = ('{0} {1}' -f $setupExitMessage, ($script:localizedData.SetupFailed)) - Write-Warning $setupExitMessageError + $processExitCode = Start-SqlSetupProcess @startProcessParameters - $setupEndedInError = $true - } - else - { - $setupExitMessageSuccessful = ('{0} {1}' -f $setupExitMessage, ($script:localizedData.SetupSuccessful)) + $setupExitMessage = ($script:localizedData.SetupExitMessage -f $processExitCode) - Write-Verbose -Message $setupExitMessageSuccessful - } + $setupEndedInError = $false - if ($ForceReboot -or (Test-PendingRestart)) - { - if (-not ($SuppressReboot)) + if ($processExitCode -eq 3010 -and -not $SuppressReboot) { - Write-Verbose -Message $script:localizedData.Reboot + $setupExitMessageRebootRequired = ('{0} {1}' -f $setupExitMessage, ($script:localizedData.SetupSuccessfulRebootRequired)) - # Rebooting, so no point in refreshing the session. - $forceReloadPowerShellModule = $false + Write-Verbose -Message $setupExitMessageRebootRequired + # Setup ended with error code 3010 which means reboot is required. $global:DSCMachineStatus = 1 } + elseif ($processExitCode -ne 0) + { + $setupExitMessageError = ('{0} {1}' -f $setupExitMessage, ($script:localizedData.SetupFailed)) + Write-Warning $setupExitMessageError + + $setupEndedInError = $true + } + else + { + $setupExitMessageSuccessful = ('{0} {1}' -f $setupExitMessage, ($script:localizedData.SetupSuccessful)) + + Write-Verbose -Message $setupExitMessageSuccessful + } + + if ($ForceReboot -or (Test-PendingRestart)) + { + if (-not ($SuppressReboot)) + { + Write-Verbose -Message $script:localizedData.Reboot + + # Rebooting, so no point in refreshing the session. + $forceReloadPowerShellModule = $false + + $global:DSCMachineStatus = 1 + } + else + { + Write-Verbose -Message $script:localizedData.SuppressReboot + $forceReloadPowerShellModule = $true + } + } else { - Write-Verbose -Message $script:localizedData.SuppressReboot $forceReloadPowerShellModule = $true } - } - else - { - $forceReloadPowerShellModule = $true - } - if ((-not $setupEndedInError) -and $forceReloadPowerShellModule) - { - <# - Force reload of SQLPS module in case a newer version of - SQL Server was installed that contains a newer version - of the SQLPS module, although if SqlServer module exist - on the target node, that will be used regardless. - This is to make sure we use the latest SQLPS module that - matches the latest assemblies in GAC, mitigating for example - issue #1151. - #> - Import-SqlDscPreferredModule -Force - } + if ((-not $setupEndedInError) -and $forceReloadPowerShellModule) + { + <# + Force reload of SQLPS module in case a newer version of + SQL Server was installed that contains a newer version + of the SQLPS module, although if SqlServer module exist + on the target node, that will be used regardless. + This is to make sure we use the latest SQLPS module that + matches the latest assemblies in GAC, mitigating for example + issue #1151. + #> + Import-SqlDscPreferredModule -Force + } - if (-not (Test-TargetResource @PSBoundParameters)) + if (-not (Test-TargetResource @PSBoundParameters)) + { + $errorMessage = $script:localizedData.TestFailedAfterSet + New-InvalidResultException -Message $errorMessage + } + } + catch { - $errorMessage = $script:localizedData.TestFailedAfterSet - New-InvalidResultException -Message $errorMessage + throw $_ } - } - catch - { - throw $_ + } } @@ -1799,7 +1822,7 @@ function Set-TargetResource False, or omitting the parameter, indicates it's covered under a SQL Server license. Default value is False. - Not used in Test-TargetResource. + Not used in Test-TargetResource. .PARAMETER UpdateEnabled Enabled updates during installation. @@ -2267,7 +2290,11 @@ function Test-TargetResource [Parameter()] [System.String] - $SqlVersion + $SqlVersion, + + [Parameter()] + [System.String] + $sqlMinorVersion ) if ($Action -eq 'Upgrade' -and $PSBoundParameters.ContainsKey('SqlVersion')) @@ -2360,7 +2387,6 @@ function Test-TargetResource { $installerSqlVersion = Get-FilePathMajorVersion -Path (Join-Path -Path $sourcePath -ChildPath 'setup.exe') $instanceSqlVersion = Get-SQLInstanceMajorVersion -InstanceName $InstanceName - if ($installerSQLVersion -gt $instanceSqlVersion) { Write-Verbose -Message ( @@ -2368,6 +2394,16 @@ function Test-TargetResource ) $result = $false + } elseif ($instanceSqlVersion -eq $installerSqlVersion){ + $latestCU = Find-SqlDscLatestCu -MediaPath $UpdateSource -MajorVersion $installerSqlVersion + $updateSourceLatestMinor = Get-FilePathMinorVersion -Path $latestCU + if ($updateSourceLatestMinor -gt $sqlMinorVersion) + { + Write-Verbose -Message ( + $script:localizedData.DifferentMinorVersion -f $InstanceName, $sqlMinorVersion, $updateSourceLatestMinor + ) + $result = $false + } } } @@ -2717,6 +2753,7 @@ function Get-SqlEngineProperties $sqlUserDatabaseDirectory = $sqlServerObject.DefaultFile $sqlUserDatabaseLogDirectory = $sqlServerObject.DefaultLog $sqlBackupDirectory = $sqlServerObject.BackupDirectory + $sqlMinorVersion = $sqlServerObject.ServerVersion.BuildNumber if ($sqlServerObject.LoginMode -eq 'Mixed') { @@ -2739,6 +2776,7 @@ function Get-SqlEngineProperties SQLUserDBLogDir = $sqlUserDatabaseLogDirectory SQLBackupDir = $sqlBackupDirectory SecurityMode = $securityMode + MinorVersion = $sqlMinorVersion } } diff --git a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.schema.mof b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.schema.mof index cd96c84940..952c3de66b 100644 --- a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.schema.mof +++ b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.schema.mof @@ -49,6 +49,7 @@ class DSC_SqlSetup : OMI_BaseResource [Write, Description("The server mode for _SQL Server Analysis Services_ instance. The default is to install in Multidimensional mode. Valid values in a cluster scenario are `'MULTIDIMENSIONAL'` or `'TABULAR'`. Parameter **ASServerMode** is case-sensitive. **All values must be expressed in upper case**."), ValueMap{"MULTIDIMENSIONAL", "TABULAR", "POWERPIVOT"}, Values{"MULTIDIMENSIONAL", "TABULAR", "POWERPIVOT"}] String ASServerMode; [Write, EmbeddedInstance("MSFT_Credential"), Description("Service account for _Integration Services_'s _Windows_ service.")] String ISSvcAccount; [Read, Description("Returns the username for the _Integration Services_'s _Windows_ service.")] String ISSvcAccountUsername; + [Read, Description("The minor version of the Sql server. for example in sql server 2022 cu2 iw will be 4015")] String SqlMinorVersion; [Write, Description("Specifies the startup mode for the _SQL Server Database Engine_'s _Windows_ service."), ValueMap{"Automatic", "Disabled", "Manual"}, Values{"Automatic", "Disabled", "Manual"}] String SqlSvcStartupType; [Write, Description("Specifies the startup mode for the _SQL Server Agent_'s _Windows_ service."), ValueMap{"Automatic", "Disabled", "Manual"}, Values{"Automatic", "Disabled", "Manual"}] String AgtSvcStartupType; [Write, Description("Specifies the startup mode for the _SQL Server Integration Services_'s _Windows_ service."), ValueMap{"Automatic", "Disabled", "Manual"}, Values{"Automatic", "Disabled", "Manual"}] String IsSvcStartupType; From 493cc9ed98d355d0a3a5c6d78de3a77e3ac945e2 Mon Sep 17 00:00:00 2001 From: Dor Breger Date: Fri, 20 Dec 2024 16:58:42 +0200 Subject: [PATCH 06/11] Updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dfe4952b5..e49c7789da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - SqlSetup - Fixed issue with AddNode where cluster IP information was not being passed to setup.exe ([issue #1171](https://github.com/dsccommunity/SqlServerDsc/issues/1171)). + - Allow installtion of minor version upgrades. ([Issue #2053](https://github.com/dsccommunity/SqlServerDsc/issues/2053)) ### Fixed From d9690325861bffafc2a6b8b3cbea4c5a5292034e Mon Sep 17 00:00:00 2001 From: Dor Breger Date: Fri, 20 Dec 2024 17:01:47 +0200 Subject: [PATCH 07/11] DSC_SqlSetup.psm1 fixed some formatting --- .../DSC_SqlSetup/DSC_SqlSetup.psm1 | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 index 20109bcd09..cf61c2b597 100644 --- a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 +++ b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 @@ -225,10 +225,13 @@ function Get-TargetResource $getTargetResourceReturnValue.SqlVersion = $SqlVersion - if ($Action -eq 'Upgrade') { + if ($Action -eq 'Upgrade') + { $sqlMinorVersion = (Connect-SQL -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop' ).ServerVersion.BuildNumber $getTargetResourceReturnValue.SqlMinorVersion = $sqlMinorVersion - } else { + } + else + { $getTargetResourceReturnValue.SqlMinorVersion = 0 } @@ -333,7 +336,7 @@ function Get-TargetResource $getRegistryPropertyParams = @{ Path = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL$($SqlVersion).$($InstanceName)\Setup" Name = 'IsProductCoveredBySA' - } + } $getTargetResourceReturnValue.ProductCoveredBySA = Get-RegistryPropertyValue @getRegistryPropertyParams } @@ -1672,14 +1675,18 @@ function Set-TargetResource $instanceSqlVersion = Get-SQLInstanceMajorVersion -InstanceName $InstanceName if ($installerSqlVersion -eq $instanceSqlVersion -and $Action -eq 'Upgrade') { - try { + try + { Connect-SQL -InstanceName $InstanceName | Update-SqlDscServer -MediaPath $UpdateSource - } catch + } + catch { throw $_ } - } else { + } + else + { try { @@ -2394,7 +2401,9 @@ function Test-TargetResource ) $result = $false - } elseif ($instanceSqlVersion -eq $installerSqlVersion){ + } + elseif ($instanceSqlVersion -eq $installerSqlVersion) + { $latestCU = Find-SqlDscLatestCu -MediaPath $UpdateSource -MajorVersion $installerSqlVersion $updateSourceLatestMinor = Get-FilePathMinorVersion -Path $latestCU if ($updateSourceLatestMinor -gt $sqlMinorVersion) From 58040b92260dd05dae020cdf5d83e376bba937c1 Mon Sep 17 00:00:00 2001 From: Dor Breger Date: Sat, 21 Dec 2024 18:24:47 +0200 Subject: [PATCH 08/11] Fixed null minor version --- source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 | 9 ++++----- .../DSC_SqlSetup/en-US/DSC_SqlSetup.strings.psd1 | 1 + 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 index cf61c2b597..65816a2816 100644 --- a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 +++ b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 @@ -1782,7 +1782,7 @@ function Set-TargetResource <# .SYNOPSIS - Tests if the SQL Server features are installed on the node. + Tests if the SQL Server Features are installed on the node. .PARAMETER Action The action to be performed. Default value is 'Install'. @@ -2301,7 +2301,7 @@ function Test-TargetResource [Parameter()] [System.String] - $sqlMinorVersion + $SqlMinorVersion ) if ($Action -eq 'Upgrade' -and $PSBoundParameters.ContainsKey('SqlVersion')) @@ -2406,11 +2406,10 @@ function Test-TargetResource { $latestCU = Find-SqlDscLatestCu -MediaPath $UpdateSource -MajorVersion $installerSqlVersion $updateSourceLatestMinor = Get-FilePathMinorVersion -Path $latestCU - if ($updateSourceLatestMinor -gt $sqlMinorVersion) + if ($updateSourceLatestMinor -gt $getTargetResourceResult.SqlMinorVersion) { Write-Verbose -Message ( - $script:localizedData.DifferentMinorVersion -f $InstanceName, $sqlMinorVersion, $updateSourceLatestMinor - ) + $script:localizedData.DifferentMinorVersion -f $InstanceName, $getTargetResourceResult.SqlMinorVersion, $updateSourceLatestMinor) $result = $false } } diff --git a/source/DSCResources/DSC_SqlSetup/en-US/DSC_SqlSetup.strings.psd1 b/source/DSCResources/DSC_SqlSetup/en-US/DSC_SqlSetup.strings.psd1 index 84d3ae0ef6..7ba65d0fa0 100644 --- a/source/DSCResources/DSC_SqlSetup/en-US/DSC_SqlSetup.strings.psd1 +++ b/source/DSCResources/DSC_SqlSetup/en-US/DSC_SqlSetup.strings.psd1 @@ -64,5 +64,6 @@ ConvertFrom-StringData @' FeatureAlreadyInstalled = The feature '{0}' is already installed so it will not be installed again. FeatureFlag = Using feature flag '{0}' DifferentMajorVersion = The instance '{0}' has the wrong major version. The major version is '{1}', but expected version '{2}'. + DifferentMinorVersion = The instance '{0} has the wrong minor version. The minor version is '{1}', but expected version '{2}'. ParameterSqlVersionNotAllowedForSetupActionUpgrade = The parameter SqlVersion is not allowed to be specified when using the setup action Upgrade. '@ From 17e5108f54d755abc588048d0958227490b37e4c Mon Sep 17 00:00:00 2001 From: Dor Breger Date: Sat, 21 Dec 2024 18:51:35 +0200 Subject: [PATCH 09/11] fixed formatting in update-sqldscserver --- source/Public/Update-SqlDscServer.ps1 | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/source/Public/Update-SqlDscServer.ps1 b/source/Public/Update-SqlDscServer.ps1 index 5f57cc2d94..5a178604bd 100644 --- a/source/Public/Update-SqlDscServer.ps1 +++ b/source/Public/Update-SqlDscServer.ps1 @@ -25,25 +25,28 @@ function Update-SqlDscServer $sqlMinorVersion = $ServerObject | Get-SqlDscServerVersion | Select-Object -ExpandProperty BuildNumber $selectedExe = Find-SqlDscLatestCu -MediaPath $MediaPath -MajorVersion $sqlMajorVersion - if (-not $selectedExe) { + if (-not $selectedExe) + { Write-Error -Message "No update found for SQL Server version $sqlMajorVersion In the folder" - throw "Could not determine the update file to use" + throw 'Could not determine the update file to use' } $exeMinorVersion = Get-FilePathMinorVersion -Path $selectedExe - if ($exeMinorVersion -le $sqlMinorVersion) { + if ($exeMinorVersion -le $sqlMinorVersion) + { return } $patchSplat = @( - "/Action=Patch" - "/Quiet" - "/IAcceptSQLServerLicenseTerms" + '/Action=Patch' + '/Quiet' + '/IAcceptSQLServerLicenseTerms' "/InstanceId=$(($ServerObject.ServiceInstanceId -split '\.')[1])" ) $process = Start-Process -FilePath $selectedExe -ArgumentList $patchSplat -NoNewWindow -Wait -PassThru - if ($process.ExitCode -ne 0) { - Write-Error "The executable encountered an error." + if ($process.ExitCode -ne 0) + { + Write-Error 'The executable encountered an error.' throw "$($selectedExe) returned an error code of $($process.ExitCode) with message: $($process.StandardOutput)" } } From 5db55766968b6a1dc717866295479103511ffba4 Mon Sep 17 00:00:00 2001 From: Dor Breger Date: Sat, 21 Dec 2024 23:07:14 +0200 Subject: [PATCH 10/11] sqlSetup.psm1 fix Test-TargetResource gettting different parametrs --- source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 index 65816a2816..0130d177d3 100644 --- a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 +++ b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 @@ -2297,11 +2297,8 @@ function Test-TargetResource [Parameter()] [System.String] - $SqlVersion, + $SqlVersion - [Parameter()] - [System.String] - $SqlMinorVersion ) if ($Action -eq 'Upgrade' -and $PSBoundParameters.ContainsKey('SqlVersion')) From 7ab71b2019fec3cdf3dc7c69735bf257fca2d28a Mon Sep 17 00:00:00 2001 From: Dor Breger Date: Sat, 21 Dec 2024 23:07:51 +0200 Subject: [PATCH 11/11] fix ScriptAnalyzer compliance --- source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 | 2 -- source/Public/Get-SqlDscServerVersion.ps1 | 6 +++--- source/Public/Update-SqlDscServer.ps1 | 12 +++++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 index 0130d177d3..5584cc190f 100644 --- a/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 +++ b/source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1 @@ -1683,7 +1683,6 @@ function Set-TargetResource { throw $_ } - } else { @@ -1776,7 +1775,6 @@ function Set-TargetResource { throw $_ } - } } diff --git a/source/Public/Get-SqlDscServerVersion.ps1 b/source/Public/Get-SqlDscServerVersion.ps1 index 70cc989808..6466cd371e 100644 --- a/source/Public/Get-SqlDscServerVersion.ps1 +++ b/source/Public/Get-SqlDscServerVersion.ps1 @@ -7,18 +7,18 @@ Specifies current server connection object. .EXAMPLE $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' - $serverObject | Get-SqlDscServerVersion + Get-SqlDscServerVersiono -ServerObject $serverObject Get the version of the SQL Server instance. .OUTPUTS `[Microsoft.SqlServer.Management.Common.ServerVersion]` - #> function Get-SqlDscServerVersion { [OutputType([Microsoft.SqlServer.Management.Common.ServerVersion])] [CmdletBinding()] - param ( + param + ( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [Microsoft.SqlServer.Management.Smo.Server] $ServerObject diff --git a/source/Public/Update-SqlDscServer.ps1 b/source/Public/Update-SqlDscServer.ps1 index 5a178604bd..c710d07b90 100644 --- a/source/Public/Update-SqlDscServer.ps1 +++ b/source/Public/Update-SqlDscServer.ps1 @@ -25,13 +25,16 @@ function Update-SqlDscServer $sqlMinorVersion = $ServerObject | Get-SqlDscServerVersion | Select-Object -ExpandProperty BuildNumber $selectedExe = Find-SqlDscLatestCu -MediaPath $MediaPath -MajorVersion $sqlMajorVersion - if (-not $selectedExe) + try { - Write-Error -Message "No update found for SQL Server version $sqlMajorVersion In the folder" - throw 'Could not determine the update file to use' + $exeMinorVersion = Get-FilePathMinorVersion -Path $selectedExe + } + catch + { + Write-Error 'The executable could not be found.' + New-InvalidOperationException -Message 'The execuatble could not be found' -ErrorRecord $_ } - $exeMinorVersion = Get-FilePathMinorVersion -Path $selectedExe if ($exeMinorVersion -le $sqlMinorVersion) { return @@ -47,6 +50,5 @@ function Update-SqlDscServer if ($process.ExitCode -ne 0) { Write-Error 'The executable encountered an error.' - throw "$($selectedExe) returned an error code of $($process.ExitCode) with message: $($process.StandardOutput)" } }