Public/Write-ECKLog.ps1

Function Write-ECKlog
    {
        # Version 1.3 -14/02/2022 - replaced write-host by Write-output

        [CmdletBinding()]
        [Alias('Write-Log')]
        Param(
              [parameter()]
              [String]$Path = [string]::Format("{0}\{1}.log", $Script:LogDir, $Script:CurrentScriptName.Replace(".ps1", "")),
              [parameter(Position=0, Mandatory=$true, HelpMessage="Please provide a message to log !")]
              [String]$Message,
              [parameter()]
              [String]$OutputToConsole = $true,
              [parameter()]
              [String]$EventLogID,
              [parameter()]
              [Switch]$EventLogOnly,
              #Severity Type(1 - Information, 2- Warning, 3 - Error)
              [parameter(Mandatory=$False)]
              [ValidateRange(1,3)]
              [Int]$Type = 1
        )

        $oDate = $(Get-Date -Format "M-d-yyyy")
        $oHour = $(Get-Date -Format "HH:mm:ss")
        $MessageType = @{1 = "Information"; 2 = "Warning"; 3 = "Error"}
        $Tab = [char]9

        # Create a log entry
        $Content = "$oDate $oHour, $($MessageType[$type]) $Tab $($Message -replace "`r`n", ", ")"

        # Write the line to the log file
        If ($Script:DontLogNow -ne $true -and (-not $EventLogOnly))
            {
                $Content| Out-file -FilePath $Path -Encoding UTF8 -Append -ErrorAction SilentlyContinue

                If ($OutputToConsole -eq $true -and $Script:EnableConsoleLogging -ne $false){Write-output $Content}
            }

        If ($EventLogID)
            {
                $AppriendlyName = $Script:CurrentScriptName -replace "-" ,"" -replace ".ps1","" -replace " ",""
                If ([System.Diagnostics.EventLog]::SourceExists($AppriendlyName) -eq $false){New-EventLog -LogName "Application" -Source $AppriendlyName}
                Write-EventLog -LogName "Application" -Source $AppriendlyName -EventID $EventLogID -EntryType $($MessageType[$type]) -Message $Message -Category 0
            }
    }