Configurations/PowerShellLab/PostSetup/Download-Git.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
74
75
76
77
#requires -version 5.1

[CmdletBinding(DefaultParameterSetName = "VM")]
Param(
    [Parameter(Mandatory, ParameterSetName = 'VM')]
    #specify the name of a VM
    [string]$VMName,
    [Parameter(Mandatory, ParameterSetName = 'VM')]
    #Specify the user credential
    [pscredential]$Credential,
    [Parameter(Mandatory, ParameterSetName = "session")]
    #specify an existing PSSession object
    [System.Management.Automation.Runspaces.PSSession[]]$Session,
    [switch]$Install
)

Try {
    if ($PSCmdlet.ParameterSetName -eq 'VM') {
        Write-Host "Creating PSSession to $VMName" -ForegroundColor cyan

        $session = New-PSSession @PSBoundParameters -ErrorAction stop
    }

    $sb = {
        Param([switch]$Install)

        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

        #download the latest 64bit version of Git for Windows
        $uri = 'https://git-scm.com/download/win'
        #path to store the downloaded file
        $path = "C:\"
        Write-Host "Getting latest version of git from $uri" -ForegroundColor cyan
        #get the web page
        $page = Invoke-WebRequest -Uri $uri -UseBasicParsing -DisableKeepAlive

        #get the download link
        $dl = ($page.links | where-object outerhtml -match 'git-.*-64-bit.exe' | Select-Object -first 1 * ).href
        Write-Host "Found download link $dl" -ForegroundColor cyan

        #split out the filename
        $filename = split-path $dl -leaf

        #construct a filepath for the download
        $out = Join-Path -Path $path -ChildPath $filename
        Write-Host "Downloading $out from $dl" -ForegroundColor cyan

        #download the file
        Try {
            Invoke-WebRequest -uri $dl -OutFile $out -UseBasicParsing -DisableKeepAlive -ErrorAction Stop

            if ($install) {
                &$out /verysilent /norestart /suppressmessageboxes
            }
            else {
                #check it out
                Get-Item $out
            }
        }
        Catch {
            Throw $_
        }
    }

    Invoke-Command -ScriptBlock $sb -Session $session -ArgumentList $Install

    if ($PSCmdlet.ParameterSetName -eq 'VM') {
        Write-Host "Removing PSSession" -ForegroundColor cyan
        $Session | Remove-PSSession
    }
}
Catch {
    Throw $_
}