Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlProtocolTcpIp: Auto-detect the TCP/IP address group name by IP address #1940

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added new public command:
- `Get-SqlDscConfigurationOption` - Returns the available configuration
options that can be used with the DSC resource _SqlConfiguration_.
- SqlProtocolTcpIp
- Auto-detect the TCP/IP address group name by IP address

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ function Get-TargetResource
$RestartTimeout = 120
)

$IpAddressGroup = Find-IpAddressGroup -IpAddressGroup $IpAddressGroup -InstanceName $InstanceName

$IpAddressGroup = Convert-IpAdressGroupCasing -IpAddressGroup $IpAddressGroup

$returnValue = @{
Expand Down Expand Up @@ -271,6 +273,8 @@ function Set-TargetResource
$RestartTimeout = 120
)

$IpAddressGroup = Find-IpAddressGroup -IpAddressGroup $IpAddressGroup -InstanceName $InstanceName

$IpAddressGroup = Convert-IpAdressGroupCasing -IpAddressGroup $IpAddressGroup

<#
Expand Down Expand Up @@ -536,6 +540,10 @@ function Test-TargetResource
$RestartTimeout = 120
)

$IpAddressGroup = Find-IpAddressGroup -IpAddressGroup $IpAddressGroup -InstanceName $InstanceName

$IpAddressGroup = Convert-IpAdressGroupCasing -IpAddressGroup $IpAddressGroup

Write-Verbose -Message (
$script:localizedData.TestDesiredState -f $IpAddressGroup, $InstanceName, $ServerName
)
Expand Down Expand Up @@ -750,3 +758,71 @@ function Convert-IpAdressGroupCasing

return ($IpAddressGroup.ToUpper() -replace 'IPALL', 'IPAll')
}


<#
.SYNOPSIS
Find's an IP address group by the specified IP.

.PARAMETER IpAddress
The IP address already stored in an IP address group.
#>
function Find-IpAddressGroup
Fixed Show fixed Hide fixed
{
[CmdletBinding()]
[OutputType([System.String])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$InstanceName,

[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[System.String]
$IpAddressGroup
)

if ($IpAddressGroup -like '*.*.*.*' -or $IpAddressGroup -like '*:*')
{
# Getting the server protocol properties by using the computer name.
$computerName = Get-ComputerName

Write-Verbose -Message (
$script:localizedData.GetIpAddressGroupByIpAddress -f $IpAddressGroup
)

Import-SqlDscPreferredModule

<#
Must connect to the local machine name because $ServerName can
point to a cluster instance or availability group listener.
#>
$getServerProtocolObjectParameters = @{
ServerName = $computerName
Instance = $InstanceName
ProtocolName = 'TcpIp'
}

$serverProtocolProperties = Get-ServerProtocolObject @getServerProtocolObjectParameters

if ($serverProtocolProperties)
{
foreach ($ipAddressGroupObject in $serverProtocolProperties.IPAddresses)
{
if ($ipAddressGroupObject.IPAddress.IPAddressToString -eq $IpAddressGroup)
{
return $ipAddressGroupObject.Name
}
}
}

Write-Warning (
$script:localizedData.IpAddressGroupNotFoundError -f $IpAddressGroup
)
}
else
{
return $IpAddressGroup
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ ConvertFrom-StringData @'
GroupIsInDesiredState = The TCP/IP address group '{0}' on the instance '{1}' is already in desired state. (SSPTI0013)
RestartSuppressed = The restart was suppressed. The configuration will not be active until the node is manually restart. (SSPTI0014)
FailedToGetSqlServerProtocol = Failed to get the settings for the SQL Server Database Engine server protocol TCP/IP. (SSPTI0015)
GetIpAddressGroupByIpAddress = Detect the TCP/IP address group by using the TCP/IP address '{0}'. (SSPTI0016)
IpAddressGroupNotFoundError = The TCP/IP address group was not detected because the TCP/IP address '{0}' is not assigned to any TCP/IP address group. (SSPTI0017)
'@
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<#
.DESCRIPTION
This example will set the TCP/IP address group by detecting the group name. Not required to specify the group name.

The resource will be run as the account provided in $SystemAdministratorAccount.
#>
Configuration Example
{
param
(
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$SystemAdministratorAccount
)

Import-DscResource -ModuleName 'SqlServerDsc'

node localhost
{
SqlProtocolTcpIP 'ChangeIP'
{
InstanceName = 'MSSQLSERVER'
IpAddressGroup = 'fe80::7894:a6b6:59dd:c8fe%9'
Enabled = $true
IpAddress = 'fe80::7894:a6b6:59dd:c8fe%9'
TcpPort = '1433,1500,1501'

PsDscRunAsCredential = $SystemAdministratorAccount
}
}
}