AMCScriptLogging.psm1
<#
=========================================================================== Created with: SAPIEN Technologies, Inc., PowerShell Studio 2024 v5.8.242 Created on: 9/19/2024 8:37 PM Created by: abranham Organization: Filename: AMCScriptLogging.psm1 ------------------------------------------------------------------------- Module Name: AMCScriptLogging =========================================================================== #> FUNCTION New-Log { <# .DESCRIPTION Adds an entry into a current log file. .PARAMETER Message The message to add to the log file. .PARAMETER Tab The nunber of tabs to add to a message to indent the text. .PARAMETER Type The type of message to add to the log. Acceptable values are INF, ERR, WRN, STR, END. .PARAMETER Path The path to the log file being worked. This value should be in $Global:LogFilePath set by New-LogFile. .EXAMPLE PS> New-Log -Type 'STR' Adds line to log file indicating the execution of script start. .EXAMPLE PS> New-Log -Message 'Someting occured to log' Adds a new line to the log with default typr of INF. .EXAMPLE PS> New-Log -Message 'An error has occured' -Type 'ERR' Adds a new line to the log with type of error and a message. .EXAMPLE PS> New-Log -Type 'END' Adds a new line to the log with type of end. ** This also clears the global variable to the log file location. New-LogFile will need to be executed again. .NOTES Author: Adam Branham Date: 20240919 #> PARAM( [Parameter(Mandatory = $false)] [string]$Message, [Parameter(Mandatory = $false)] [int]$Tab, [Parameter(Mandatory = $false)] [ValidateSet('INF', 'ERR', 'WRN', 'STR', 'END')] [string]$Type = 'INF', [Parameter(Mandatory = $false)] [System.IO.FileInfo]$Path = $Global:LogFilePath, [Parameter(Mandatory = $false)] [string]$Console = $Global:LogFileConsole ) BEGIN { IF (-NOT(Test-Path -Path $Path)) { THROW 'Log file missing, ensure New-LogFile has been executed first.' } $NowTime = (Get-Date).ToString('hh:mm:ss tt') $Tabs = "`t" * $Tab } PROCESS { SWITCH ($Type) { 'STR' { $LogMessage = "[$NowTime][STR] Execution Starting" } 'END' { $LogMessage = "[$NowTime][END] Execution End" } 'ERR' { $LogMessage = "[$NowTime][ERR] $Tabs$Message" } 'INF' { $LogMessage = "[$NowTime][INF] $Tabs$Message" } 'WRN' { $LogMessage = "[$NowTime][WRN] $Tabs$Message" } DEFAULT { $LogMessage = "[$NowTime][UNK] [Message type not found] $Message" } } } END { Add-Content -Path $Global:LogFilePath -Value $LogMessage IF ($Console -eq $true) { Write-Host $LogMessage } IF ($Type -eq 'END') { Clear-Variable -Name 'LogFilePath' -Scope 'Global' Clear-Variable -Name 'LogFileConsole' -Scope 'Global' } } } FUNCTION New-LogFile { <# .DESCRIPTION Creates a new log file for script logging. .PARAMETER Path Root path to your log folder. .PARAMETER Name Name of the application your logs should be for. .PARAMETER NoDatedPath switch If defined the directory path will not include a dated structure. .OUTPUTS Returns a global variable with log file fullname. $Global:LogFilePath .EXAMPLE PS> New-LogFile -Path 'C:\Logs' -Name 'TEST' -NoDatedPath Creates a new log file at the following location C:\Logs\TEST\TEST-Date.txt .EXAMPLE PS> New-LogFile -Path 'C:\Logs' -Name 'TEST' Creates a new log file at the following location C:\Logs\TEST\2024\09\TEST-Date.txt .EXAMPLE PS> New-LogFile -Path 'C:\Logs' -Name 'TEST' -SubName 'Processed' Creates a new log file at the following location C:\Logs\TEST\Processed\2024\09\TEST-Processed-Date.txt .NOTES Author: Adam Branham Date: 20240919 #> PARAM( [Parameter(Mandatory = $true)] [System.IO.FileInfo]$Path, [Parameter(Mandatory = $true)] [string]$Name, [Parameter(Mandatory = $false)] [string]$SubName, [Parameter(Mandatory = $false)] [switch]$NoDatedPath, [Parameter(Mandatory = $false)] [switch]$Console = $true ) BEGIN { IF (-NOT(Test-Path -Path $Path)) { $null = New-Item -Path $Path -ItemType Directory -Force } $Today = (Get-Date).ToString('yyyyMMdd') } PROCESS { IF ($NoDatedPath -eq $true) { IF ($SubName) { $FilePath = Join-Path -Path $Path -ChildPath "$Name\$SubName\$Name-$SubName-$Today.txt" } ELSE { $FilePath = Join-Path -Path $Path -ChildPath "$Name\$Name-$Today.txt" } } ELSEIF ($NoDatedPath -eq $false) { $DatedPath = (Get-Date).ToString('yyyy\\MM') IF ($SubName) { $FilePath = Join-Path -Path $Path -ChildPath "$Name\$SubName\$DatedPath\$Name-$SubName-$Today.txt" } ELSE { $FilePath = Join-Path -Path $Path -ChildPath "$Name\$DatedPath\$Name-$Today.txt" } } IF (-NOT(Test-Path -Path $FilePath)) { $null = New-Item -Path $FilePath -ItemType File -Force $RunAs = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name Add-Content -Path $FilePath -Value '=======================================' Add-Content -Path $FilePath -Value "File Date: [$Today]" IF ($Name) { Add-Content -Path $FilePath -Value "Log Name: [$Name]" } IF ($SubName) { Add-Content -Path $FilePath -Value "Sub Name: [$SubName]" } Add-Content -Path $FilePath -Value "Server: [$($env:COMPUTERNAME)]" Add-Content -Path $FilePath -Value "RunAs: [$RunAs]" Add-Content -Path $FilePath -Value '=======================================' Add-Content -Path $FilePath -Value '' } } END { Set-Variable -Name 'LogFilePath' -Value $FilePath -Scope 'Global' IF ($Console -eq $true) { Set-Variable -Name 'LogFileConsole' -Value $true -Scope 'Global' } } } |