public/Install-SpeedTestServer.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
function Install-SpeedTestServer {
    <#
        .SYNOPSIS
        Configures the local computer as an iPerf3 server.

        .DESCRIPTION
        Configures iPerf3 as a constantly-listening service on the local computer.
        
        .PARAMETER Port
        The port number that the iPerf3 server will listen on.
        If not specified, the default port '5201' will be used.

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

        .EXAMPLE
        Install-SpeedTestServer
        Sets up the local computer as an iPerf3 server listening on default iPerf port 5201.

        .EXAMPLE
        Install-SpeedTestServer -Port 5555
        Sets up the local computer as an iPerf3 server listening on port 5555.

        .EXAMPLE
        Install-SpeedTestServer -Port 5555 -PassThru
    #>


    [CmdletBinding()]
    Param (
        [ValidateNotNullOrEmpty()]
        [String]
        $Port = '5201',
        [Switch]
        $PassThru
    )

    $timeout = 30 # Seconds

    if (!(Test-Administrator)) {
        throw "You are not running as administrator. Please re-run this function after opening PowerShell as administrator."
    }

    Write-Verbose -Message "Setting up iPerf3 server on local machine on port $Port."
    Install-ChocolateyGetProvider
    Install-iPerf3
    Set-iPerf3Port -Port $Port
    Set-iPerf3Task -Port $Port

    $timeoutTimer = [Diagnostics.Stopwatch]::StartNew()
    $processTest = $false
    while ($timeoutTimer.Elapsed.TotalSeconds -lt $timeout) {
        $getProcessResult = Get-Process -Name 'iperf3' -ErrorAction 'SilentlyContinue'
        if ($getProcessResult) {
            Write-Verbose -Message "iPerf3 Server started on port $Port."
            $processTest = $true
            break
        } else {
            Start-Sleep -Seconds 3
        }
    }
    $timeoutTimer.Stop()

    if (!($processTest)) {
        throw "iPerf3 Server failed to start on port $Port. Timeout of $timeout seconds reached."
    }

    if ($PassThru) {
        return $getProcessResult
    }
}