Function/Setup/Remove-F2B.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
<#
    .NOTES
        File Name : Enable-F2B.ps1
        Author : Thomas ILLIET, contact@thomas-illiet.fr
        Date : 2018-02-15
        Last Update : 2018-02-15
        Version : 1.0.0
#>


function Remove-F2B(){
    
    Write-Host "# ++++++++++++++++++++++++++++++++++++++ " -ForegroundColor Yellow
    Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "Fail2Ban Remove"
    Write-Host "# ++++++++++++++++++++++++++++++++++++++ " -ForegroundColor Yellow

    Write-Host "# + " -ForegroundColor Yellow -nonewline;
    $Choice = Read-Host 'Do you really want to delete all data [yes/NO] '

    if($Choice -eq "yes") {
        
        # Stop Fail2ban
        Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "- Stop Service"
        Stop-F2B

        # Remove Folder
        Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "- Remove Folder"
        Try {
            Remove-Item -Path "$($env:PROGRAMFILES)\Fail2Ban" -Recurse -Force -ErrorAction Stop
        } Catch {
            Write-Error "Unable to remove properly this folder : $_"
        }

        # Remove Registry Key
        Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "- Remove Registry Key"
        Try {
            Remove-Item -Path "HKLM:\SOFTWARE\Fail2Ban\" -Recurse -Force -ErrorAction Stop
        } Catch {
            Write-Error "Unable to remove properly this registry Key : $_"
        }

        # Remove scheduled Task
        Write-Host "# + " -ForegroundColor Yellow -nonewline; Write-Host "- Remove scheduled Task"
        Try {
            Unregister-ScheduledTask -TaskName "Fail2Ban-Service" -Confirm:$false
            Unregister-ScheduledTask -TaskName "Fail2Ban-Manager" -Confirm:$false
        } Catch {
            write-error "Unable to Remove scheduled Task"
        }

    } else {
        Write-Output "Operation canceled"
    }

    Write-Host "# ++++++++++++++++++++++++++++++++++++++ " -ForegroundColor Yellow
}