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

SqlServerNetwork: Refactored to not load assembly from GAC #1172

Merged
merged 4 commits into from
Jul 6, 2018
Merged
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@

## Unreleased

- Changes to SqlServerDsc
- Updated helper function Restart-SqlService to have to new optional parameters
`SkipClusterCheck` and `SkipWaitForOnline`. This was to support more aspects
of the resource SqlServerNetwork.
- Changes to SqlAlwaysOnService
- Integration tests was updated to handle new IPv6 addresses on the AppVeyor
build worker ([issue #1155](https://github.com/PowerShell/SqlServerDsc/issues/1155)).
- Changes to SqlServerNetwork
- Refactor SqlServerNetwork to not load assembly from GAC ([issue #1151](https://github.com/PowerShell/SqlServerDsc/issues/1151)).
- The resource now supports restarting the SQL Server service when both
enabling and disabling the protocol.
- Added integration tests for this resource
([issue #751](https://github.com/PowerShell/SqlServerDsc/issues/751)).

## 11.3.0.0

Expand Down
175 changes: 91 additions & 84 deletions DSCResources/MSFT_SqlServerNetwork/MSFT_SqlServerNetwork.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,24 @@ function Get-TargetResource
$ProtocolName
)

try
{
$applicationDomainObject = Register-SqlWmiManagement -SQLInstanceName $InstanceName

$managedComputerObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
Import-SQLPSModule

Write-Verbose -Message ($script:localizedData.GetNetworkProtocol -f $ProtocolName, $InstanceName)
$tcp = $managedComputerObject.ServerInstances[$InstanceName].ServerProtocols[$ProtocolName]
$managedComputerObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer

Write-Verbose -Message $script:localizedData.ReadingNetworkProperties
$returnValue = @{
InstanceName = $InstanceName
ProtocolName = $ProtocolName
IsEnabled = $tcp.IsEnabled
TcpDynamicPort = ($tcp.IPAddresses['IPAll'].IPAddressProperties['TcpDynamicPorts'].Value -ge 0)
TcpPort = $tcp.IPAddresses['IPAll'].IPAddressProperties['TcpPort'].Value
}
Write-Verbose -Message ($script:localizedData.GetNetworkProtocol -f $ProtocolName, $InstanceName)
$tcp = $managedComputerObject.ServerInstances[$InstanceName].ServerProtocols[$ProtocolName]

$returnValue.Keys | ForEach-Object {
Write-Verbose -Message "$_ = $($returnValue[$_])"
}
Write-Verbose -Message $script:localizedData.ReadingNetworkProperties
$returnValue = @{
InstanceName = $InstanceName
ProtocolName = $ProtocolName
IsEnabled = $tcp.IsEnabled
TcpDynamicPort = ($tcp.IPAddresses['IPAll'].IPAddressProperties['TcpDynamicPorts'].Value -ge 0)
TcpPort = $tcp.IPAddresses['IPAll'].IPAddressProperties['TcpPort'].Value
}
finally
{
Unregister-SqlAssemblies -ApplicationDomain $applicationDomainObject

$returnValue.Keys | ForEach-Object {
Write-Verbose -Message "$_ = $($returnValue[$_])"
}

return $returnValue
Expand Down Expand Up @@ -145,90 +138,104 @@ function Set-TargetResource

$getTargetResourceResult = Get-TargetResource -InstanceName $InstanceName -ProtocolName $ProtocolName

try
{
$applicationDomainObject = Register-SqlWmiManagement -SQLInstanceName $InstanceName

$desiredState = @{
InstanceName = $InstanceName
ProtocolName = $ProtocolName
IsEnabled = $IsEnabled
TcpDynamicPort = $TcpDynamicPort
TcpPort = $TcpPort
}
$desiredState = @{
InstanceName = $InstanceName
ProtocolName = $ProtocolName
IsEnabled = $IsEnabled
TcpDynamicPort = $TcpDynamicPort
TcpPort = $TcpPort
}

$isRestartNeeded = $false
$isRestartNeeded = $false

$managedComputerObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
# Get-TargetResource makes the necessary calls so the type ManagedComputer is available.
$managedComputerObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer

Write-Verbose -Message ($script:localizedData.GetNetworkProtocol -f $ProtocolName, $InstanceName)
$tcp = $managedComputerObject.ServerInstances[$InstanceName].ServerProtocols[$ProtocolName]
Write-Verbose -Message ($script:localizedData.GetNetworkProtocol -f $ProtocolName, $InstanceName)
$tcp = $managedComputerObject.ServerInstances[$InstanceName].ServerProtocols[$ProtocolName]

Write-Verbose -Message ($script:localizedData.CheckingProperty -f 'IsEnabled')
if ($desiredState.IsEnabled -ine $getTargetResourceResult.IsEnabled)
{
Write-Verbose -Message ($script:localizedData.UpdatingProperty -f 'IsEnabled', $getTargetResourceResult.IsEnabled, $desiredState.IsEnabled)
$tcp.IsEnabled = $desiredState.IsEnabled
$tcp.Alter()
Write-Verbose -Message ($script:localizedData.CheckingProperty -f 'IsEnabled')
if ($desiredState.IsEnabled -ine $getTargetResourceResult.IsEnabled)
{
Write-Verbose -Message ($script:localizedData.UpdatingProperty -f 'IsEnabled', $getTargetResourceResult.IsEnabled, $desiredState.IsEnabled)
$tcp.IsEnabled = $desiredState.IsEnabled
$tcp.Alter()

$isRestartNeeded = $true
}

$isRestartNeeded = $true
Write-Verbose -Message ($script:localizedData.CheckingProperty -f 'TcpDynamicPort')
if ($desiredState.TcpDynamicPort -ne $getTargetResourceResult.TcpDynamicPort)
{
# Translates the current and desired state to a string for display
$dynamicPortDisplayValueTable = @{
$true = 'enabled'
$false = 'disabled'
}

Write-Verbose -Message ($script:localizedData.CheckingProperty -f 'TcpDynamicPort')
if ($desiredState.TcpDynamicPort -ne $getTargetResourceResult.TcpDynamicPort)
{
# Translates the current and desired state to a string for display
$dynamicPortDisplayValueTable = @{
$true = 'enabled'
$false = 'disabled'
}
# Translates the desired state to a valid value
$desiredDynamicPortValue = @{
$true = '0'
$false = ''
}

# Translates the desired state to a valid value
$desiredDynamicPortValue = @{
$true = '0'
$false = ''
}
$fromTcpDynamicPortDisplayValue = $dynamicPortDisplayValueTable[$getTargetResourceResult.TcpDynamicPort]
$toTcpDynamicPortDisplayValue = $dynamicPortDisplayValueTable[$desiredState.TcpDynamicPort]

$fromTcpDynamicPortDisplayValue = $dynamicPortDisplayValueTable[$getTargetResourceResult.TcpDynamicPort]
$toTcpDynamicPortDisplayValue = $dynamicPortDisplayValueTable[$desiredState.TcpDynamicPort]
Write-Verbose -Message ($script:localizedData.UpdatingProperty -f 'TcpDynamicPorts', $fromTcpDynamicPortDisplayValue, $toTcpDynamicPortDisplayValue)
$tcp.IPAddresses['IPAll'].IPAddressProperties['TcpDynamicPorts'].Value = $desiredDynamicPortValue[$desiredState.TcpDynamicPort]
$tcp.Alter()

Write-Verbose -Message ($script:localizedData.UpdatingProperty -f 'TcpDynamicPorts', $fromTcpDynamicPortDisplayValue, $toTcpDynamicPortDisplayValue)
$tcp.IPAddresses['IPAll'].IPAddressProperties['TcpDynamicPorts'].Value = $desiredDynamicPortValue[$desiredState.TcpDynamicPort]
$tcp.Alter()
$isRestartNeeded = $true
}

$isRestartNeeded = $true
Write-Verbose -Message ($script:localizedData.CheckingProperty -f 'TcpPort')
if ($desiredState.TcpPort -ine $getTargetResourceResult.TcpPort)
{
$fromTcpPort = $getTargetResourceResult.TcpPort
if ($fromTcpPort -eq '')
{
$fromTcpPort = 'none'
}

Write-Verbose -Message ($script:localizedData.CheckingProperty -f 'TcpPort')
if ($desiredState.TcpPort -ine $getTargetResourceResult.TcpPort)
$toTcpPort = $desiredState.TcpPort
if ($toTcpPort -eq '')
{
$fromTcpPort = $getTargetResourceResult.TcpPort
if ($fromTcpPort -eq '')
{
$fromTcpPort = 'none'
}
$toTcpPort = 'none'
}

$toTcpPort = $desiredState.TcpPort
if ($toTcpPort -eq '')
{
$toTcpPort = 'none'
}
Write-Verbose -Message ($script:localizedData.UpdatingProperty -f 'TcpPort', $fromTcpPort, $toTcpPort)
$tcp.IPAddresses['IPAll'].IPAddressProperties['TcpPort'].Value = $desiredState.TcpPort
$tcp.Alter()

Write-Verbose -Message ($script:localizedData.UpdatingProperty -f 'TcpPort', $fromTcpPort, $toTcpPort)
$tcp.IPAddresses['IPAll'].IPAddressProperties['TcpPort'].Value = $desiredState.TcpPort
$tcp.Alter()
$isRestartNeeded = $true
}

$isRestartNeeded = $true
if ($RestartService -and $isRestartNeeded)
{
$restartSqlServiceParameters = @{
SQLServer = $ServerName
SQLInstanceName = $InstanceName
Timeout = $RestartTimeout
}

if ($RestartService -and $isRestartNeeded)
if ($getTargetResourceResult.IsEnabled -eq $false -and $IsEnabled -eq $true)
{
Restart-SqlService -SQLServer $ServerName -SQLInstanceName $InstanceName -Timeout $RestartTimeout
<#
If the protocol was disabled and now being enabled, is not possible
to connect to the instance to evaluate if it is a clustered instance.
This is being tracked in issue #1174.
#>
$restartSqlServiceParameters['SkipClusterCheck'] = $true
}
}
finally
{
Unregister-SqlAssemblies -ApplicationDomain $applicationDomainObject

if ($PSBoundParameters.ContainsKey('IsEnabled') -and $IsEnabled -eq $false)
{
# If the protocol is disabled it is not possible to connect to the instance.
$restartSqlServiceParameters['SkipWaitForOnline'] = $true
}

Restart-SqlService @restartSqlServiceParameters
}
}

Expand Down
Loading