Merge pull request #568 from Darleev/v-danurg/install_sql_express

Add Microsoft SQL Server Express to Windows VMs
This commit is contained in:
Alena Sviridenko
2020-03-30 12:52:20 +03:00
committed by GitHub
6 changed files with 201 additions and 30 deletions

View File

@@ -469,6 +469,14 @@
"{{ template_dir }}/scripts/Installers/Install-MysqlCli.ps1"
]
},
{
"type": "powershell",
"elevated_user": "SYSTEM",
"elevated_password": "",
"scripts":[
"{{ template_dir }}/scripts/Installers/Install-SQLExpress.ps1"
]
},
{
"type": "powershell",
"scripts":[
@@ -730,6 +738,12 @@
"{{ template_dir }}/scripts/Installers/Validate-Bazel.ps1"
]
},
{
"type": "powershell",
"scripts":[
"{{ template_dir }}/scripts/Installers/Validate-SQLExpress.ps1"
]
},
{
"type": "powershell",
"scripts":[

View File

@@ -450,6 +450,14 @@
"{{ template_dir }}/scripts/Installers/Install-MysqlCli.ps1"
]
},
{
"type": "powershell",
"elevated_user": "SYSTEM",
"elevated_password": "",
"scripts":[
"{{ template_dir }}/scripts/Installers/Install-SQLExpress.ps1"
]
},
{
"type": "powershell",
"scripts":[
@@ -741,6 +749,12 @@
"{{ template_dir }}/scripts/Installers/Validate-MysqlCli.ps1"
]
},
{
"type": "powershell",
"scripts":[
"{{ template_dir }}/scripts/Installers/Validate-SQLExpress.ps1"
]
},
{
"type": "powershell",
"scripts":[
@@ -895,4 +909,4 @@
]
}
]
}
}

View File

@@ -20,6 +20,7 @@ Export-ModuleMember -Function @(
'Add-SoftwareDetailsToMarkdown'
'Stop-SvcWithErrHandling'
'Set-SvcWithErrHandling'
'Start-DownloadWithRetry'
'Install-VsixExtension'
'Get-VSExtensionVersion'
'Get-WinVersion'

View File

@@ -161,6 +161,46 @@ Hashtable for service arguments
}
}
function Start-DownloadWithRetry
{
param (
[Parameter(Mandatory)]
[string] $Url,
[Parameter(Mandatory)]
[string] $Name,
[string] $DownloadPath = "${env:Temp}",
[int] $retries = 20
)
$FilePath = Join-Path $DownloadPath $Name
#Default retry logic for the package.
while($retries -gt 0)
{
try
{
Write-Host "Downloading package from: $Url to path $FilePath ."
(New-Object System.Net.WebClient).DownloadFile($Url, $FilePath)
break
}
catch
{
Write-Host "There is an error during package downloading"
$_
$retries--
if ($retries -eq 0)
{
Write-Host "File can't be downloaded. Please try later or check that file exists by url: $Url"
$_
exit 1
}
Write-Host "Waiting 30 seconds before retrying. Retries left: $retries"
Start-Sleep -Seconds 30
}
}
return $FilePath
}
function Install-VsixExtension
{
Param
@@ -177,34 +217,7 @@ function Install-VsixExtension
if (!$InstallOnly)
{
$FilePath = "${env:Temp}\$Name"
while($retries -gt 0)
{
try
{
Write-Host "Downloading $Name..."
(New-Object System.Net.WebClient).DownloadFile($Url, $FilePath)
break
}
catch
{
Write-Host "There is an error during $Name downloading"
$_
$retries--
if ($retries -eq 0)
{
Write-Host "File can't be downloaded"
$_
exit 1
}
Write-Host "Waiting 30 seconds before retrying. Retries left: $retries"
Start-Sleep -Seconds 30
}
}
$FilePath = Start-DownloadWithRetry -Url $Url -Name $Name
}
$ArgumentList = ('/quiet', "`"$FilePath`"")
@@ -311,4 +324,3 @@ function Test-IsWin16
{
(Get-WinVersion) -match "2016"
}

View File

@@ -0,0 +1,78 @@
################################################################################
## File: Install-SQLExpress.ps1
## Desc: Install SQL Express for Windows
################################################################################
Import-Module -Name ImageHelpers -Force;
function Download-FullSQLPackage {
param(
[String]$InstallerPath,
[String]$DownloadPath,
[String]$Arguments = ("/MEDIAPATH=$DownloadPath", "/MEDIATYPE=Core","/Action=Download", "/QUIET")
)
Write-Host "Downloading full package to $DownloadPath..."
$process = Start-Process -FilePath $InstallerPath -ArgumentList $Arguments -Wait -PassThru
$exitCode = $process.ExitCode
if ($exitCode -eq 0)
{
Write-Host -Object "Full SQL Express package has been successfully downloaded to $DownloadPath : ExitCode: $exitCode"
}
else
{
Write-Host -Object "Full package downloading process was unsuccessful. Exit code: $exitCode."
exit $exitCode
}
}
function Unpack-SQLInstaller {
param(
[String]$InstallPath,
[String]$Arguments = ("/Q", "/IACCEPTSQLSERVERLICENSETERMS")
)
Write-Host "Start unpacking procedure to $InstallPath..."
$process = Start-Process -FilePath $InstallPath -ArgumentList $Arguments -Wait -PassThru
$exitCode = $process.ExitCode
# Exit code -2067529716 is added since SQL Unpack procedure returns it on success.
if ($exitCode -eq 0 -or $exitCode -eq -2067529716)
{
Write-Host -Object "SQL installer unpacking has been completed."
}
else
{
Write-Host -Object "SQL installer unpacking was interrupted : $exitCode."
exit $exitCode
}
}
function Start-Installer {
param(
[String]$InstallerPath,
[String]$Arguments = ("/Q", "/IACCEPTSQLSERVERLICENSETERMS", "/Action=Install", "/INSTANCEID=SQL2019", "/INSTANCENAME=SQL2019", "/SECURITYMODE=SQL", "/SAPWD=P@ssword!!", "/TCPENABLED=1")
)
Write-Host "Installating SQL Express..."
$process = Start-Process -FilePath $InstallerPath -ArgumentList $Arguments -Wait -PassThru
$exitCode = $process.ExitCode
if ($exitCode -eq 0)
{
Write-Host -Object "SQL Express has been successfully installed: ExitCode: $exitCode"
}
else
{
Write-Host -Object "Installation procedure was not correctly completed. Exit code: $exitCode."
exit $exitCode
}
}
#Main function
$installerUrl = "https://go.microsoft.com/fwlink/?linkid=866658"
$downloadPath = "C:\SQLInstall"
$setupPath = Join-Path $downloadPath "SQLEXPR_x64_ENU"
#Create directory for temporary files
New-Item -Path $downloadPath -ItemType Directory
Set-Location -Path $downloadPath
$installerPath = Start-DownloadWithRetry -Url $installerUrl -DownloadPath $downloadPath -Name "SQL2019-SSEI-Expr.exe"
Download-FullSQLPackage -InstallerPath $installerPath -DownloadPath $downloadPath
Unpack-SQLInstaller -InstallPath "$setupPath.exe"
$resultPath = Join-Path $setupPath "SETUP.exe"
Start-Installer -InstallerPath $resultPath
#Cleanup folder with installation packages.
Remove-Item $downloadPath -Recurse -Force

View File

@@ -0,0 +1,52 @@
################################################################################
## File: Validate-SQLExpress.ps1
## Desc: Validate Microsoft SQL Express installation
################################################################################
#Parameters for database access
$sqlUser = "sa"
$sqlPassword = "P@ssword!!"
$sqlInstance = "SQL2019"
function Test-SqlConnection {
param(
[Parameter(Mandatory)]
[string]$ServerName,
[Parameter(Mandatory)]
[string]$IntegratedSecurity,
[Parameter(Mandatory)]
[string]$UserName,
[Parameter(Mandatory)]
[string]$Password
)
$ErrorActionPreference = 'Stop'
try {
$connectionString = 'Server={0};Integrated Security={1};User ID={2};Password={3}' -f $ServerName,$IntegratedSecurity,$UserName,$Password
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $connectionString
$sqlConnection.Open()
Write-Host -Object "Connection to SQL Express was successful."
return $sqlConnection.ServerVersion
} catch {
Write-Host -Object "Connection to SQL Express cannot be established."
exit 1
} finally {
## Close the connection when we're done
$sqlConnection.Close()
}
}
$instanceName = "$env:computername\$sqlInstance"
$version = Test-SqlConnection -ServerName $instanceName -IntegratedSecurity "false" -UserName $sqlUser -Password $sqlPassword
# Adding description of the software to Markdown
$SoftwareName = "Git"
$Description = @"
_Version:_ $version<br/>
_InstanceName:_ $sqlInstance<br/>
_Username:_ $sqlUser<br/>
_Password:_ $sqlPassword<br/>
_Default Path:_ C:\Program Files (x86)\Microsoft SQL Server
"@
Add-SoftwareDetailsToMarkdown -SoftwareName $SoftwareName -DescriptionMarkdown $Description