private/Install-ChocolateyGetProvider.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
function Install-ChocolateyGetProvider {
    <#
        .SYNOPSIS
        Installs the ChocolateyGet package provider/source on this computer.

        .DESCRIPTION
        Installs the ChocolateyGet package provider/source on this computer forcefully.

        .PARAMETER PassThru
        Returns the object returned by "Get-PackageProvider -Name 'ChocolateyGet' -ErrorAction 'SilentlyContinue'".

        .EXAMPLE
        Install-ChocolateyGetProvider

        .EXAMPLE
        Install-ChocolateyGetProvider -PassThru
    #>

    
    [CmdletBinding(SupportsShouldProcess = $True, ConfirmImpact = "Medium")]
    Param (
        [Switch]
        $PassThru
    )

    Write-Verbose -Message "Checking for existence of ChocolateyGet provider."
    $toReturn = Get-PackageProvider -Name 'ChocolateyGet' -ErrorAction 'SilentlyContinue'
    if ($toReturn) {
        Write-Verbose -Message 'Chocolatey package provider/source already installed.'
        if ($PassThru) {
            return $toReturn
        } else {
            return
        }
    }

    $PackageProviderParams = @{
        Name        = 'ChocolateyGet';
        Scope       = 'CurrentUser';
        Force       = $true;
        ErrorAction = 'SilentlyContinue';
        Confirm     = $false;
    }

    Write-Verbose -Message 'Installing ChocolateyGet PackageProvider as it was not found.'
    if ($PSCmdlet.ShouldProcess($PackageProviderParams['Name'], "Install-PackageProvider")) {
        Install-PackageProvider @PackageProviderParams | Out-Null
    }
    
    $toReturn = Get-PackageProvider -Name 'ChocolateyGet' -ErrorAction 'SilentlyContinue'
    if ($toReturn) {
        Write-Verbose -Message 'Chocolatey package provider/source successfully installed.'
    } else {
        throw "ChocolateyGet failed to install or was not installed. Message: {0}" -f $error[0].Exception.message
    }

    if ($PassThru) {
        return $toReturn
    }
}