Scripting SQL Server Installation

This is where I show that I’m really not a DBA.  I’m an Accidental DBA at best, and a nuisance at worst.  SQL Server already has the capability to be installed via command line parameters or a configuration file.  If I setup the machine the same way every single time, this would be fine, but since I need to change the placement of certain file types, I need to have the ability to change on the fly.

Let’s start with the configuration file.  This is the INI file that is created when you run the SQL Server installation wizard.

;SQL Server 2016 Configuration File

[OPTIONS]

; Specifies a Setup work flow, like INSTALL, UNINSTALL, or UPGRADE. This is a required parameter. 

ACTION="Install"

; Specifies that SQL Server Setup should not display the privacy statement when ran from the command line. 

SUPPRESSPRIVACYSTATEMENTNOTICE="True"

; By specifying this parameter and accepting Microsoft R Open and Microsoft R Server terms, you acknowledge that you have read and understood the terms of use. 

IACCEPTROPENLICENSETERMS="False"

; Use the /ENU parameter to install the English version of SQL Server on your localized Windows operating system. 

ENU="True"

; Setup will not display any user interface. 

QUIET="False"

; Setup will display progress only, without any user interaction. 

QUIETSIMPLE="True"

; Parameter that controls the user interface behavior. Valid values are Normal for the full UI,AutoAdvance for a simplied UI, and EnableUIOnServerCore for bypassing Server Core setup GUI block. 

;UIMODE="Normal"

; Specify whether SQL Server Setup should discover and include product updates. The valid values are True and False or 1 and 0. By default SQL Server Setup will include updates that are found. 

UpdateEnabled="True"

; If this parameter is provided, then this computer will use Microsoft Update to check for updates. 

USEMICROSOFTUPDATE="True"

; Specifies features to install, uninstall, or upgrade. The list of top-level features include SQL, AS, RS, IS, MDS, and Tools. The SQL feature will install the Database Engine, Replication, Full-Text, and Data Quality Services (DQS) server. The Tools feature will install shared components. 

FEATURES=SQLENGINE,FULLTEXT

; Specify the location where SQL Server Setup will obtain product updates. The valid values are "MU" to search Microsoft Update, a valid folder path, a relative path such as .\MyUpdates or a UNC share. By default SQL Server Setup will search Microsoft Update or a Windows Update service through the Window Server Update Services. 

UpdateSource="MU"

; Displays the command line parameters usage 

HELP="False"

; Specifies that the detailed Setup log should be piped to the console. 

INDICATEPROGRESS="False"

; Specifies that Setup should install into WOW64. This command line argument is not supported on an IA64 or a 32-bit system. 

X86="False"

; Specify a default or named instance. MSSQLSERVER is the default instance for non-Express editions and SQLExpress for Express editions. This parameter is required when installing the SQL Server Database Engine (SQL), Analysis Services (AS), or Reporting Services (RS). 

INSTANCENAME="MSSQLSERVER"

; Specify the root installation directory for shared components.  This directory remains unchanged after shared components are already installed. 

INSTALLSHAREDDIR="E:\Program Files\Microsoft SQL Server"

; Specify the root installation directory for the WOW64 shared components.  This directory remains unchanged after WOW64 shared components are already installed. 

INSTALLSHAREDWOWDIR="E:\Program Files (x86)\Microsoft SQL Server"

; Specify the Instance ID for the SQL Server features you have specified. SQL Server directory structure, registry structure, and service names will incorporate the instance ID of the SQL Server instance. 

INSTANCEID="MSSQLSERVER"

; TelemetryUserNameConfigDescription 

SQLTELSVCACCT="NT Service\SQLTELEMETRY"

; TelemetryStartupConfigDescription 

SQLTELSVCSTARTUPTYPE="Automatic"

; Specify the installation directory. 

INSTANCEDIR="G:\Program Files\Microsoft SQL Server"

; Agent account name 

AGTSVCACCOUNT="NT AUTHORITY\NETWORK SERVICE"

; Auto-start service after installation.  

AGTSVCSTARTUPTYPE="Automatic"

; CM brick TCP communication port 

COMMFABRICPORT="0"

; How matrix will use private networks 

COMMFABRICNETWORKLEVEL="0"

; How inter brick communication will be protected 

COMMFABRICENCRYPTION="0"

; TCP port used by the CM brick 

MATRIXCMBRICKCOMMPORT="0"

; Startup type for the SQL Server service. 

SQLSVCSTARTUPTYPE="Automatic"

; Level to enable FILESTREAM feature at (0, 1, 2 or 3). 

FILESTREAMLEVEL="0"

; Set to "1" to enable RANU for SQL Server Express. 

ENABLERANU="False"

; Specifies a Windows collation or an SQL collation to use for the Database Engine. 

SQLCOLLATION="SQL_Latin1_General_CP1_CI_AS"

; Account for SQL Server service: Domain\User or system account. 

SQLSVCACCOUNT="NT AUTHORITY\NETWORK SERVICE"

; Set to "True" to enable instant file initialization for SQL Server service. If enabled, Setup will grant Perform Volume Maintenance Task privilege to the Database Engine Service SID. This may lead to information disclosure as it could allow deleted content to be accessed by an unauthorized principal. 

SQLSVCINSTANTFILEINIT="True"

; Windows account(s) to provision as SQL Server system administrators. 

SQLSYSADMINACCOUNTS=".\Administrator"

; The default is Windows Authentication. Use "SQL" for Mixed Mode Authentication. 

SECURITYMODE="SQL"

; The number of Database Engine TempDB files. 

SQLTEMPDBFILECOUNT="8"

; Specifies the initial size of a Database Engine TempDB data file in MB. 

SQLTEMPDBFILESIZE="64"

; Specifies the automatic growth increment of each Database Engine TempDB data file in MB. 

SQLTEMPDBFILEGROWTH="128"

; Specifies the initial size of the Database Engine TempDB log file in MB. 

SQLTEMPDBLOGFILESIZE="32"

; Specifies the automatic growth increment of the Database Engine TempDB log file in MB. 

SQLTEMPDBLOGFILEGROWTH="64"

; Default directory for the Database Engine backup files. 

SQLBACKUPDIR="I:\Backups"

; Default directory for the Database Engine user databases. 

SQLUSERDBDIR="G:\Data"

; Default directory for the Database Engine user database logs. 

SQLUSERDBLOGDIR="H:\Logs"

; Directories for Database Engine TempDB files. 

SQLTEMPDBDIR="F:\TempDB"

; Provision current user as a Database Engine system administrator for %SQL_PRODUCT_SHORT_NAME% Express. 

; ADDCURRENTUSERASSQLADMIN="True"

; Specify 0 to disable or 1 to enable the TCP/IP protocol. 

TCPENABLED="1"

; Specify 0 to disable or 1 to enable the Named Pipes protocol. 

NPENABLED="0"

; Startup type for Browser Service. 

BROWSERSVCSTARTUPTYPE="Automatic"

; Add description of input argument FTSVCACCOUNT 

FTSVCACCOUNT="NT Service\MSSQLFDLauncher"

This one has already been tweaked for my current build from my previous scripts, but suppose that I wanted to make changes.  It would be pretty easy with the Get-Content and Out-File functions if I wanted to just do simple line substitution.  However, I remembered seeing something in the past on the Scripting Guys Blog about working with INI files and thinking of them as hashtables.  So I went in search and found two functions: Get-IniContent and Out-IniFile.

Here’s how I script its use in the lab.

Let’s say that I want to use the C:\Data location for some files instead of the place configured in the INI file.

# Pre-load the scripts
. "\\demo.lab\files\Scripts\_functions\func_Get-IniContent.ps1"
. "\\demo.lab\files\Scripts\_functions\func_Out-IniFile.ps1"

$IniFile = Get-IniContent -FilePath "\\demo.lab\files\Data\SQL_Install_ConfigurationFile.ini"
$IniFile.OPTIONS.SQLUSERDBDIR    = '"C:\Data"'
$IniFile.OPTIONS.SQLBACKUPDIR    = '"C:\Data"'
$IniFile.OPTIONS.SQLUSERDBLOGDIR = '"C:\Data"'
$IniFile | Out-IniFile -FilePath "\\demo.lab\files\Data\stuff_and_junk.ini"

So what’s that do to the file?  Here’s what I get (omitting the comments):

[OPTIONS]
ACTION="Install"
AGTSVCACCOUNT="NT AUTHORITY\NETWORK SERVICE"
AGTSVCSTARTUPTYPE="Automatic"
BROWSERSVCSTARTUPTYPE="Automatic"
COMMFABRICENCRYPTION="0"
COMMFABRICNETWORKLEVEL="0"
COMMFABRICPORT="0"
ENABLERANU="False"
ENU="True"
FEATURES=SQLENGINE,FULLTEXT
FILESTREAMLEVEL="0"
FTSVCACCOUNT="NT Service\MSSQLFDLauncher"
HELP="False"
IACCEPTROPENLICENSETERMS="False"
INDICATEPROGRESS="False"
INSTALLSHAREDDIR="E:\Program Files\Microsoft SQL Server"
INSTALLSHAREDWOWDIR="E:\Program Files (x86)\Microsoft SQL Server"
INSTANCEDIR="G:\Program Files\Microsoft SQL Server"
INSTANCEID="MSSQLSERVER"
INSTANCENAME="MSSQLSERVER"
MATRIXCMBRICKCOMMPORT="0"
NPENABLED="0"
QUIET="False"
QUIETSIMPLE="True"
SECURITYMODE="SQL"
SQLBACKUPDIR="C:\Data"
SQLCOLLATION="SQL_Latin1_General_CP1_CI_AS"
SQLSVCACCOUNT="NT AUTHORITY\NETWORK SERVICE"
SQLSVCINSTANTFILEINIT="True"
SQLSVCSTARTUPTYPE="Automatic"
SQLSYSADMINACCOUNTS=".\Administrator"
SQLTELSVCACCT="NT Service\SQLTELEMETRY"
SQLTELSVCSTARTUPTYPE="Automatic"
SQLTEMPDBDIR="F:\TempDB"
SQLTEMPDBFILECOUNT="8"
SQLTEMPDBFILEGROWTH="128"
SQLTEMPDBFILESIZE="64"
SQLTEMPDBLOGFILEGROWTH="64"
SQLTEMPDBLOGFILESIZE="32"
SQLUSERDBDIR="C:\Data"
SQLUSERDBLOGDIR="C:\Data"
SUPPRESSPRIVACYSTATEMENTNOTICE="True"
TCPENABLED="1"
UpdateEnabled="True"
UpdateSource="MU"
USEMICROSOFTUPDATE="True"
X86="False"

So, we’ve got the SQL Server INI File ready, but how do we use it?  Thankfully, Microsoft documented that very well.

All I need is a few parameters and the path to the executable.

$FilePath = "\\demo.lab\files\Applications\Microsoft\SQL Server\2017\Enterprise\setup.exe"
$ArgumentList =  @()
$ArgumentList += '/SAPWD="PorkchopExpress"'
$ArgumentList += '/ConfigurationFile="\\demo.lab\files\Data\stuff_and_junk.ini"'
$ArgumentList += '/IAcceptSqlServerLicenseTerms'
Start-Process -FilePath $FilePath -ArgumentList $ArgumentList -Wait

Pretty cool, right?  That’s one of the best things about PowerShell.  It’s about sharing.  I didn’t have to write these two functions.  Someone has already done that and shared that information.

Ramble on friends and enjoy the holidays!

1 thought on “Scripting SQL Server Installation”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.