Skip to content

Commit

Permalink
Merge pull request #70 from PowerShell/dev
Browse files Browse the repository at this point in the history
Release of version 1.7.0.0 of xSQLServer
  • Loading branch information
kwirkykat authored Jun 29, 2016
2 parents b87803f + 71ff460 commit 7d38ef3
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 63 deletions.
256 changes: 256 additions & 0 deletions DSCResources/xSQLServerConfiguration/xSQLServerConfiguration.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
$dom = [System.AppDomain]::CreateDomain("xSQLServerConfiguration")

Function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param(
[parameter(Mandatory = $true)]
[System.String]
$InstanceName,

[parameter(Mandatory = $true)]
[System.String]
$OptionName,

[parameter(Mandatory = $true)]
[System.Int32]
$OptionValue,

[System.Boolean]
$RestartService = $false
)

$sqlServer = Get-SqlServerObject -InstanceName $InstanceName
$option = $sqlServer.Configuration.Properties | where {$_.DisplayName -eq $optionName}
if(!$option)
{
throw "Specified option '$OptionName' was not found!"
}

$returnValue = @{
InstanceName = $InstanceName
OptionName = $option.DisplayName
OptionValue = $option.ConfigValue
RestartService = $RestartService
}

return $returnValue
}

Function Set-TargetResource
{
[CmdletBinding()]
param(
[parameter(Mandatory = $true)]
[System.String]
$InstanceName,

[parameter(Mandatory = $true)]
[System.String]
$OptionName,

[parameter(Mandatory = $true)]
[System.Int32]
$OptionValue,

[System.Boolean]
$RestartService = $false
)

$sqlServer = Get-SqlServerObject -InstanceName $InstanceName

$option = $sqlServer.Configuration.Properties | where {$_.DisplayName -eq $optionName}

if(!$option)
{
throw "Specified option '$OptionName' was not found!"
}

$option.ConfigValue = $OptionValue
$sqlServer.Configuration.Alter()
if ($option.IsDynamic -eq $true)
{
Write-Verbose "Configuration option has been updated."
}
elseif ($option.IsDynamic -eq $false -and $RestartService -eq $true)
{
Write-Verbose "Configuration option has been updated ..."

Restart-SqlServer -InstanceName $InstanceName
}
else
{
Write-Warning "Configuration option was set but SQL Server restart is required."
}
}

Function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param(
[parameter(Mandatory = $true)]
[System.String]
$InstanceName,

[parameter(Mandatory = $true)]
[System.String]
$OptionName,

[parameter(Mandatory = $true)]
[System.Int32]
$OptionValue,

[System.Boolean]
$RestartService = $false
)

$state = Get-TargetResource -InstanceName $InstanceName -OptionName $OptionName -OptionValue $OptionValue

return ($state.OptionValue -eq $OptionValue)
}

#region helper functions
Function Get-SqlServerMajorVersion
{
[CmdletBinding()]
[OutputType([System.String])]
param(
[parameter(Mandatory = $true)]
[System.String]
$InstanceName
)

$instanceId = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL").$InstanceName
$sqlVersion = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instanceId\Setup").Version
$sqlMajorVersion = $sqlVersion.Split(".")[0]
if (!$sqlMajorVersion)
{
throw "Unable to detect version for sql server instance: $InstanceName!"
}
return $sqlMajorVersion
}

Function Get-SqlServerObject
{
[CmdletBinding()]
param(
[parameter(Mandatory = $true)]
[System.String]
$InstanceName
)

if($InstanceName -eq "MSSQLSERVER")
{
$connectSQL = $env:COMPUTERNAME
}
else
{
$connectSQL = "$($env:COMPUTERNAME)\$InstanceName"
}

$sqlMajorVersion = Get-SqlServerMajorVersion -InstanceName $InstanceName
$smo = $dom.Load("Microsoft.SqlServer.Smo, Version=$sqlMajorVersion.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")
Write-Verbose "Loaded assembly: $($smo.FullName)"

$sqlServer = new-object $smo.GetType("Microsoft.SqlServer.Management.Smo.Server") $connectSQL

if(!$sqlServer)
{
throw "Unable to connect to sql instance: $InstanceName"
}

return $sqlServer
}

Function Restart-SqlServer
{
[CmdletBinding()]
param(
[parameter(Mandatory = $true)]
[System.String]
$InstanceName
)

$sqlMajorVersion = Get-SqlServerMajorVersion -InstanceName $InstanceName
$sqlWmiManagement = $dom.Load("Microsoft.SqlServer.SqlWmiManagement, Version=$sqlMajorVersion.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91")
Write-Verbose "Loaded assembly: $($sqlWmiManagement.FullName)"
$wmi = new-object $sqlWmiManagement.GetType("Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer")

if(!$wmi)
{
throw "Unable to create wmi ManagedComputer object for sql instance: $InstanceName"
}

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()
Wait-SqlServiceState -Service $sqlService -State "Stopped"

Write-Verbose "Starting [$dbServiceName] service ..."
$sqlService.Start()
Wait-SqlServiceState -Service $sqlService -State "Running"

if ($startAgent)
{
Write-Verbose "Starting [$agtServiceName] service ..."
$agentService.Start()
Wait-SqlServiceState -Service $agentService -State "Running"
}
}

function Wait-SqlServiceState
{
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[System.Object]
$Service,

[Parameter(Mandatory = $true)]
[System.String]
$State,

[System.Int32]
$TimeOut = 60
)

$startTime = Get-Date

while($Service.ServiceState -ne $State)
{
if((New-TimeSpan -Start $startTime -End (Get-Date)).TotalSeconds -gt $TimeOut)
{
throw "Time out of $TimeOut seconds exceeded while waiting for service [$($Service.DisplayName)] to enter '$State' state!"
}

Start-Sleep -Milliseconds 500
$Service.Refresh()
}

Write-Verbose "[$($Service.DisplayName)] service is $State"
}
#endregion

Export-ModuleMember -Function *-TargetResource
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[ClassVersion("1.0.0.0"), FriendlyName("xSQLServerConfiguration")]
class xSQLServerConfiguration : OMI_BaseResource
{
[Key, Description("SQL Server instance name of which configuration will be managed")] String InstanceName;
[Key, Description("SQL Server configuration option to be set")] String OptionName;
[Required, Description("Configuration option value to be set")] Sint32 OptionValue;
[Write, Description("Controls if affected SQL Service should be restarted automatically")] Boolean RestartService;
};
3 changes: 3 additions & 0 deletions Examples/SQL-ClusterDB.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#requires -Version 5

[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
param ()

Configuration SQL
{
Import-DscResource -Module xSQLServer
Expand Down
3 changes: 3 additions & 0 deletions Examples/SQL-Standalone.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#requires -Version 5

[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
param ()

Configuration SQLSA
{
Import-DscResource -Module xSQLServer
Expand Down
5 changes: 4 additions & 1 deletion Examples/SQLServerNetwork.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Configuration SQLNetwork
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
param ()

Configuration SQLNetwork
{
Import-DscResource -Module xSQLServer

Expand Down
28 changes: 28 additions & 0 deletions Examples/xSQLServerConfiguration.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#this will configure 'show advanced options' option of default instance on local machine

configuration SQLConfigSample
{
Import-DscResource -ModuleName xSQLServer
Node 'localhost'
{

LocalConfigurationManager
{
#this option should only be used during testing, remove it in production environment
DebugMode = 'ForceModuleImport'
}

#to get all available options run sp_configure, or refer to https://msdn.microsoft.com/en-us/library/ms189631.aspx
xSQLServerConfiguration test
{
InstanceName = 'MSSQLSERVER'
OptionName = 'priority boost'
OptionValue = 1
RestartService = $false
}
}
}

SQLConfigSample
Set-DscLocalConfigurationManager .\SQLConfigSample -Force -Verbose #only needed if using DebugMode
Start-DscConfiguration .\SQLConfigSample -Wait -Force -Verbose
Loading

0 comments on commit 7d38ef3

Please sign in to comment.