
Applies to SQL Server 2016 to 2025. The scripts need the dbatools module and Pester 4, and run from an admin machine against the new server.
A SQL Server install usually fails for a boring reason: a drive that isn’t there, a service account password that’s wrong, a media share the server can’t see. And when an install does succeed, very few people check the new instance against the build standard afterwards. This kit wraps dbatools’ Install-DbaInstance in Pester tests on both sides. Before it installs anything, it proves the server is ready. Afterwards, it proves the instance was configured the way it should be.
The kit started from the example scripts that come with Deploying and Managing SQL Server with dbatools. This post covers how it works, the second data centre’s settings file I added, and the bugs I fixed on the way. The code shown here is my own; for the original scripts, go to the source material.
What it does
- Pre-install tests. The target server answers on WinRM. The engine, Agent and installation accounts are valid in Active Directory, so a mistyped password fails here instead of halfway through setup. The required drives exist. The media and update folders can be reached.
- The install.
Install-DbaInstancewith the drive layout from the settings file, instant file initialisation switched on, the latest cumulative update applied during setup, and the DBA group as sysadmin. - Configuration. High Performance power plan, MAXDOP and max server memory set to the dbatools recommendations, tempdb files sized and placed, Ola Hallengren’s maintenance solution with its jobs, sp_WhoIsActive, sa disabled, trace flag 3226 (plus 1117 and 1118 before 2016), the DAC, optimize for ad hoc workloads, cost threshold for parallelism at 50, model set to simple recovery with 512 MB growth, Database Mail and Agent mail alerts.
- Post-install tests. The Agent is running and keeps enough history, Database Mail is set up, the maintenance jobs exist and have run, SPNs are correct, the DBA group is sysadmin, sa is disabled, the trace flags are set, max memory matches the recommendation, MAXDOP doesn’t span a NUMA node, tempdb has eight equal fixed-growth files off the system drive, model’s settings are right, and the power plan is High Performance.
How it fits together
Install-SqlServer.ps1is the one you run. It loads the settings for a data centre, runs the pre-install tests, installs, configures and runs the post-install tests.Import-EnvironmentSettings.ps1takes-DataCenter DC1orLABand loads that data centre’s settings file.Import-Dc1EnvironmentSettings.ps1andImport-LabEnvironmentSettings.ps1hold each data centre’s drive paths, media and update folders, mail settings and DBA group.Test-PreInstallationChecks.ps1andTest-PostInstallationChecks.ps1are the Pester tests.Test-AdCredential.ps1checks an account against Active Directory; it credits techibee.com for the approach.Invoke-SqlConfigure.ps1does the configuration after the install.
The DC1 settings file
Import-EnvironmentSettings.ps1 already knew about a DC1 data centre, but its settings file had never been written, so every DC1 build failed on the first line. This is the file I added. It uses the same drive layout and media folders as my unattended build script, and it reads the install share from that kit’s settings file, so the share is named in one place and in no script.
#DC1 environment settings. Loaded by Import-EnvironmentSettings.ps1 -DataCenter DC1.
#The install share is read from SqlBuild.settings.psd1 (the same file the SQL build kit uses),
#so the share name is not in this script. Copy that file into this folder.
#Domain information
$ActiveDirectoryDomain = $env:USERDOMAIN
$NumberOfPhysicalCoresPerCPU = 8 # MAXDOP ceiling the post-install checks allow; set to the cores per NUMA node
#Drive Path Information (same layout as the SQL build kit)
$InstancePath = 'S:\MSSQL'
$DataPath = 'D:\MSSQL\Data'
$LogPath = 'L:\MSSQL\Logs'
$TempPath = 'T:\MSSQL\TempDB'
$BackupPath = 'F:\MSSQL\Backup' # F: is one of the drives Test-PreInstallationChecks requires
#Log file
$logfile = "output.log"
#Database Mail for alerts. Fill these in: Database Mail is skipped while they are empty.
$MailServer = ''
$MailFromAddress = ''
#DBA group: made sysadmin at install, and checked by the post-install tests
$DbEngineers = "$ActiveDirectoryDomain\SQLDBA"
#Edition to install. Must match a media folder on the share called "SQL <Version> <Edition>".
$Edition = 'Standard'
#Location of the install share
$SettingsFile = Join-Path $PSScriptRoot 'SqlBuild.settings.psd1'
if (-not (Test-Path -LiteralPath $SettingsFile)) {
throw "DC1 settings: $SettingsFile not found. Copy SqlBuild.settings.psd1 from the SQL build kit into this folder."
}
$InstallRoot = (Import-PowerShellDataFile -LiteralPath $SettingsFile).SharePath
if (-not $InstallRoot) {
throw "DC1 settings: SharePath is not set in $SettingsFile."
}
$InstallRoot = $InstallRoot.TrimEnd('\')
#Location of installation files for each version of SQL Server: the build kit's media folders.
$InstallationSources = @{
2016 = "$InstallRoot\SQL 2016 $Edition"
2017 = "$InstallRoot\SQL 2017 $Edition"
2019 = "$InstallRoot\SQL 2019 $Edition"
2022 = "$InstallRoot\SQL 2022 $Edition"
2025 = "$InstallRoot\SQL 2025 $Edition"
}
#Location of update sources for each version of SQL Server. The build kit keeps the latest CU
#(SQLServer<Version>-KB<number>-x64.exe) in the media folder, so it is the same folder.
$UpdateSources = @{}
foreach ($v in $InstallationSources.Keys) { $UpdateSources[$v] = $InstallationSources[$v] }
What I fixed
A failed pre-install test didn’t stop the install. The script reported the failure like this:
Write-Output "FAILED: Preflight checks failed please ensure pester test passes" -ErrorAction Stop
-ErrorAction Stop only matters when a command raises an error, and Write-Output never does. So the script printed FAILED and carried straight on with the install, which defeats the point of testing first. The same pattern followed a failed install. Both now throw:
throw "FAILED: Preflight checks failed. Nothing has been installed; fix the failed tests and run again."
The post-install tests couldn’t see their own settings. Pester runs a test file in its own scope, so the MAXDOP limit and the DBA group from the settings file never reached the tests. The MAXDOP test was comparing against nothing, and the sysadmin test was looking for a group with no name. Both values are now parameters of the test script, passed in by Install-SqlServer.ps1:
Invoke-Pester -Script @{ Path = '.\Test-PostInstallationChecks.ps1' ; Parameters = @{ SqlInstance = $SqlInstance; DbEngineers = $DbEngineers; NumberOfPhysicalCoresPerCPU = $NumberOfPhysicalCoresPerCPU } }
The DBA group was never made sysadmin. Install-DbaInstance makes whoever runs the install the sysadmin unless you tell it otherwise, so the sysadmin test could never pass. The install now passes AdminAccount = $DbEngineers.
Trace flags 1117 and 1118 went on every version. They’re only needed before SQL Server 2016, and the check used a $SqlVersion variable that was never set. An unset variable is less than 2016, so they were always added. The check now reads the instance’s own major version:
$VersionMajor = (Connect-DbaInstance -SqlInstance "$SqlInstance\$InstanceName").VersionMajor
if ($VersionMajor -lt 13 ) {
tempdb ignored the settings file. Its paths were typed into the configure script as T:\TEMPDB and L:\LOGS, which only matched the lab. It now uses $TempPath and $LogPath.
Database Mail pointed at the example’s own mail server. The mail server and sender address were written into the configure script. They’re now $MailServer and $MailFromAddress in each settings file, and while they’re empty the script warns and skips Database Mail rather than setting up an account that can’t send. The post-install Database Mail test then fails, so it can’t be missed.
The service account password was in the script. It was a plain-text string in Install-SqlServer.ps1. It’s now asked for when the script runs:
$svcPassword = Read-Host -AsSecureString -Prompt "Password for service account $ActiveDirectoryDomain\$ServiceAccount"
The lab settings file was cut off. It ended partway through its table of update folders, with no closing brace, so every LAB build failed when it loaded its settings.
Running it
- Copy the kit and
SqlBuild.settings.psd1into one folder on an admin machine with dbatools and Pester 4 installed. - Fill in
$MailServerand$MailFromAddressin the settings file, and check the drive paths, edition and MAXDOP limit. - In
Install-SqlServer.ps1, set$Environment,$Versionand$SqlInstancefor this build. - Run
.\Install-SqlServer.ps1from that folder. It asks for the service account password and your own credentials, then works through the tests, the install and the configuration.
Watch out for
- Pester 4, not 5.
Invoke-Pester -Script @{...}is Pester 4 syntax. Pester 5 removed it, so install the older version alongside:Install-Module Pester -RequiredVersion 4.10.1 -SkipPublisherCheck. - The build details are set in the script. Version, instance name and data centre are variables at the top of
Install-SqlServer.ps1, not parameters, so check them before every run. - The lab MAXDOP limit is 1. The lab settings set
$NumberOfPhysicalCoresPerCPU = 1, but the configure step sets the recommended MAXDOP, so the MAXDOP test fails on any lab server with more than one core. Set it to your lab’s cores per NUMA node. - Two configure functions are never called. One sets the page file using
Set-PageFile, which isn’t a dbatools command; the other adds a management group to local administrators. Neither runs, so don’t expect either to happen. - Not yet run end to end. All eight scripts parse with no errors. The settings files load and produce the right paths, and the trace flag, Database Mail and tempdb changes were checked with dbatools stubbed out. The full build hasn’t yet been run against a Windows server, so do that on a test VM first.
Where it fits
This is one of three ways I build and configure SQL Server. The Projects page also covers the unattended build script, which runs on the new server itself and patches Windows as well, and the post-install configuration script in T-SQL with its rollback. Pick this kit when you want every build proved by tests before and after.
Clive Roberts DBA