Files

91 lines
4.5 KiB
PowerShell

# Installs SQL Server 2016 as the default instance on TCP 1433, then creates the project's databases at the
# configured compatibility level, with an application login that owns them. Settings in C:\setup\settings.ps1.
$ErrorActionPreference = "Stop"
. C:\setup\settings.ps1
function Find-DeveloperSetup {
Get-PSDrive -PSProvider FileSystem | ForEach-Object {
$setup = Join-Path $_.Root "setup.exe"
if ((Test-Path $setup) -and (Test-Path (Join-Path $_.Root "x64"))) { return $setup }
}
}
function Get-ExpressSetup {
$media = "C:\setup\media"
New-Item -ItemType Directory -Force $media | Out-Null
$package = Get-ChildItem $media -Filter "SQLEXPR*_x64_*.exe" -Recurse | Select-Object -First 1
if (-not $package) {
$ssei = Get-ChildItem C:\setup -Filter "SQLServer2016-SSEI-Expr*.exe" | Select-Object -First 1
if (-not $ssei) { throw "no SQL Server media: neither a Developer disc nor SQLServer2016-SSEI-Expr.exe" }
Write-Host "downloading the Express media with $($ssei.Name)"
$p = Start-Process $ssei.FullName -ArgumentList "/ACTION=Download", "/MEDIAPATH=$media", "/MEDIATYPE=Core", "/QUIET" -Wait -PassThru
if ($p.ExitCode -ne 0) { throw "the Express web installer exited with $($p.ExitCode)" }
$package = Get-ChildItem $media -Filter "SQLEXPR*_x64_*.exe" -Recurse | Select-Object -First 1
}
$extracted = "C:\setup\sqlsetup"
$p = Start-Process $package.FullName -ArgumentList "/q", "/x:$extracted" -Wait -PassThru
if ($p.ExitCode -ne 0) { throw "extracting $($package.Name) exited with $($p.ExitCode)" }
Join-Path $extracted "setup.exe"
}
$setup = Find-DeveloperSetup
if (-not $setup) { $setup = Get-ExpressSetup }
Write-Host "installing with $setup"
$arguments = @(
"/Q", "/ACTION=Install", "/IACCEPTSQLSERVERLICENSETERMS", "/UPDATEENABLED=False",
"/FEATURES=SQLENGINE", "/INSTANCENAME=MSSQLSERVER",
"/SQLSYSADMINACCOUNTS=`"BUILTIN\Administrators`"",
"/SECURITYMODE=SQL", "/SAPWD=`"$SaPassword`"",
"/SQLCOLLATION=$Collation", "/TCPENABLED=1", "/NPENABLED=0"
)
$p = Start-Process $setup -ArgumentList $arguments -Wait -PassThru
if ($p.ExitCode -notin 0, 3010) {
throw "SQL Server setup exited with $($p.ExitCode); see C:\Program Files\Microsoft SQL Server\130\Setup Bootstrap\Log\Summary.txt"
}
Restart-Service MSSQLSERVER -Force
# A service pack next to the scripts (the Express installer still online is SP2's) brings the instance to it.
$update = Get-ChildItem C:\setup -Filter "SQLServer2016SP*-x64-*.exe" | Select-Object -First 1
if ($update) {
Write-Host "applying $($update.Name)"
$p = Start-Process $update.FullName -ArgumentList "/quiet", "/IAcceptSQLServerLicenseTerms", "/Action=Patch", "/AllInstances" -Wait -PassThru
if ($p.ExitCode -notin 0, 3010) {
throw "$($update.Name) exited with $($p.ExitCode); see C:\Program Files\Microsoft SQL Server\130\Setup Bootstrap\Log\Summary.txt"
}
Restart-Service MSSQLSERVER -Force
}
function Invoke-Sql([string]$sql, [string]$database = "master") {
$connection = New-Object System.Data.SqlClient.SqlConnection(
"Server=127.0.0.1,1433;Database=$database;User ID=sa;Password=$SaPassword;Connect Timeout=30")
$connection.Open()
try {
$command = $connection.CreateCommand()
$command.CommandText = $sql
$reader = $command.ExecuteReader()
$table = New-Object System.Data.DataTable
$table.Load($reader)
return ,$table
} finally { $connection.Close() }
}
$login = $AppLogin.Replace("]", "]]")
$password = $AppPassword.Replace("'", "''")
Invoke-Sql "IF SUSER_ID(N'$login') IS NULL CREATE LOGIN [$login] WITH PASSWORD = N'$password', CHECK_POLICY = OFF;" | Out-Null
foreach ($name in $Databases) {
Invoke-Sql @"
IF DB_ID(N'$name') IS NULL CREATE DATABASE [$name] COLLATE $Collation;
ALTER DATABASE [$name] SET COMPATIBILITY_LEVEL = $CompatibilityLevel;
"@ | Out-Null
Invoke-Sql @"
IF USER_ID(N'$login') IS NULL CREATE USER [$login] FOR LOGIN [$login];
ALTER ROLE db_owner ADD MEMBER [$login];
"@ $name | Out-Null
}
$server = Invoke-Sql "SELECT @@VERSION AS version, SERVERPROPERTY('Edition') AS edition, SERVERPROPERTY('ProductLevel') AS level, SERVERPROPERTY('ProductVersion') AS build, SERVERPROPERTY('Collation') AS collation;"
$databases = Invoke-Sql "SELECT name, compatibility_level, collation_name FROM sys.databases ORDER BY name;"
($server | Format-List | Out-String) + ($databases | Format-Table | Out-String) | Set-Content C:\setup\version.txt
Get-Content C:\setup\version.txt