Skip to content

Commit

Permalink
SqlSetup: Add parameter SqlVersion (#1962)
Browse files Browse the repository at this point in the history
- SqlSetup
  - Added the parameter `SqlVersion` that can be used to set the SQL Server
    version to be installed instead of it looking for version in the setup
    executable of the SQL Server media. This parameter is not allowed for
    the setup action `Upgrade`, if specified it will throw an exception
    (issue #1946).
  • Loading branch information
johlju authored Aug 26, 2023
1 parent 8047f7c commit 472ef04
Show file tree
Hide file tree
Showing 6 changed files with 1,073 additions and 886 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- SqlServerDsc
- Updated pipeline files to support ModuleFast.
- SqlSetup
- Added the parameter `SqlVersion` that can be used to set the SQL Server
version to be installed instead of it looking for version in the setup
executable of the SQL Server media. This parameter is not allowed for
the setup action `Upgrade`, if specified it will throw an exception
([issue #1946](https://github.com/dsccommunity/SqlServerDsc/issues/1946)).

### Changed

Expand Down
126 changes: 105 additions & 21 deletions source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
the host name for the listener or cluster group. If using a secure connection
the specified value should be the same name that is used in the certificate.
Default value is the current computer name.
.PARAMETER SqlVersion
Specifies the SQL Server version that should be installed. Only the major
version will be used, but the provided value must be set to at least major
and minor version (e.g. `14.0`). When providing this parameter the media
will not be used to evaluate version. Although, if the setup action is
`Upgrade` then setting this parameter will throw an exception as the version
from the install media is required.
#>
function Get-TargetResource
{
Expand Down Expand Up @@ -96,9 +104,20 @@ function Get-TargetResource

[Parameter()]
[System.String]
$ServerName
$ServerName,

[Parameter()]
[System.String]
$SqlVersion
)

if ($Action -eq 'Upgrade' -and $PSBoundParameters.ContainsKey('SqlVersion'))
{
$errorMessage = $script:localizedData.ParameterSqlVersionNotAllowedForSetupActionUpgrade

New-InvalidOperationException -Message $errorMessage
}

if ($FeatureFlag)
{
Write-Verbose -Message ($script:localizedData.FeatureFlag -f ($FeatureFlag -join ''','''))
Expand Down Expand Up @@ -156,6 +175,7 @@ function Get-TargetResource
FailoverClusterIPAddress = $null
UseEnglish = $UseEnglish
ServerName = $ServerName
SqlVersion = $null
}

<#
Expand Down Expand Up @@ -188,18 +208,27 @@ function Get-TargetResource
Connect-UncPath -RemotePath $SourcePath -SourceCredential $SourceCredential
}

$pathToSetupExecutable = Join-Path -Path $SourcePath -ChildPath 'setup.exe'
if (-not $PSBoundParameters.ContainsKey('SqlVersion'))
{
$pathToSetupExecutable = Join-Path -Path $SourcePath -ChildPath 'setup.exe'

Write-Verbose -Message ($script:localizedData.UsingPath -f $pathToSetupExecutable)
Write-Verbose -Message ($script:localizedData.UsingPath -f $pathToSetupExecutable)

$sqlVersion = Get-FilePathMajorVersion -Path $pathToSetupExecutable
$SqlVersion = Get-FilePathMajorVersion -Path $pathToSetupExecutable
}
else
{
$SqlVersion = ([System.Version] $SqlVersion).Major
}

$getTargetResourceReturnValue.SqlVersion = $SqlVersion

if ($SourceCredential)
{
Disconnect-UncPath -RemotePath $SourcePath
}

$serviceNames = Get-ServiceNamesForInstance -InstanceName $InstanceName -SqlServerMajorVersion $sqlVersion
$serviceNames = Get-ServiceNamesForInstance -InstanceName $InstanceName -SqlServerMajorVersion $SqlVersion

$features = ''

Expand Down Expand Up @@ -255,7 +284,7 @@ function Get-TargetResource
Write-Verbose -Message $script:localizedData.EvaluateDataQualityServicesFeature

# Check if the Data Quality Services sub component is configured.
$isDQInstalled = Test-IsDQComponentInstalled -InstanceName $InstanceName -SqlServerMajorVersion $sqlVersion
$isDQInstalled = Test-IsDQComponentInstalled -InstanceName $InstanceName -SqlServerMajorVersion $SqlVersion

if ($isDQInstalled)
{
Expand All @@ -276,7 +305,7 @@ function Get-TargetResource
$getTargetResourceReturnValue.InstanceDir = `
Get-InstanceProgramPath -InstanceName $InstanceName

if ($sqlVersion -ge 13)
if ($SqlVersion -ge 13)
{
# Retrieve information about Tempdb database and its files.
$currentTempDbProperties = Get-TempDbProperties -ServerName $sqlHostName -InstanceName $InstanceName
Expand Down Expand Up @@ -413,15 +442,15 @@ function Get-TargetResource
Write-Verbose -Message $script:localizedData.IntegrationServicesFeatureNotFound
}

$installedSharedFeatures = Get-InstalledSharedFeatures -SqlServerMajorVersion $sqlVersion
$installedSharedFeatures = Get-InstalledSharedFeatures -SqlServerMajorVersion $SqlVersion
$features += '{0},' -f ($installedSharedFeatures -join ',')

if ((Test-IsSsmsInstalled -SqlServerMajorVersion $sqlVersion))
if ((Test-IsSsmsInstalled -SqlServerMajorVersion $SqlVersion))
{
$features += 'SSMS,'
}

if ((Test-IsSsmsAdvancedInstalled -SqlServerMajorVersion $sqlVersion))
if ((Test-IsSsmsAdvancedInstalled -SqlServerMajorVersion $SqlVersion))
{
$features += 'ADV_SSMS,'
}
Expand All @@ -430,7 +459,7 @@ function Get-TargetResource

if ($features)
{
$currentSqlSharedPaths = Get-SqlSharedPaths -SqlServerMajorVersion $sqlVersion
$currentSqlSharedPaths = Get-SqlSharedPaths -SqlServerMajorVersion $SqlVersion

$getTargetResourceReturnValue.InstallSharedDir = $currentSqlSharedPaths.InstallSharedDir
$getTargetResourceReturnValue.InstallSharedWOWDir = $currentSqlSharedPaths.InstallSharedWOWDir
Expand Down Expand Up @@ -668,6 +697,14 @@ function Get-TargetResource
the host name for the listener or cluster group. If using a secure connection
the specified value should be the same name that is used in the certificate.
Default value is the current computer name.
.PARAMETER SqlVersion
Specifies the SQL Server version that should be installed. Only the major
version will be used, but the provided value must be set to at least major
and minor version (e.g. `14.0`). When providing this parameter the media
will not be used to evaluate version. Although, if the setup action is
`Upgrade` then setting this parameter will throw an exception as the version
from the install media is required.
#>
function Set-TargetResource
{
Expand Down Expand Up @@ -933,9 +970,20 @@ function Set-TargetResource

[Parameter()]
[System.String]
$ServerName
$ServerName,

[Parameter()]
[System.String]
$SqlVersion
)

if ($Action -eq 'Upgrade' -and $PSBoundParameters.ContainsKey('SqlVersion'))
{
$errorMessage = $script:localizedData.ParameterSqlVersionNotAllowedForSetupActionUpgrade

New-InvalidOperationException -Message $errorMessage
}

<#
Fixing issue 448, setting FailoverClusterGroupName to default value
if not specified in configuration.
Expand Down Expand Up @@ -1008,11 +1056,18 @@ function Set-TargetResource
$SourcePath = Invoke-InstallationMediaCopy @invokeInstallationMediaCopyParameters
}

$pathToSetupExecutable = Join-Path -Path $SourcePath -ChildPath 'setup.exe'
if (-not $PSBoundParameters.ContainsKey('SqlVersion'))
{
$pathToSetupExecutable = Join-Path -Path $SourcePath -ChildPath 'setup.exe'

Write-Verbose -Message ($script:localizedData.UsingPath -f $pathToSetupExecutable)
Write-Verbose -Message ($script:localizedData.UsingPath -f $pathToSetupExecutable)

$sqlVersion = Get-FilePathMajorVersion -Path $pathToSetupExecutable
$SqlVersion = Get-FilePathMajorVersion -Path $pathToSetupExecutable
}
else
{
$SqlVersion = ([System.Version] $SqlVersion).Major
}

# Determine features to install
$featuresToInstall = ''
Expand All @@ -1022,7 +1077,7 @@ function Set-TargetResource

foreach ($feature in $featuresArray)
{
if (-not ($feature | Test-SqlDscIsSupportedFeature -ProductVersion $sqlVersion))
if (-not ($feature | Test-SqlDscIsSupportedFeature -ProductVersion $SqlVersion))
{
$errorMessage = $script:localizedData.FeatureNotSupported -f $feature
New-InvalidOperationException -Message $errorMessage
Expand All @@ -1042,7 +1097,7 @@ function Set-TargetResource
$Features = $featuresToInstall.Trim(',')

# If SQL shared components already installed, clear InstallShared*Dir variables
switch ($sqlVersion)
switch ($SqlVersion)
{
{ $_ -in ('10', '11', '12', '13', '14', '15', '16') }
{
Expand Down Expand Up @@ -1550,8 +1605,11 @@ function Set-TargetResource
$arguments += '/ENU'
}

$arguments = $arguments.Trim()

# Replace sensitive values for verbose output
$log = $arguments

if ($SecurityMode -eq 'SQL')
{
$log = $log.Replace($SAPwd.GetNetworkCredential().Password, "********")
Expand All @@ -1571,12 +1629,14 @@ function Set-TargetResource
}
}

$arguments = $arguments.Trim()
Write-Verbose -Message ($script:localizedData.SetupArguments -f $log)

$pathToSetupExecutable = Join-Path -Path $SourcePath -ChildPath 'setup.exe'

Write-Verbose -Message ($script:localizedData.UsingPath -f $pathToSetupExecutable)

try
{
Write-Verbose -Message ($script:localizedData.SetupArguments -f $log)

<#
This handles when PsDscRunAsCredential is set, or running as the SYSTEM account (when
PsDscRunAsCredential is not set).
Expand Down Expand Up @@ -1896,6 +1956,14 @@ function Set-TargetResource
the host name for the listener or cluster group. If using a secure connection
the specified value should be the same name that is used in the certificate.
Default value is the current computer name.
.PARAMETER SqlVersion
Specifies the SQL Server version that should be installed. Only the major
version will be used, but the provided value must be set to at least major
and minor version (e.g. `14.0`). When providing this parameter the media
will not be used to evaluate version. Although, if the setup action is
`Upgrade` then setting this parameter will throw an exception as the version
from the install media is required.
#>
function Test-TargetResource
{
Expand Down Expand Up @@ -2161,9 +2229,20 @@ function Test-TargetResource

[Parameter()]
[System.String]
$ServerName
$ServerName,

[Parameter()]
[System.String]
$SqlVersion
)

if ($Action -eq 'Upgrade' -and $PSBoundParameters.ContainsKey('SqlVersion'))
{
$errorMessage = $script:localizedData.ParameterSqlVersionNotAllowedForSetupActionUpgrade

New-InvalidOperationException -Message $errorMessage
}

<#
Fixing issue 448, setting FailoverClusterGroupName to default value
if not specified in configuration.
Expand All @@ -2182,6 +2261,11 @@ function Test-TargetResource
FeatureFlag = $FeatureFlag
}

if ($PSBoundParameters.ContainsKey('SqlVersion'))
{
$getTargetResourceParameters.SqlVersion = $SqlVersion
}

if ($PSBoundParameters.ContainsKey('ServerName'))
{
$getTargetResourceParameters.ServerName = $ServerName
Expand Down
1 change: 1 addition & 0 deletions source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.schema.mof
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ class DSC_SqlSetup : OMI_BaseResource
[Write, Description("Specifies to install the English version of _SQL Server_ on a localized operating system when the installation media includes language packs for both English and the language corresponding to the operating system.")] Boolean UseEnglish;
[Write, Description("Specifies optional skip rules during setup.")] String SkipRule[];
[Write, Description("Specifies the host or network name of the _SQL Server_ instance. If the SQL Server belongs to a cluster or availability group it could be set to the host name for the listener or cluster group. If using a secure connection the specified value should be the same name that is used in the certificate. Default value is the current computer name.")] String ServerName;
[Write, Description("Specifies the SQL Server version that should be installed. Only the major version will be used, but the provided value must be set to at least major and minor version (e.g. `14.0`). When providing this parameter the media will not be used to evaluate version. Although, if the setup action is `Upgrade` then setting this parameter will throw an exception as the version from the install media is required.")] String SqlVersion;
[Read, Description("Returns a boolean value of `$true` if the instance is clustered, otherwise it returns `$false`.")] Boolean IsClustered;
};
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ 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}'.
ParameterSqlVersionNotAllowedForSetupActionUpgrade = The parameter SqlVersion is not allowed to be specified when using the setup action Upgrade.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ ConvertFrom-StringData @'
FeatureAlreadyInstalled = Funktionen '{0}' är redan installerad så den kommer inte bli installerad igen.
FeatureFlag = Använder tilläggsflagga '{0}'
DifferentMajorVersion = The instance '{0}' has the wrong major version. The major version is '{1}', but expected version '{2}'.
ParameterSqlVersionNotAllowedForSetupActionUpgrade = The parameter SqlVersion is not allowed to be specified when using the setup action Upgrade.
'@
Loading

0 comments on commit 472ef04

Please sign in to comment.