Function/System/Add-F2BAddress.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 Add-F2BAddress(){
    <#
    .SYNOPSIS
        .
    .PARAMETER Type
        .
    .PARAMETER IP
        .
    .PARAMETER Unlimited
        .
    .EXAMPLE
        C:\PS> Add-F2BAddress -Type Black -IP 1.2.3.4
        C:\PS> Add-F2BAddress -Type Black -IP 1.2.3.4 -Unlimited $true
    .NOTES
        Author : Thomas ILLIET
        Date : 2018-02-15
        Last Update : 2018-02-15
    #>

    Param(
        [Parameter(Mandatory=$true,Position=0)]
        [ValidateSet('Black','White')]
        [String]$Type,

        [Parameter(Mandatory=$true,Position=1)]
        [String]$IP,

        [Parameter(Mandatory=$false)]
        [ValidateSet($true,$false)]
        [bool]$Unlimited=$false
    )

    # ++++++++++++++++++++++++
    # Add IP to BlackList
    if($Type -eq 'Black') {
        if((Test-F2BRegistryIP -IP $IP -Type White) -eq $false){
            if((Test-F2BRegistryIP -IP $IP -Type Black) -eq $false){
                Try {
                    Add-F2BRegistryIP -IP $IP -Type Black -Unlimited $Unlimited | Out-Null
                    Add-F2BFirewallRule -IP $IP | Out-Null
                } Catch {
                    Write-Error "Unable to Add IP to BlackList: $_"
                }
            }
        } else {
            Write-Error "Unable to add IP, is present in WhiteList"
        }
    }

    # ++++++++++++++++++++++++
    # Add IP to WhiteList
    if($Type -eq 'White') {
        if((Test-F2BRegistryIP -IP $IP -Type White) -eq $false){
            # Remove IP if present in BlackList
            if((Test-F2BRegistryIP -IP $IP -Type Black) -eq $true){
                Try {
                    Remove-F2BRegistryIP -IP $IP -Type Black | Out-Null
                    Remove-F2BFirewallRule -IP $IP | Out-Null
                } Catch {
                    Write-Error "Unable to Remove IP to WhiteList: $_"
                }
            }
            # Add to WhiteList
            Try {
                Add-F2BRegistryIP -IP $IP -Type White -Unlimited $Unlimited | Out-Null
            } Catch {
                Write-Error "Unable to Add IP to WhiteList : $_"
            }
        } 
    }
}