Functions/InstallDotNet.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

function Install-DotNet {
    param ( [string] $WorkingFolder,
        [string] $uri,
        [string] $DotNetVersion
    )

    $splitArray = $uri -split "/"
    $fileName = $splitArray[-1]
    
    Write-Verbose "Am attempting to install .NET $($DotNetVersion)" -Verbose
    $netInstaller = Join-Path -Path $WorkingFolder -ChildPath $fileName
    try {
        Invoke-WebRequest -Uri $uri -OutFile $netInstaller 
    }
    catch {
        Throw $_.Exception
    }
    If ((Test-Path $netInstaller)) {
        "File $fileName downloaded!"
    }
    else {
        "Oh dear!"
    }
    "attempting to install .Net $($DotNetVersion) from $($fileName)..."
    try {
        $args = " /q /norestart"
        $installNet471BuildTools = Start-Process $netInstaller -ArgumentList $args -Wait -PassThru -WorkingDirectory $WorkingFolder -NoNewWindow
    }
    catch {
        $fail = $_.Exception
        Write-Error $fail
    }
    if ($installNet471BuildTools.ExitCode -eq 0) {
        Write-Host "Install Successful! Run Test-NetInstalled to verify!" -ForegroundColor DarkGreen -BackgroundColor White
    }
    else {
        Write-Host "Something went wrong in installing .NET $($DotNetVersion) $($fileName)"
        Write-Error $fail
        throw
    }
}