2020-03-17 21:47:47 +07:00
function Start-Task {
param (
[ String ] $InstallPath ,
2020-03-17 21:54:40 +07:00
[ String ] $Arguments ,
[ String ] $SuccessMessage
2020-03-17 21:47:47 +07:00
)
$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 -2067529716 )
{
2020-03-17 21:54:40 +07:00
Write-Host -Object " $SuccessMessage : ExitCode: $exitCode "
2020-03-17 21:47:47 +07:00
}
else
{
Write-Host -Object "Non zero exit code returned by the installation process : $exitCode ."
exit $exitCode
}
}
function Start-DownloadSQLExpress {
param (
[ String ] $InstallerUrl ,
[ String ] $InstallerPath
)
2020-03-17 21:56:27 +07:00
Write-Host "Download SQL Express web-installer from: $InstallerUrl "
2020-03-17 21:47:47 +07:00
( New-Object System . Net . WebClient ). DownloadFile ( $InstallerUrl , $InstallerPath )
2020-03-17 21:56:27 +07:00
Write-Host "SQL Express has been successfully downladed from the link: $InstallerUrl "
2020-03-17 21:47:47 +07:00
}
2020-03-12 21:22:54 +07:00
$installerUrl = "https://go.microsoft.com/fwlink/?linkid=866658"
2020-03-17 21:47:47 +07:00
$installerPath = " ${env:Temp} \SQL2019-SSEI-Expr.exe"
2020-03-12 21:22:54 +07:00
$downloadPath = "C:\SQLEXPRESS2019"
$setupPath = Join-Path $downloadPath "SQLEXPR_x64_ENU"
$downloadArgs = ( "/MEDIAPATH= $downloadPath " , "/MEDIATYPE=Core" , "/Action=Download" , "/QUIET" )
2020-03-17 21:47:47 +07:00
$unpackArgs = ( "/Q" , "/IACCEPTSQLSERVERLICENSETERMS" )
$installArgs = ( "/Q" , "/IACCEPTSQLSERVERLICENSETERMS" , "/Action=Install" , "/INSTANCEID=SQL2019" , "/INSTANCENAME=SQL2019" , "/SECURITYMODE=SQL" , "/SAPWD=P@ssword!!" , "/TCPENABLED=1" )
#Download installer for SQL Express
Start-DownloadSQLExpress -InstallerUrl $installerUrl -InstallerPath $installerPath
#Download full SQL Express package
2020-03-17 21:56:27 +07:00
Start-Task -InstallPath $installerPath -Arguments $downloadArgs -SuccessMessage "Downloaded full package. To path: $installerPath "
2020-03-17 21:47:47 +07:00
#Unpack SQL Express Installer
2020-03-17 21:54:40 +07:00
Start-Task -InstallPath " $setupPath .exe" -Arguments $unpackArgs -SuccessMessage "Unpacked package to directory: $setupPath "
2020-03-17 21:47:47 +07:00
#Start SQL Express installer silently
2020-03-17 21:54:40 +07:00
Start-Task -InstallPath " $setupPath /SETUP.exe" -Arguments $installArgs -SuccessMessage "SQL has been installed to directory: ${env:ProgramFiles} /Microsoft SQL Server"