Public/Write-LogMessage.ps1

<#
.SYNOPSIS
Writes log messages to a file and/or sends them to an online log host.
 
.DESCRIPTION
The Write-LogMessage function is used to log messages with various severity levels. It allows writing log messages to a file and optionally sending them to an online log host. The log messages can be categorized based on severity levels such as Error, Critical, Warning, Info, Debug, or None.
 
.PARAMETER LogMessage
Specifies the log message to be written.
 
.PARAMETER LogFile
Specifies the path to a log file where the message should be written. If not provided, the log message will be written to the console output.
 
.PARAMETER Severity
Specifies the severity level of the log message. Valid values are 'Error', 'Critical', 'Warning', 'Info', 'Debug', or 'None'. If not provided, the default value is 'None'.
 
.EXAMPLE
Write-LogMessage -LogMessage "An error occurred" -LogFile "C:\Logs\app.log" -Severity "Error"
Writes an error log message to the specified log file with the current timestamp and severity level.
 
#>

function Write-LogMessage {
  [CmdletBinding()]
  param (
      [Parameter()][String]$LogMessage,
      [Parameter()][String]$LogFile,
      [ValidateSet('Error','Critical','Warning','Info','Debug','None')]$Severity
  )

  if(-not $LogMessage){
      Throw 'No message provided'
  }

  if(-not $Severity){
      $Severity = 'Info'
  }

  $timestamp = (Get-Date).ToString("HH:mm:ss")
  $LogMessageContent = "[$($TimeStamp)][$($Severity.ToUpper())] - $($LogMessage)"

  if($LogFile){
      Add-Content -Path $LogFile -Value $LogMessageContent
  } else {
      Write-Output $LogMessageContent
  }
}