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
65
66
67
function Set-iPerf3Port {
    <#
        .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
    #>

    
    [CmdletBinding(SupportsShouldProcess = $True, ConfirmImpact = 'Medium')]
    Param(
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Port,
        [Switch]
        $PassThru
    )

    Write-Verbose -Message 'Setting inbound and outbound iperf3 firewall rules.'

    $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';
    }

    if ($PSCmdlet.ShouldProcess('iperf3 Inbound/Outbound Firewall Rules', 'New-NetFirewallRule')) {
        $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: {0}' -f $error[0].Exception.message
    }

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