Skip to content

Commit

Permalink
Changes to xSQLServerNetwork
Browse files Browse the repository at this point in the history
Added optional parameter SQLServer with default value of $env:COMPUTERNAME (issue dsccommunity#528).
 Added optional parameter RestartTimeout with default value of 120 seconds.
Now the resource supports restarting a sql server in a cluster (issue dsccommunity#527).
  • Loading branch information
johlju committed Apr 24, 2017
1 parent a93647c commit aa07d7a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 64 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
- Removed helper function Get-SQLPSInstance and Get-SQLPSInstanceName because there is no resource using it any longer.
- Changes to xSQLServer
- Fixed typos in markdown files; CHANGELOG, CONTRIBUTING, README and ISSUE_TEMPLATE.
- Changes to xSQLServerNetwork
- Added optional parameter SQLServer with default value of $env:COMPUTERNAME (issue #528).
- Added optional parameter RestartTimeout with default value of 120 seconds.
- Now the resource supports restarting a sql server in a cluster (issue #527).

## 7.0.0.0

Expand Down
93 changes: 32 additions & 61 deletions DSCResources/MSFT_xSQLServerNetwork/MSFT_xSQLServerNetwork.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Function Get-TargetResource
Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) `
-ChildPath 'xSQLServerHelper.psm1') `
-Force

Function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
Expand Down Expand Up @@ -29,7 +33,7 @@
{
throw "Unable to resolve SQL version for instance"
}

$smo = $dom_get.Load("Microsoft.SqlServer.Smo, Version=$version.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")
$sqlWmiManagement = $dom_get.Load("Microsoft.SqlServer.SqlWmiManagement, Version=$version.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")

Expand All @@ -55,14 +59,19 @@
{
[System.AppDomain]::Unload($dom_get)
}

return $returnValue
}

Function Set-TargetResource
{
[CmdletBinding()]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$SQLServer = $env:COMPUTERNAME,

[parameter(Mandatory = $true)]
[System.String]
$InstanceName,
Expand All @@ -83,7 +92,11 @@ Function Set-TargetResource
$TCPPort,

[System.Boolean]
$RestartService = $false
$RestartService = $false,

[Parameter()]
[System.UInt16]
$RestartTimeout = 120
)

Write-Verbose "xSQLServerNetwork.Set-TargetResource ..."
Expand All @@ -103,7 +116,7 @@ Function Set-TargetResource
{
throw "Unable to resolve SQL version for instance"
}

$smo = $dom_set.Load("Microsoft.SqlServer.Smo, Version=$version.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")
$sqlWmiManagement = $dom_set.Load("Microsoft.SqlServer.SqlWmiManagement, Version=$version.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")

Expand Down Expand Up @@ -147,58 +160,7 @@ Function Set-TargetResource

if($RestartService)
{
Write-Verbose "SQL Service will be restarted ..."
if($InstanceName -eq "MSSQLSERVER")
{
$dbServiceName = "MSSQLSERVER"
$agtServiceName = "SQLSERVERAGENT"
}
else
{
$dbServiceName = "MSSQL`$$InstanceName"
$agtServiceName = "SQLAgent`$$InstanceName"
}

$sqlService = $wmi.Services[$dbServiceName]
$agentService = $wmi.Services[$agtServiceName]
$startAgent = ($agentService.ServiceState -eq "Running")

if ($sqlService -eq $null)
{
throw "$dbServiceName service was not found, restart service failed"
}

Write-Verbose "Stopping [$dbServiceName] service ..."
$sqlService.Stop()

while($sqlService.ServiceState -ne "Stopped")
{
Start-Sleep -Milliseconds 500
$sqlService.Refresh()
}
Write-Verbose "[$dbServiceName] service stopped"

Write-Verbose "Starting [$dbServiceName] service ..."
$sqlService.Start()

while($sqlService.ServiceState -ne "Running")
{
Start-Sleep -Milliseconds 500
$sqlService.Refresh()
}
Write-Verbose "[$dbServiceName] service started"

if ($startAgent)
{
Write-Verbose "Staring [$agtServiceName] service ..."
$agentService.Start()
while($agentService.ServiceState -ne "Running")
{
Start-Sleep -Milliseconds 500
$agentService.Refresh()
}
Write-Verbose "[$agtServiceName] service started"
}
Restart-SqlService -SQLServer $SQLServer -SQLInstanceName $InstanceName -Timeout $RestartTimeout
}
}
Finally
Expand All @@ -212,6 +174,11 @@ Function Test-TargetResource
[CmdletBinding()]
[OutputType([System.Boolean])]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$SQLServer = $env:COMPUTERNAME,

[parameter(Mandatory = $true)]
[System.String]
$InstanceName,
Expand All @@ -232,7 +199,11 @@ Function Test-TargetResource
$TCPPort,

[System.Boolean]
$RestartService = $false
$RestartService = $false,

[Parameter()]
[System.UInt16]
$RestartTimeout = 120
)

Write-Verbose "xSQLServerNetwork.Test-TargetResource ..."
Expand All @@ -244,8 +215,8 @@ Function Test-TargetResource
IsEnabled = $IsEnabled
TCPDynamicPorts = $TCPDynamicPorts
TCPPort = $TCPPort
}
}

Write-Verbose "Calling xSQLServerNetwork.Get-TargetResource ..."
$currentState = Get-TargetResource -InstanceName $InstanceName -ProtocolName $ProtocolName

Expand All @@ -267,7 +238,7 @@ Function Test-TargetResource
}
}

Write-Verbose "States match"
Write-Verbose "States match"
return $true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ class MSFT_xSQLServerNetwork : OMI_BaseResource
{
[Key, Description("SQL Server instance name of which network protocol should be configured")] String InstanceName;
[Required, Description("Network protocol name that should be configured"), ValueMap{"tcp"}, Values{"tcp"}] String ProtocolName;
[Write, Description("The host name of the SQL Server to be configured.")] String SQLServer;
[Write, Description("Is network protocol should be enabled or disabled")] Boolean IsEnabled;
[Write, Description("If dynamic ports are used should be set to 0, otherwise leave empty"), ValueMap{"0"}, Values{"0"}] String TCPDynamicPorts;
[Write, Description("Sets static port for TCP/IP")] String TCPPort;
[Write, Description("Controls if affected SQL Service should be restarted automatically")] Boolean RestartService;
[Write, Description("Timeout value for restarting the SQL services. The default value is 120 seconds.")] UInt16 RestartTimeout;
};
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,8 @@ SQL Max Memory = TotalPhysicalMemory - (NumOfSQLThreads\*ThreadStackSize) - (102
#### Parameters

* **[String] SQLInstance** _(Key)_: The name of the SQL instance to be configured.
* **[String] SQLServer** _(Write)_: The host name of the SQL Server to be configured. Default value is *$env:COMPUTERNAME*.
* **[Boolean] DynamicAlloc** _(Write)_: If set to $true then max memory will be dynamically configured. When this is set parameter is set to $true, the parameter MaxMemory must be set to $null or not be configured. Default value is *$false*.
* **[String] SQLServer** _(Write)_: The host name of the SQL Server to be configured. Default value is $env:COMPUTERNAME.
* **[Boolean] DynamicAlloc** _(Write)_: If set to $true then max memory will be dynamically configured. When this is set parameter is set to $true, the parameter MaxMemory must be set to $null or not be configured. Default value is $false.
* **[String] Ensure** _(Write)_: When set to 'Present' then min and max memory will be set to either the value in parameter MinMemory and MaxMemory or dynamically configured when parameter DynamicAlloc is set to $true. When set to 'Absent' min and max memory will be set to default values. { *Present* | Absent }.
* **[Sint32] MinMemory** _(Write)_: Minimum amount of memory, in MB, in the buffer pool used by the instance of SQL Server.
* **[Sint32] MaxMemory** _(Write)_: Maximum amount of memory, in MB, in the buffer pool used by the instance of SQL Server.
Expand All @@ -765,10 +765,12 @@ No description.

* **[String] InstanceName** _(Key)_: name of SQL Server instance for which network will be configured.
* **[String] ProtocolName** _(Required)_: Name of network protocol to be configured. Only tcp is currently supported. { tcp }.
* **[String] SQLServer** _(Write)_: The host name of the SQL Server to be configured. Default value is $env:COMPUTERNAME.
* **[Boolean] IsEnabled** _(Write)_: Enables/Disables network protocol.
* **[String] TCPDynamicPorts** _(Write)_: 0 if Dynamic ports should be used otherwise empty. { 0 }.
* **[String] TCPPort** _(Write)_: Custom TCP port.
* **[Boolean] RestartService** _(Write)_: If true will restart SQL Service instance service after update. Default false.
* **[Boolean] RestartService** _(Write)_: If true will restart SQL Service instance service after update. The default value is $false.
* **[Uint16] RestartTimeout** _(Write)_: Timeout value for restarting the SQL services. The default value is 120 seconds.

#### Examples

Expand Down

0 comments on commit aa07d7a

Please sign in to comment.