Skip to content

Commit

Permalink
SqlSetup: Add parameter ServerName (#1900)
Browse files Browse the repository at this point in the history
- SqlSetup
  - Added new parameter `ServerName` that will be used as the host name when
    evaluating the SQL Server instance. If using a secure connection the
    specified value should be the same name that is used in the certificate
    (issue #1893).
  • Loading branch information
johlju authored Apr 8, 2023
1 parent ac13d70 commit 4dc1bee
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New public command:
- `Get-SqlDscPreferredModule` - Returns the name of the first available
preferred module ([issue #1879](https://github.com/dsccommunity/SqlServerDsc/issues/1879)).
- `SqlSecureConnection`
- SqlSecureConnection
- Added new parameter `ServerName` that will be used as the host name when
restarting the SQL Server instance. The specified value should be the same
name that is used in the certificate ([issue #1888](https://github.com/dsccommunity/SqlServerDsc/issues/1888)).
- SqlSetup
- Added new parameter `ServerName` that will be used as the host name when
evaluating the SQL Server instance. If using a secure connection the
specified value should be the same name that is used in the certificate
([issue #1893](https://github.com/dsccommunity/SqlServerDsc/issues/1893)).

### Changed

Expand Down
68 changes: 60 additions & 8 deletions source/DSCResources/DSC_SqlSetup/DSC_SqlSetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
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.
.PARAMETER ServerName
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.
#>
function Get-TargetResource
{
Expand Down Expand Up @@ -85,7 +92,11 @@ function Get-TargetResource

[Parameter()]
[System.Boolean]
$UseEnglish
$UseEnglish,

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

if ($FeatureFlag)
Expand Down Expand Up @@ -144,6 +155,7 @@ function Get-TargetResource
FailoverClusterGroupName = $null
FailoverClusterIPAddress = $null
UseEnglish = $UseEnglish
ServerName = $ServerName
}

<#
Expand All @@ -156,7 +168,14 @@ function Get-TargetResource
}
else
{
$sqlHostName = Get-ComputerName
if ($PSBoundParameters.ContainsKey('ServerName'))
{
$sqlHostName = $ServerName
}
else
{
$sqlHostName = Get-ComputerName
}
}

# Force drive list update, to pick up any newly mounted volumes
Expand Down Expand Up @@ -642,6 +661,13 @@ function Get-TargetResource
.PARAMETER SkipRule
Specifies optional skip rules during setup.
.PARAMETER ServerName
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.
#>
function Set-TargetResource
{
Expand Down Expand Up @@ -903,7 +929,11 @@ function Set-TargetResource
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String[]]
$SkipRule
$SkipRule,

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

<#
Expand All @@ -927,6 +957,11 @@ function Set-TargetResource
FeatureFlag = $FeatureFlag
}

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

$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters

$InstanceName = $InstanceName.ToUpper()
Expand Down Expand Up @@ -1854,6 +1889,13 @@ function Set-TargetResource
Specifies optional skip rules during setup.
Not used in Test-TargetResource.
.PARAMETER ServerName
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.
#>
function Test-TargetResource
{
Expand Down Expand Up @@ -2115,7 +2157,11 @@ function Test-TargetResource
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String[]]
$SkipRule
$SkipRule,

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

<#
Expand All @@ -2136,9 +2182,13 @@ function Test-TargetResource
FeatureFlag = $FeatureFlag
}

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

$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters

if ($null -eq $getTargetResourceResult.Features -or $getTargetResourceResult.Features -eq '')
{
Write-Verbose -Message $script:localizedData.NoFeaturesFound
Expand All @@ -2160,6 +2210,7 @@ function Test-TargetResource
if ($feature -notin $foundFeaturesArray)
{
Write-Verbose -Message ($script:localizedData.UnableToFindFeature -f $feature, $($getTargetResourceResult.Features))

$result = $false
}
}
Expand All @@ -2173,14 +2224,15 @@ function Test-TargetResource
{
Write-Verbose -Message $script:localizedData.EvaluatingClusterParameters

$variableNames = $boundParameters.Keys |
$variableNames = $PSBoundParameters.Keys |
Where-Object -FilterScript { $_ -imatch "^FailoverCluster" }

foreach ($variableName in $variableNames)
{
if ($getTargetResourceResult.$variableName -ne $boundParameters[$variableName])
if ($getTargetResourceResult.$variableName -ne $PSBoundParameters.$variableName)
{
Write-Verbose -Message ($script:localizedData.ClusterParameterIsNotInDesiredState -f $variableName, $($boundParameters[$variableName]))
Write-Verbose -Message ($script:localizedData.ClusterParameterIsNotInDesiredState -f $variableName, ($PSBoundParameters.$variableName))

$result = $false
}
}
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 @@ -68,5 +68,6 @@ class DSC_SqlSetup : OMI_BaseResource
[Write, Description("Feature flags are used to toggle DSC resource functionality on or off. See the DSC resource documentation for what additional functionality exist through a feature flag.")] String FeatureFlag[];
[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;
[Read, Description("Returns a boolean value of `$true` if the instance is clustered, otherwise it returns `$false`.")] Boolean IsClustered;
};
4 changes: 4 additions & 0 deletions tests/Unit/DSC_SqlSetup.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ Describe 'SqlSetup\Get-TargetResource' -Tag 'Get' {
SourceCredential = $null
SourcePath = $TestDrive
Feature = 'NewFeature' # Test enabling a code-feature.
ServerName = 'host.company.local'
}
}
}
Expand Down Expand Up @@ -376,6 +377,7 @@ Describe 'SqlSetup\Get-TargetResource' -Tag 'Get' {
$result.ASConfigDir | Should -BeNullOrEmpty
$result.ASServerMode | Should -BeNullOrEmpty
$result.ISSvcAccountUsername | Should -BeNullOrEmpty
$result.ServerName | Should -Be 'host.company.local'
}
}
}
Expand Down Expand Up @@ -1734,6 +1736,7 @@ Describe 'SqlSetup\Test-TargetResource' -Tag 'Test' {
$mockTestTargetResourceParameters.InstanceName = 'MSSQLSERVER'
$mockTestTargetResourceParameters.SourceCredential = $null
$mockTestTargetResourceParameters.SourcePath = $TestDrive
$mockTestTargetResourceParameters.ServerName = 'host.company.local'

$result = Test-TargetResource @mockTestTargetResourceParameters

Expand Down Expand Up @@ -2300,6 +2303,7 @@ Describe 'SqlSetup\Set-TargetResource' -Tag 'Set' {
SourcePath = $TestDrive
ProductKey = '1FAKE-2FAKE-3FAKE-4FAKE-5FAKE'
NpEnabled = $true
ServerName = 'host.company.local'
}

{ Set-TargetResource @mockSetTargetResourceParameters } | Should -Not -Throw
Expand Down

0 comments on commit 4dc1bee

Please sign in to comment.