Checking SQL Configuration with Pester & Dbatools

Andrew Pruski, 2017-12-13 (first published: 2017-11-29)

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*' `
            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: –

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!





Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

Robert Davis


1,567 reads

Networking – Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I’d like to talk about social networking. We’ll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let…

Andy Warren


1,530 reads

Speaking at Community Events – More Thoughts

Last week I posted Speaking at Community Events – Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I’ve got a few more thoughts on the topic this week, and I look forward to your comments.

Andy Warren


360 reads