Public/Progress/Write-OrbitErrorLog.ps1
# Module: Orbit.Tools # Function: Tool # Author: David Eberhardt # Updated: 02-JUN-2022 # Status: Live #TODO Refactor function calls to Write-OrbitErrorLog and remove alias Write-TFErrorLog function Write-OrbitErrorLog { <# .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-OrbitErrorLog -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-OrbitErrorLog .EXAMPLE Write-OrbitErrorLog -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-OrbitErrorLog .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/Orbit/tree/main/docs/Orbit.Tools/Write-OrbitErrorLog.md .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_Supporting_Functions.md .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/ #> [CmdletBinding()] [Alias('Write-TFErrorLog')] 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-OrbitFunctionStatus -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['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['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 "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss K') | $CallingFunction | $ArtifactString | $ErrorLog" | Out-File -FilePath $LogPath -Append } #process end { #Write-Verbose -Message "[END ] $($MyInvocation.MyCommand)" } #end } #Write-OrbitErrorLog |