public/Install-BoxStarter.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


function Install-Boxstarter {
    Param(
        [string] $Version = $null,
        [switch] $Force
    )

    if(Test-Path "$Env:ProgramData\boxstarter") {
        Write-Debug "BoxStarter Install folder already exists"

        if(!$Force.ToBool()) {
            return;
        }
    }

    if($null -eq (Get-Command choco.exe -EA SilentlyContinue)) {
        if($Force.ToBool()) {
            Install-Chocolatey -Force
        } else {

            Write-Warning "Chocolatey is not installed, execute Install-Chocolatey first."
            return;
        }
    }

    if(!(Test-IsAdmin)) {
        $bootstrapperFile = ${function:Get-Boxstarter}.File
        if($bootstrapperFile) {
            Write-Host "User is not running with administrative rights. Attempting to elevate..."
            $command = "-ExecutionPolicy bypass -noexit -command . '$bootstrapperFile';Get-Boxstarter $($args)"
            Start-Process powershell -verb runas -argumentlist $command
        }
        else {
            Write-Warning "User is not running with administrative rights.`nPlease open a PowerShell console as administrator and try again."
        }
        return
    }

    $badPolicy = $false
    @("Restricted", "AllSigned") | ? { $_ -eq (Get-ExecutionPolicy).ToString() } | % {
        Write-Warning "Your current PowerShell Execution Policy is set to '$(Get-ExecutionPolicy)' and will prohibit boxstarter from operating propperly."
        Write-Warning "Please use Set-ExecutionPolicy to change the policy to RemoteSigned or Unrestricted."
        $badPolicy = $true
    }
    
    if($badPolicy) { return }

    Write-Output "Welcome to the Boxstarter Module installer!"
    $args = @(
        "install",  
        "Boxstarter",
        "-y")

    if(![string]::IsNullOrWhiteSpace($Version)) {
        $args += "--version"
        $args += "$VErsion"
    } 
    
    & choco.exe @args 

    if(!(Test-Path "$Env:ProgramData\boxstarter")) {
        Write-Warning "Boxstarter not found at $Env:ProgramData\boxstarter"
        return 
    }

    #
    Import-Module "$Env:ProgramData\boxstarter\boxstarter.chocolatey\boxstarter.chocolatey.psd1" -Force
}