Function/Registry/Add-F2BRegistryIP.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
Function Add-F2BRegistryIP(){
    <#
    .SYNOPSIS
        .
    .PARAMETER Type
        .
    .PARAMETER IP
        .
    .PARAMETER Unlimited
        .
    .EXAMPLE
        C:\PS> Add-F2BRegistryIP -Type Black -IP 5.6.7.8
        C:\PS> Add-F2BRegistryIP -Type Black -IP 5.6.7.8 -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)]
        [IpAddress]$IP,
        [Parameter(Mandatory=$false,Position=2)]
        [ValidateSet($true,$false)]
        [bool]$Unlimited=$false
    )
    # Add IP to registry
    Try {

        # Set Duration
        if($Unlimited -eq $true) {
            $Value = 'Unlimited'
        } else {
            $Value = ([String](Get-Date))
        }

        $NewItem = New-ItemProperty -Path "HKLM:\SOFTWARE\Fail2Ban\List\$Type" -Name $IP -Value $Value -PropertyType "String"
        Add-F2BLog -Type Information -Message "Add registry $IP to $($Type)List"
        return $NewItem
    } Catch {
        $Message = "Unable to add a registry '$IP' to $($Type)List"
        Add-F2BLog -Type Error -Message $Message 
        return $false
    }

}