Functions/GetLocalDb2016NuGet.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
Function Get-LocalDb2016NuGet {
    <#
.SYNOPSIS
Get Microsoft SQL Server 2016 LocalDB from Nuget.
.DESCRIPTION
Downloads Microsoft SQL Server 2016 LocalDB from Nuget. This Nuget package is not official release from Microsoft.
Before we use Nuget, we use the function "Install-Nuget" to check is Nuget is installed in local folder.
.PARAMETER WorkingFolder
Location that Nuget.exe is/isn't.
Location that we want to download Nuget package to.
Mandatory
.PARAMETER NugetInstalluri
The url used to download Nuget.
mandatory
.PARAMETER targetVersion
The version of the Nuget package we want to get.
Not mandatory - will download the latest if not included.
.INPUTS
N/A
.OUTPUTS
targetMsi: the path to Microsoft SQL Server 2016 LocalDB msi, irrespective of whether it was downloaded or already existed.
.EXAMPLE
$targetMsi = Get-LocalDb2016NuGet -WorkingFolder $PsScriptRoot -NugetInstallUri "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -targetVersion "13.1.4001.0"
.NOTES
  N/A
#>

    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [string] $WorkingFolder,
        [Parameter(Mandatory = $false)]
        [string] $targetVersion
    )
    $NugetInstallUri = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
    $NuGetPath = Install-NuGet -WorkingFolder $WorkingFolder -NuGetInstallUri $NugetInstallUri
    $nugetInstallLocalDb = "&$NuGetPath install Microsoft.SQL.Server.2016.LocalDB -ExcludeVersion -OutputDirectory $WorkingFolder"

    if ($targetVersion) {
        $nugetInstallLocalDb += " -version $targetVersion"
    }
    else {
        Write-Verbose "As no version target version of Microsoft SQL Server 2016 LocalDB, the latest version will be downloaded." -Verbose
    }
    Write-Verbose $nugetInstallLocalDb -Verbose
    Invoke-Expression $nugetInstallLocalDb | Out-Null
    $localDbInstallFolder = "$WorkingFolder\Microsoft.SQL.Server.2016.LocalDB"
    if (-not (Test-Path $localDbInstallFolder)) {
        Throw "It appears that the nuget install hasn't worked, check output above to see whats going on"
    }
    $targetMsi = Join-Path $localDbInstallFolder "SqlLocalDB.msi"
    Return $targetMsi
}