Public/Support/Helper/Write-TFErrorLog.ps1

# Module: TeamsFunctions
# Function: Tool
# Author: David Eberhardt
# Updated: 02-JUN-2022
# Status: Live




function Write-TFErrorLog {
  <#
    .SYNOPSIS
        Writes an Error-log for an Artifact to the FilePath
    .DESCRIPTION
        Writes a log entry to a file
  .PARAMETER ErrorLog
    Required. Text to log
  .PARAMETER Artifact
    Required. Affected object this error was received for
  .PARAMETER LogPath
    Optional. Path to write log file to. If not provided, log is written to C:\Temp
  .EXAMPLE
    Write-TFErrorLog -ErrorLog "UserNotFound" -Artifact "john@domain.com"
 
    Writes the current timestamp followed by the Artifact in one line and the ErrorLog in the second line.
    Creates a file in C:\Temp with the current date, and hour followed by the function calling Write-TFErrorLog
  .EXAMPLE
    Write-TFErrorLog -ErrorLog $Log -Artifact "john@domain.com" -LogPath C:\Logs\Today
 
    Writes the current timestamp followed by the Artifact in one line and the ErrorLog in the second line.
    If $Log is an Arraylist, will write multiple errors in one go.
    Creates a file in C:\Logs\Today with the current date, and hour followed by the function calling Write-TFErrorLog
  .INPUTS
    System.String
  .OUTPUTS
    System.File
  .NOTES
    Just my take on writing consistent error logs that are easily digestible
    Please note that a new file is written with every new starting hour. This is intended to keep the log size managable.
  .COMPONENT
    SupportingFunction
  .FUNCTIONALITY
    Enhances writing error-transcripts for evidencing
  .LINK
    https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/Write-TFErrorLog.md
  .LINK
    https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/about_Supporting_Functions.md
  .LINK
    https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/
  #>


  [CmdletBinding()]
  param (
    [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
    $ErrorLog,

    [Parameter(HelpMessage = 'Artifact involved')]
    [string]$Artifact,

    [Parameter(HelpMessage = 'Optional path to log to')]
    [string]$LogPath
  ) #param

  begin {
    #Show-FunctionStatus -Level Live
    #Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)"

    # Determining Function calling this function
    [string]$CallingFunction = $((Get-PSCallStack)[1].Command)

    # Defining Log path
    if ( -not $PSBoundParameters.ContainsKey('LogPath') ) {
      $Path = 'C:\Temp'
      $Filename = "$(Get-Date -Format 'yyyy-MM-dd HH')xx - $CallingFunction - ERROR.log"
      $LogPath = "$Path\$Filename"
    }
    else {
      $Path = $LogPath | Split-Path -Parent
      $Filename = $LogPath | Split-Path -Leaf
      if (-not (Test-Path -Path $Path)) { New-Item -Path $Path -ItemType Directory }
    }

    # Defining Artifact prefix for strings
    [string]$ArtifactString = if ( $PSBoundParameters.ContainsKey('Artifact') ) { "'$Artifact'" } else { 'ERROR' }
  } #begin

  process {
    Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)"

    # Write log entry to $Path
    Write-Verbose -Message "$ArtifactString`: Issues encountered are written to '$Path'"
    "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss K') | $CallingFunction | $ArtifactString" | Out-File -FilePath $LogPath -Append
    $ErrorLog | Out-File -FilePath $LogPath -Append
  } #process

  end {
    #Write-Verbose -Message "[END ] $($MyInvocation.MyCommand)"
  } #end
}