I would like to begin by expressing my enthusiasm for dbatools, a remarkable tool for database administrators (DBAs). If you haven’t already, I highly recommend familiarizing yourself with it by visiting their website: https://dbatools.io/.
In this blog post, I will be sharing a few scripts that can be used to perform a seamless SQL installation using the powerful commands provided by dbatools.
check if system needs a reboot or not
Get-WUIsPendingReboot
First, check if SQL Server is already installed
$SqlInstance = Find-DbaInstance -ComputerName localhost
Set the path to the install files (lets say you have kept the binaries in E:\abhi folder)
Set-DbatoolsConfig -Name Path.SQLServerSetup -Value ‘E:\abhi\SQL_Server_2019-Standard’
create sqlserviceaccount account which will run the SQL services and make it OS admin
$Username3 = “sqlserviceaccount”
$Password3 = “Password”
$SecurePassword3 = ConvertTo-SecureString -String $Password3 -AsPlainText -Force
$User3 = New-LocalUser -Name $Username3 -Password $SecurePassword3 -PasswordNeverExpires
Add sqlserviceaccount user to local administrators group
$Group3 = “Administrators”
Add-LocalGroupMember -Group $Group3 -Member $Username3
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 with providing details like data , log file path , sa credential, mixed mode authentication
Install-DbaInstance -Version $Version -AuthenticationMode Mixed -Feature Engine -SaCredential $sacredential -InstancePath “F:\SQLDATA” -DataPath “F:\SQLDATA” -LogPath “L:\SQLLOGS” -TempPath “T:\TEMPDB” -BackupPath “J:\SQLBACKUPS” -Port 1440 -PerformVolumeMaintenanceTasks -Confirm:$false -enginecredential “sqlserviceaccount” -agentcredential “sqlserviceaccount” -restart
}
Keep learning!!