Function/System/Initialize-F2BService.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
function Initialize-F2BService(){
    <#
    .SYNOPSIS
        .
    .EXAMPLE
        C:\PS> Initialize-F2BService
    .NOTES
        Author : Thomas ILLIET
        Date : 2018-02-15
        Last Update : 2018-02-15
    #>


    Try {
        Write-Host "# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor yellow
        Write-F2BConsole -Type Information -Message "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"  
        Write-F2BConsole -Type Information -Message "+ Initialize Fail2Ban Service"
        Write-F2BConsole -Type Information -Message "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

        # ++++++++++++++++++++++++
        # Get Configuration
        Write-F2BConsole -Type Information -Message "Get Configuration"
        $ConfigSystem = Get-F2BConfig -ConfigFolder System
        $ConfigModule = Get-F2BConfig -ConfigFolder Module

        # ++++++++++++++++++++++++
        # Load Module
        Write-F2BConsole -Type Information -Message "Get Module :"
        $ModuleFile = Get-Content (Join-Path -Path $F2BModuleRoot -ChildPath "Config/Module.json") -ErrorAction Stop
        $Modules =  ($ModuleFile | ConvertFrom-Json ).Module

        foreach ($Module in $Modules) {
            if((($ConfigModule).("$($Module.Prefix)_Status")) -eq "1"){
                Write-F2BConsole -Type Information -Message "+ $($Module.Prefix) ($($Module.Name))"
                . (join-Path -Path $F2BModuleRoot -ChildPath "Module\$($Module.FilePath)")
            }
        }

        # ++++++++++++++++++++++++
        # Loop
        Write-F2BConsole -Type Information -Message "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"  
        Write-F2BConsole -Type Information -Message "+ Run the loop every $($ConfigSystem.Service_Loop) seconds ..."
        Write-F2BConsole -Type Information -Message "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
        while($true)
        {
            # Execute Module
            foreach ($Module in $Modules) {
                Write-F2BConsole -Type Information -Message "Execute Module - $($Module.Prefix) "
                & "Use-$($Module.Prefix)" -ConfigModule $ConfigModule -ConfigSystem $ConfigSystem
            }

            # Wait to next step
            Start-F2BSleep -S $ConfigSystem.Service_Loop -Message "Wait next Loop ..."
        }

    } Catch {
        Write-F2BConsole -Type Error -Message "An error has occurred : $_"
    } finally {
        Write-F2BConsole -Type Information -Message "Stop Fail2Ban Service"
        Write-Host "# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor yellow
    }
}