private/Install-iPerf3.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
function Install-iPerf3 {
    <#
        .SYNOPSIS
        Installs the latest version of iPerf3 on this computer.

        .DESCRIPTION
        Installs the latest version of iPerf3 on this computer from the ChocolateyGet package source.

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

        .EXAMPLE
        Install-iPerf3

        .EXAMPLE
        Install-iPerf3 -PassThru
    #>

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

    try {
        Write-Verbose -Message 'Checking for existing iperf3 package.'
        Import-PackageProvider -Name 'ChocolateyGet' -ErrorAction 'Stop'
        $toReturn = Get-Package -Name 'iperf3' -ProviderName 'ChocolateyGet' -Force -ErrorAction 'SilentlyContinue'

        if ($toReturn) {
            Write-Verbose -Message 'iPerf3 package already installed.'
            if ($PassThru) {
                return $toReturn
            } else {
                return
            }
        }
    } catch {
        if (!(Get-PackageProvider -ListAvailable -Name 'ChocolateyGet' -ErrorAction 'SilentlyContinue')) {
            Write-Verbose -Message 'ChocolateyGet package provider not found; installing.'
            try {
                Install-ChocolateyGetProvider
            } catch {
                $PSCmdlet.ThrowTerminatingError($_)
            }
        }
    }

    Write-Verbose -Message 'Importing ChocolateyGet package provider and installing iperf3 package as it was not found.'
    Import-PackageProvider -Name 'ChocolateyGet'

    $PackageParams = @{
        Name         = 'iperf3';
        ProviderName = 'ChocolateyGet';
        Force        = $true;
        ErrorAction  = 'SilentlyContinue';
        Confirm      = $false;
    }
    if ($PSCmdlet.ShouldProcess($PackageParams['Name'], 'Install-Package')) {
        Install-Package @PackageParams |
            Out-Null
    }

    $toReturn = Get-Package -Name 'iperf3' -ProviderName 'ChocolateyGet' -ErrorAction 'SilentlyContinue'
    if ($toReturn) {
        Write-Verbose -Message 'iPerf3 package installed.'
    } else {
        throw 'iPerf3 failed to install or was not installed. Message: {0}' -f $error[0].Exception.message
    }

    if ($PassThru) {
        return $toReturn
    }
}