Last active
October 28, 2024 07:08
-
-
Save robdmoore/c22a9d305a0c16bf21b2 to your computer and use it in GitHub Desktop.
Automated install of SQL Express from commandline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If you want to give owner access to the whole server to a particular user account this is how | |
exec sp_addrolemember 'db_owner', 'NT AUTHORITY\NETWORK SERVICE'; | |
GO | |
# If you want to idempotently ensure a particular database with name DatabaseName exists this is how | |
IF NOT db_id('DatabaseName') IS NOT NULL BEGIN | |
PRINT 'Creating database...' | |
CREATE DATABASE [DatabaseName] | |
PRINT 'Created database.' | |
END | |
GO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Install-SQLServerExpress2014AndManagementStudio() { | |
if (-not (Test-Path "C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS")) { | |
if (-not (Test-Path "C:\tempsql\SQLEXPRWT_x64_ENU.exe")) { | |
Write-Output "Downloading SQL Server 2014 Express: this may take a while`r`n" | |
mkdir c:\tempsql -Force | |
(New-Object Net.WebClient).DownloadFile('http://download.microsoft.com/download/E/A/E/EAE6F7FC-767A-4038-A954-49B8B05D04EB/ExpressAndTools%2064BIT/SQLEXPRWT_x64_ENU.exe','C:\tempsql\SQLEXPRWT_x64_ENU.exe') | |
} | |
Write-Output "Unpacking SQL Server 2014 Express`r`n" | |
C:\tempsql\SQLEXPRWT_x64_ENU.exe /x:"c:\tempsql\SQLEXPRWT_x64_ENU" /u | Out-Null | |
Write-Output "Adding .NET 3.5`r`n" | |
Import-Module Servermanager | |
Add-WindowsFeature NET-Framework-Core | |
Write-Output "Installing SQL Server 2014 Express: this may take a while`r`n" | |
C:\tempsql\SQLEXPRWT_x64_ENU\setup.exe /QUIETSIMPLE /ACTION=install /FEATURES=SQL,Tools /IAcceptSQLServerLicenseTerms /INSTANCENAME="SQLExpress" | |
$env:PATH = [System.Environment]::GetEnvironmentVariable("Path","Machine") | |
Remove-Item -Recurse -Force c:\tempsql | |
} else { | |
Write-Output "SQL Server 2014 Express already installed`r`n" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module (Join-Path $PSScriptRoot sqlexpress.psm1) -Force | |
Install-SQLServerExpress2014AndManagementStudio | |
& sqlcmd -S ".\SQLEXPRESS" -E -i "$PsScriptRoot\setup.sql" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternatively, you can use this which doesn't also install management studio (and the download drops from over 800MB to just under 200MB so the install is a lot faster):