Function/Tool/Add-F2BLog.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
function Add-F2BLog(){
    <#
    .SYNOPSIS
        .
    .PARAMETER Type
        .
    .PARAMETER Message
        .
    .PARAMETER Category
        .
    .EXAMPLE
        C:\PS> Add-F2BLog -Type Error -Message 'My Unicorn is beatifull'
        C:\PS> Add-F2BLog -Type Error -Message 'My Unicorn is beatifull'
    .NOTES
        Author : Thomas ILLIET
        Date : 2018-02-15
        Last Update : 2018-02-15
    #>

    Param(
        [Parameter(Mandatory=$true,Position=0)]
        [ValidateSet('Information','Error','Warning')]
        [String]$Type,
        [Parameter(Mandatory=$true,Position=1)]
        [String]$Message
    )

    # ++++++++++++++++++++++++
    # Get Configuration
    $Config = Get-F2BConfig -ConfigFolder System

    # ++++++++++++++++++++++++
    # File Log
    if($Config.FileLog_Status -eq "1") {
        Try {
            $Time = Get-Date -Format "HH:mm:ss.ffffff"
            $Date = Get-Date -Format "MM-dd-yyyy"
            $FilePath = (join-path -Path $Config.FileLog_Folder -ChildPath "Fail2Ban-Service.log")

            switch ($Type) {
                Information { $TypeID = 1 }
                Error       { $TypeID = 3 }
                Warning     { $TypeID = 2 }
            }

            $LogMessage = "<![LOG[$Message" + "]LOG]!><time=`"$Time`" date=`"$Date`" component=`"`" context=`"`" type=`"$TypeID`" thread=`"`" file=`"`">"
            $LogMessage | Out-File -Append -Encoding UTF8 -FilePath $FilePath
        } Catch {
            write-error "Unable to write File : $_"
        }
    }

    # ++++++++++++++++++++++++
    # Windows Log
    if($Config.EventLog_Status -eq "1") {
        Try {
            $Params = @{
                LogName   = $Config.EventLog_Name
                Source    = "Fail2Ban"
                EntryType = $Type
                EventId   = $Config.EventLog_Id
                Message   = $Message
            }
            Write-EventLog @Params
        } Catch {
            write-error "Unable to write EventLog : $_"
        }
    }
}