Build/Install-SqlExpress.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
################################################################################
## File: Install-SQLExpress.ps1
## Desc: Install SQL Express for Windows
################################################################################
. ./Build/InstallHelpers.ps1

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@ssw0rdl!ng", "/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