How I use dbatools to automate SQL Server installs

I lot of you have asked me to expand on the automated SQL Server installation I mentioned in my previous article:

https://marcin.gminski.net/blog/why-i-use-vmware-workstation-instead-of-hyper-v-on-my-laptop/

dbatools is a free PowerShell module with over 500 SQL Server best practice, administration, development and migration commands included

https://dbatools.io/

The dbatools team have spent long hours making sure everything is super simple and almost a one-liner therefore there is not that much I had to do.

First, check if SQL Server is already installed:

$SqlInstance = Find-DbaInstance -ComputerName localhost

Set the path to the install files:

Set-DbatoolsConfig -Name Path.SQLServerSetup -Value '\vmware-hostShared FoldersSQL Server ISO'

And run the installation:

if ($SqlInstance -eq $null) {
   
   //ASK which version of SQL to install:
   [string]$Version = Read-Host -Prompt 'Install SQL Server Version (2008R2, 2012, 2014, 2016, 2019)'

   //Get SA credentials:
   $sacredential = Get-Credential
   
   //Install SQL Server:
   Install-DbaInstance -Version $Version -AuthenticationMode Mixed -Feature Engine,IntegrationServices -SaCredential $sacredential -Restart -DataPath "S:SQLDATA" -LogPath "L:SQLLOGS" -TempPath "T:TEMPDB" -BackupPath "R:SQLBACKUPS" -PerformVolumeMaintenanceTasks -Confirm:$false
}

The script will run on startup and if SQL Server is not found, it will install it. The whole process takes less than a couple of minutes.

You can learn more about the command here : https://docs.dbatools.io/#Install-DbaInstance

Thanks for reading!

This post was originally published on June 8, 2020.

Help us grow, please share this content!

Author

Posted by
Marcin Gminski
August 17, 2021

Let us help you. Get in touch.

Help us grow, please share this content!