SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Checking SQL Configuration with Pester & Dbatools

I know, I know, there’s loads of different ways to test the configuration of SQL Server but I’ve started playing around with Pester recently and it’s pretty cool!

The script I’ve written here will use Pester & Dbatools to check all settings in sys.configurations against values stored in a .csv file. The script will then alter the settings in SQL Server if they differ. So be warned!! ??

Here’s the script: –

Import-Module Pester
Import-Module dbatools

[string]$SqlServer = ''

$csv = Import-Csv .\ExampleServerConfigValues.csv
foreach($row in $csv){
        $ConfigName = $row.Name
        $ConfigValue = $row.Value

        $ConfigTest = Invoke-Pester -Script @{Path = '.\Test-SqlConfig.ps1'; `
            Parameters = @{SqlServer = $SqlServer; `
                ConfigName = $ConfigName; `
                    ConfigValue = $ConfigValue}} `
                        -TestName '*Testing configuration*' `
                            -PassThru

        $ConfigTest.TestResult.foreach{
            if($_.Result -ne "Passed"){
                Write-Host "Setting $ConfigName on $SqlServer..." -ForegroundColor Yellow
                    Set-DbaSpConfigure -SqlInstance $SqlServer -ConfigName $ConfigName -Value $ConfigValue
            }
        }   
    }

N.B. – This can easily be altered to reference, say, a Central Management Server that holds the server names to test and the desired config values

The Pester test [.\Test-SqlConfig.ps1] that it’s referencing is: –

param(
    [Parameter(Mandatory)] 
    [ValidateNotNullOrEmpty()]
    [string]$SqlServer,
    [string]$ConfigName,
    [string]$ConfigValue
)

Describe "Testing configuration $ConfigName of Sql Instance $SqlServer"{
    Context "SqlConfig"{
        It "$ConfigName should be $ConfigValue"{
            $value = Get-DbaSpConfigure -SqlServer $SqlServer -Config $ConfigName
            $value.ConfiguredValue | Should be $ConfigValue
        }
    }
}

N.B. – This code is also on GitHub here.

So run the first script and, BOOM!

One Pester test running!

What I like about this is that it can be easily dropped into a job scheduler (e.g.- Jenkins) and then you’ve got a way to routinely check (and correct) all the configuration settings of the SQL instances that you monitor.

Thanks for reading!


The DBA Who Came In From The Cold

I’m Andrew Pruski and I am a SQL Server DBA with 5 years experience in database development and administration.

The online SQL Server community has helped me out immensely throughout my career, whether from providing reference material in blog posts, or answering my (sometimes obscure) questions on forums. So, to try and say thank you, I would like to contribute my own experiences in the hope that they could benefit someone out there.

So here’s my general ramblings and thoughts about working as a SQL Server DBA.

You can find me on twitter @DBAFromTheCold

If you have any feedback on my blog please send me an email to dbafromthecold@gmail.com.

Comments

Leave a comment on the original post [dbafromthecold.com, opens in a new window]

Loading comments...