private/Set-iPerf3Port.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
<#
    .SYNOPSIS
    Set the local firewall rules for the port that iPerf3 will listen on.

    .DESCRIPTION
    Set the local firewall rules for the port that iPerf3 will listen on.
    This will set inbound/outbound Allow TCP on the given port.

    .PARAMETER Port
    The port that iPerf3 will listen on.

    .PARAMETER PassThru
    Returns the objects returned by "New-NetFirewallRule" for both the inbound and outbound rules, in an array.

    .EXAMPLE
    Set-iPerf3Port -Port "5201"

    .EXAMPLE
    Set-iPerf3Port -Port "5201" -PassThru
#>


function Set-iPerf3Port {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Port,
        [Switch]
        $PassThru
    )

    $FirewallInboundParams = @{
        DisplayName = "iPerf3 Server Inbound TCP Rule";
        Direction = "Inbound";
        LocalPort = $Port;
        Protocol = "TCP";
        Action = "Allow";
        ErrorAction = "SilentlyContinue";
    }

    $FirewallOutboundParams = @{
        DisplayName = "iPerf3 Server Outbound TCP Rule";
        Direction = "Outbound";
        LocalPort = $Port;
        Protocol = "TCP";
        Action = "Allow";
        ErrorAction = "SilentlyContinue";
    }

    $inboundResult = New-NetFirewallRule @FirewallInboundParams
    $outboundResult = New-NetFirewallRule @FirewallOutboundParams

    if ($inboundResult -and $outboundResult) {
        Write-Verbose -Message 'iPerf3 server port firewall rules set.'
    }
    else {
        throw "iPerf3 server port firewall rules could not be set. Message: $($error[0].Exception.message)"
    }

    if ($PassThru) {
        return @($inboundResult, $outboundResult)
    }
}