Public/Write-LogEntry.ps1

function Write-LogEntry
{
    <#
        .DESCRIPTION
            Write data to a CMTrace compatible log file
 
        .PARAMETER Value
            The data to write to the log file
 
        .PARAMETER Severity
            The severity of the log file entry (1 = Information, 2 = Warning, 3 = Error)
 
        .PARAMETER FileName
            The name of the log file
 
        .EXAMPLE
            Write-LogEntry -Value "This is a log entry" -Severity 1
 
        .NOTES
            Created by: Nickolaj Andersen / Maurice Daly
            Modified by: Jon Anderson
            Modified: 2023-07-10
 
    #>

    param(
        [parameter(Mandatory = $true, HelpMessage = "Value added to the log file.")][ValidateNotNullOrEmpty()]
        [string]$Value,
        [parameter(Mandatory = $true, HelpMessage = "Severity for the log entry. 1 for Informational, 2 for Warning and 3 for Error.")][ValidateNotNullOrEmpty()][ValidateSet("1", "2", "3")]
        [string]$Severity,
        [parameter(Mandatory = $false, HelpMessage = "Name of the log file that the entry will written to.")][ValidateNotNullOrEmpty()]
        [string]$FileName = "Win32AppManagement.log"
    )

    $LogFilePath = Join-Path -Path $LogsDirectory -ChildPath $FileName
    if(-not(Test-Path -Path 'variable:global:TimezoneBias'))
    {
        [string]$global:TimezoneBias = [System.TimeZoneInfo]::Local.GetUtcOffset((Get-Date)).TotalMinutes
        if($TimezoneBias -match "^-")
        {
            $TimezoneBias = $TimezoneBias.Replace('-', '+')
        }
        else
        {
            $TimezoneBias = '-' + $TimezoneBias
        }
    }
    $Time = -join @((Get-Date -Format "HH:mm:ss.fff"), $TimezoneBias)    
    $Date = (Get-Date -Format "MM-dd-yyyy")
    $Context = $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
    $LogText = "<![LOG[$($Value)]LOG]!><time=""$($Time)"" date=""$($Date)"" component=""Win32AppManagement"" context=""$($Context)"" type=""$($Severity)"" thread=""$($PID)"" file="""">"
    try
    {
        Out-File -InputObject $LogText -Append -NoClobber -Encoding Default -FilePath $LogFilePath -ErrorAction Stop
    }
    catch [System.Exception]
    {
        Write-Warning -Message "Unable to append log entry to $FileName file. Error message at line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
    }
}