public/Invoke-SpeedTest.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<#
    .SYNOPSIS
    Starts a bandwidth test over the internet or on the local private network.

    .DESCRIPTION
    Starts a bandwidth test with iPerf3 over the internet against a public iPerf3 server, or on the local private network against a previously-configured iPerf3 server.

    .PARAMETER Internet
    Forces the bandwitdth test to run over the internet against a public iPerf3 server.
    If a default public iPerf3 server is not specified in the configuration file, the user will be prompted to run Set-SpeedTestConfig.

    .PARAMETER Local
    Forces the bandwidth test to run over the local network against a locally-accessible iPerf3 server.
    If a default server is not specified in the configuration file, the user will be prompted to run Set-SpeedTestConfig.

    .PARAMETER Server
    The hostname or IP address of a server that is running iPerf3 as a listening service.

    .PARAMETER Port
    The port on the iPerf3 server that iPerf3 is listening on.
    This will run the local iPerf3 client on the same port as they must match on the client and the server.
    If Server is specified and Port is not, the default port '5201' will be used.

    .EXAMPLE
    Invoke-SpeedTest -Internet
    Runs a bandwidth test against default public iPerf3 server that is stored in the configuration.
    If there is no stored default, you will be prompted to set one.

    .EXAMPLE
    Invoke-SpeedTest -Local
    Runs a bandwidth test against default local iPerf3 server that is stored in the configuration.
    If there is no stored default, you will be prompted to set one.

    .EXAMPLE
    Invoke-SpeedTest -Server local.domain.com
    Runs a bandwidth test against iPerf3 server 'local.domain.com' on default port '5201'.

    .EXAMPLE
    Invoke-SpeedTest -Server 20.19.57.21 -Port 7777
    Runs a bandwidth test against iPerf3 server '20.19.57.21' on port '7777'.
#>


function Invoke-SpeedTest {
    [CmdletBinding()]
    Param (
        [Parameter(ParameterSetName="Internet")]
        [Switch]
        $Internet,
        [Parameter(ParameterSetName="Local")]
        [Switch]
        $Local,
        [Parameter(Mandatory=$true,
            ParameterSetName="Specified")]
        [ValidateNotNullOrEmpty()]
        [String]
        $Server,
        [Parameter(ParameterSetName="Specified")]
        [ValidateNotNullOrEmpty()]
        [String]
        $Port
    )

    Install-ChocolateyGetProvider
    Install-iPerf3

    $config = Get-SpeedTestConfig
    $command = "iperf3.exe "

    if ($Internet) {
        if (!($config.defaultInternetServer.defaultServer)) {
            throw "No default Internet server configured - run Set-SpeedTestConfig."
        }
        else {
            $command = $command + "-c $($config.defaultInternetServer.defaultServer) "
            if ($config.defaultInternetServer.defaultPort) {
                $command = $command + "-p $($config.defaultInternetServer.defaultPort) "
            }
            else {
                $command = $command + "-p $($config.defaultPort) "
            }
        }
    }
    elseif ($Local) {
        if (!($config.defaultLocalServer.defaultServer)) {
            throw "No default Local server configured - run Set-SpeedTestConfig."
        }
        else {
            $command = $command + "-c $($config.defaultLocalServer.defaultServer) "
            if ($config.defaultLocalServer.defaultPort) {
                $command = $command + "-p $($config.defaultLocalServer.defaultPort) "
            }
            else {
                $command = $command + "-p $($config.defaultPort) "
            }
        }
    }
    elseif ($Server) {
        $command = $command + "-c $Server "
        if ($Port) {
            $command = $command + "-p $Port "
        }
        else {
            $command = $command + "-p $($config.defaultPort) "
        }
    }

    $command = $command + "-f m -J"

    $resultsJSON = Invoke-Expression -Command $command
    $resultsPS = $resultsJSON | ConvertFrom-Json

    if ($resultsPS.error) {
        if ($resultsPS.error -match 'unable to connect to server'){
            throw "Catastrophic error occurred: $($resultsPS.error)"
        }
        else {
            Write-Warning -Message "Problem occurred: $($resultsPS.error)"
            $megabitsPerSecSent = 0
            $megabitsPerSecReceived = 0
        }
    }
    else {
        $megabitsPerSecSent = (($resultsPS.end.sum_sent.bits_per_second) / 1000000.0).ToInt32($null)
        $megabitsPerSecReceived = (($resultsPS.end.sum_received.bits_per_second) / 1000000.0).ToInt32($null)
    }

    $returnObj = New-Object -TypeName 'PSCustomObject' @{
        megabitsPerSecSent = $megabitsPerSecSent;
        megabitsPerSecReceived = $megabitsPerSecReceived;
    }

    return $returnObj
}