DSCResources/MSFT_SqlServerConfiguration/MSFT_SqlServerConfiguration.psm1

# Load Common Code
Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) `
        -ChildPath 'SqlServerDscHelper.psm1') `
    -Force
<#
    .SYNOPSIS
    Gets the current value of a SQL configuration option
 
    .PARAMETER ServerName
    Hostname of the SQL Server to be configured
 
    .PARAMETER InstanceName
    Name of the SQL instance to be configued. Default is 'MSSQLSERVER'
 
    .PARAMETER OptionName
    The name of the SQL configuration option to be checked
 
    .PARAMETER OptionValue
    The desired value of the SQL configuration option
 
    .PARAMETER RestartService
    *** Not used in this function ***
    Determines whether the instance should be restarted after updating the configuration option.
 
    .PARAMETER RestartTimeout
    *** Not used in this function ***
    The length of time, in seconds, to wait for the service to restart. Default is 120 seconds.
#>

function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param(
        [Parameter(Mandatory = $true)]
        [String]
        $ServerName,

        [Parameter(Mandatory = $true)]
        [String]
        $InstanceName,

        [Parameter(Mandatory = $true)]
        [String]
        $OptionName,

        [Parameter(Mandatory = $true)]
        [Int32]
        $OptionValue,

        [Parameter()]
        [Boolean]
        $RestartService = $false,

        [Parameter()]
        [Int32]
        $RestartTimeout = 120
    )

    $sql = Connect-SQL -SQLServer $ServerName -SQLInstanceName $InstanceName

    ## get the configuration option
    $option = $sql.Configuration.Properties | Where-Object { $_.DisplayName -eq $OptionName }

    if (!$option)
    {
        throw New-TerminatingError -ErrorType "ConfigurationOptionNotFound" -FormatArgs $OptionName -ErrorCategory InvalidArgument
    }

    return @{
        ServerName     = $ServerName
        InstanceName   = $InstanceName
        OptionName     = $option.DisplayName
        OptionValue    = $option.ConfigValue
        RestartService = $RestartService
        RestartTimeout = $RestartTimeout
    }
}

<#
    .SYNOPSIS
    Sets the value of a SQL configuration option
 
    .PARAMETER ServerName
    Hostname of the SQL Server to be configured
 
    .PARAMETER InstanceName
    Name of the SQL instance to be configued. Default is 'MSSQLSERVER'
 
    .PARAMETER OptionName
    The name of the SQL configuration option to be set
 
    .PARAMETER OptionValue
    The desired value of the SQL configuration option
 
    .PARAMETER RestartService
    Determines whether the instance should be restarted after updating the configuration option
 
    .PARAMETER RestartTimeout
    The length of time, in seconds, to wait for the service to restart. Default is 120 seconds.
#>

function Set-TargetResource
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [String]
        $ServerName,

        [Parameter(Mandatory = $true)]
        [String]
        $InstanceName,

        [Parameter(Mandatory = $true)]
        [String]
        $OptionName,

        [Parameter(Mandatory = $true)]
        [Int32]
        $OptionValue,

        [Parameter()]
        [Boolean]
        $RestartService = $false,

        [Parameter()]
        [Int32]
        $RestartTimeout = 120
    )

    $sql = Connect-SQL -SQLServer $ServerName -SQLInstanceName $InstanceName

    ## get the configuration option
    $option = $sql.Configuration.Properties | Where-Object { $_.DisplayName -eq $OptionName }

    if (!$option)
    {
        throw New-TerminatingError -ErrorType "ConfigurationOptionNotFound" -FormatArgs $OptionName -ErrorCategory InvalidArgument
    }

    $option.ConfigValue = $OptionValue
    $sql.Configuration.Alter()

    if ($option.IsDynamic -eq $true)
    {
        New-VerboseMessage -Message 'Configuration option has been updated.'
    }
    elseif (($option.IsDynamic -eq $false) -and ($RestartService -eq $true))
    {
        New-VerboseMessage -Message 'Configuration option has been updated, restarting instance...'
        Restart-SqlService -SQLServer $ServerName -SQLInstanceName $InstanceName -Timeout $RestartTimeout
    }
    else
    {
        New-WarningMessage -WarningType 'ConfigurationRestartRequired' -FormatArgs $OptionName
    }
}

<#
    .SYNOPSIS
    Determines whether a SQL configuration option value is properly set
 
    .PARAMETER ServerName
    Hostname of the SQL Server to be configured
 
    .PARAMETER InstanceName
    Name of the SQL instance to be configued. Default is 'MSSQLSERVER'
 
    .PARAMETER OptionName
    The name of the SQL configuration option to be tested
 
    .PARAMETER OptionValue
    The desired value of the SQL configuration option
 
    .PARAMETER RestartService
    *** Not used in this function ***
    Determines whether the instance should be restarted after updating the configuration option
 
    .PARAMETER RestartTimeout
    *** Not used in this function ***
    The length of time, in seconds, to wait for the service to restart.
#>

function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory = $true)]
        [String]
        $ServerName,

        [Parameter(Mandatory = $true)]
        [String]
        $InstanceName,

        [Parameter(Mandatory = $true)]
        [String]
        $OptionName,

        [Parameter(Mandatory = $true)]
        [Int32]
        $OptionValue,

        [Parameter()]
        [Boolean]
        $RestartService = $false,

        [Parameter()]
        [Int32]
        $RestartTimeout = 120
    )

    ## Get the current state of the configuration item
    $state = Get-TargetResource @PSBoundParameters

    ## return whether the value matches the desired state
    return ($state.OptionValue -eq $OptionValue)
}

Export-ModuleMember -Function *-TargetResource