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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<#
    .SYNOPSIS
    Configures the local or a domain computer as an iPerf3 server.

    .DESCRIPTION
    Configures iPerf3 as a constantly-listening service on the local or a domain computer.

    .PARAMETER ComputerName
    The name of the domain computer that will act as an iPerf3 server.
    If this parameter is not specified, the local computer will be configured as an iPerf3 server.
    
    .PARAMETER Port
    The port number that the iPerf3 server will listen on.
    If not specified, the default port '5201' will be used.

    .PARAMETER Credential
    Domain credentials used to authenticate to a domain computer, if necessary.

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

    .EXAMPLE
    Install-SpeedTestServer -ComputerName SERVER01 -Port 5201 -Credential domain\user
    Sets up computer SERVER01 as an iPerf3 server listening on port 5201.
    This runs the appropriate setup functions under the 'domain\user' credential.

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

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


function Install-SpeedTestServer {
    [CmdletBinding()]
    Param (
        [ValidateNotNullOrEmpty()]
        [String]
        $ComputerName,
        [ValidateNotNullOrEmpty()]
        [String]
        $Port = '5201',
        [ValidateNotNullOrEmpty()]
        [PSCredential]
        $Credential,
        [Switch]
        $PassThru
    )

    Write-Verbose -Message "Setting up $ComputerName as a speed test server."

    $timeout = 30 # Seconds

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

    if (!($ComputerName)) {
        Write-Verbose -Message "Setting up 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
        }
    }
    else {
        if (!($Credential)) {
            Write-Verbose -Message "Setting up server on domain machine $ComputerName on port $Port."
            try {
                Invoke-Command -ComputerName $ComputerName -ScriptBlock ${Function:Install-ChocolateyGetProvider}
                Invoke-Command -ComputerName $ComputerName -ScriptBlock ${Function:Install-iPerf3}
                Invoke-Command -ComputerName $ComputerName -ScriptBlock ${Function:Set-iPerf3Port} -ArgumentList $Port
                Invoke-Command -ComputerName $ComputerName -ScriptBlock ${Function:Set-iPerf3Task} -ArgumentList $Port
            }
            catch {
                $PSCmdlet.ThrowTerminatingError($_)
            }

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

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

            if ($PassThru) {
                return $getProcessResult
            }
        }
        else {
            Write-Verbose -Message "Setting up server on domain machine $ComputerName on port $Port with credential $Credential."
            try {
                Invoke-Command -ComputerName $ComputerName -ScriptBlock ${Function:Install-ChocolateyGetProvider} -Credential $Credential
                Invoke-Command -ComputerName $ComputerName -ScriptBlock ${Function:Install-iPerf3} -Credential $Credential
                Invoke-Command -ComputerName $ComputerName -ScriptBlock ${Function:Set-iPerf3Port} -ArgumentList $Port -Credential $Credential
                Invoke-Command -ComputerName $ComputerName -ScriptBlock ${Function:Set-iPerf3Task} -ArgumentList $Port -Credential $Credential
            }
            catch {
                $PSCmdlet.ThrowTerminatingError($_)
            }

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

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

            if ($PassThru) {
                return $getProcessResult
            }
        }
    }
}