Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

60


Part I: Laying the Foundations


Installing SQL Server 2012 Through PowerShell
You can also use PowerShell to perform unattended installs. Simple PowerShell scripts
can be written to execute SQL Server 2012 Setup through its command-line interface. For
example, you can execute the command-line script used in the previous section from the
command line as follows:

$cmd = "d:\setup.exe /ACTION=install /Q /INSTANCENAME="MSSQLSERVER"
/IACCEPTSQLSERVERLICENSETERMS
/FEATURES=SQLENGINE,SSMS
/SQLSYSADMINACCOUNTS="YourDomain\Administrators";
Invoke-Expression -command $cmd | out-null;

More complex PowerShell scripts can be written for larger SQL Server 2012 deployments.
A common approach is the use of PowerShell functions that accept the setup parameters
necessary to perform unattended installations. These PowerShell functions are then exe-
cuted in batches or inside a process that loops through a list of server names with
corresponding parameters.

For example, a PowerShell function can be saved in a PowerShell script fi le and called along
with setup parameters to perform a large scale unattended deployment of SQL Server 2012.
Listing 3-1 provides an example of a PowerShell function that you can use for SQL Server
2012 unattended installations.

LISTING 3-1 Install-Sql2012.ps1

The PowerShell function contained in the Install-SQL2012.ps1 script fi le (included in
the downloads on this book’s website http://www.wiley.com/go/sql2012bible) is as follows:

Function Install-Sql2012
{
param
(
[Parameter(Position=0,Mandatory=$false)][string] $Path,
[Parameter(Position=1,Mandatory=$false)][string] $InstanceName =
"MSSQLSERVER",
[Parameter(Position=2,Mandatory=$false)][string] $ServiceAccount,
[Parameter(Position=3,Mandatory=$false)][string] $ServicePassword,
[Parameter(Position=4,Mandatory=$false)][string] $SaPassword,
[Parameter(Position=5,Mandatory=$false)][string] $LicenseKey,
[Parameter(Position=6,Mandatory=$false)][string] $SqlCollation =
"SQL_Latin1_General_CP1_CI_AS",
[Parameter(Position=7,Mandatory=$false)][switch] $NoTcp,
[Parameter(Position=8,Mandatory=$false)][switch] $NoNamedPipes
)
#Build the setup command using the install mode
if ($Path -eq $null -or $Path -eq "")
{
#No path means that the setup is in the same folder

c03.indd 60c03.indd 60 7/30/2012 4:10:07 PM7/30/2012 4:10:07 PM


http://www.it-ebooks.info
Free download pdf