updated the write-loghelper and write-loghybrid

This commit is contained in:
2025-07-02 23:56:13 -04:00
parent 5c37b33bb3
commit 8ed2e06454

View File

@@ -293,75 +293,118 @@
#region Write-Log #region Write-Log
# This function is used as a fallback if the SVSMSP module is not installed
# This function is used as a fallback if the SVSMSP module is not installed # This function is used as a fallback if the SVSMSP module is not installed
function Write-LogHelper { function Write-LogHelper {
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory)][string]$Message, [Parameter(Mandatory)][string]$Message,
[ValidateSet("Info","Warning","Error","Success","General")][string]$Level = "Info", [ValidateSet("Info","Warning","Error","Success","General")]
[string]$TaskCategory = "GeneralTask", [string]$Level = "Info",
[switch]$LogToEvent, [string]$EventSource="SVSMSP_Module", [string]$EventLog="Application", [string]$TaskCategory = "GeneralTask",
[int]$CustomEventID, [string]$LogFile, [switch]$PassThru [switch]$LogToEvent,
[string]$EventSource = "Script Automation Monkey",
[string]$EventLog = "SVS Scripting",
[int] $CustomEventID,
[string]$LogFile,
[switch]$PassThru
) )
# IDs & colors
$idMap = @{ Info=1000; Warning=2000; Error=3000; Success=4000; General=1000 } # ─── IDs & Colors ────────────────────────────────────────────────
$colMap= @{ Info="Cyan"; Warning="Yellow"; Error="Red"; Success="Green"; General="White" } $idMap = @{ Info=1000; Warning=2000; Error=3000; Success=4000; General=1000 }
$colMap = @{ Info="Cyan"; Warning="Yellow"; Error="Red"; Success="Green"; General="White" }
$EventID = if ($PSBoundParameters.CustomEventID) { $CustomEventID } else { $idMap[$Level] } $EventID = if ($PSBoundParameters.CustomEventID) { $CustomEventID } else { $idMap[$Level] }
$color = $colMap[$Level] $color = $colMap[$Level]
$fmt = "[$Level] [$TaskCategory] $Message (Event ID: $EventID)" $fmt = "[$Level] [$TaskCategory] $Message (Event ID: $EventID)"
# ─── Console Output ─────────────────────────────────────────────
Write-Host $fmt -ForegroundColor $color Write-Host $fmt -ForegroundColor $color
# cache # ─── In-Memory Cache ─────────────────────────────────────────────
if (-not $Global:LogCache) { $Global:LogCache = @() } if (-not $Global:LogCache) { $Global:LogCache = [System.Collections.ArrayList]::new() }
$entry = [pscustomobject]@{ Timestamp=(Get-Date -Format "yyyy-MM-dd HH:mm:ss"); Level=$Level; Message=$fmt } $Global:LogCache.Add([pscustomobject]@{
$Global:LogCache += $entry Timestamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')
Level = $Level
Message = $fmt
}) | Out-Null
# file # ─── File Logging ────────────────────────────────────────────────
if ($PSBoundParameters.LogFile) { if ($PSBoundParameters.LogFile) {
try { "$($entry.Timestamp) $fmt" | Out-File $LogFile -Append -Encoding UTF8 }
catch { Write-Host "[Warning] File log failed: $_" -ForegroundColor Yellow }
}
# event log
if ($LogToEvent) {
$etype = if ($Level -in 'Warning','Error') { $Level } else { 'Information' }
try { try {
if (-not (Get-EventLog -LogName $EventLog -Source $EventSource -ErrorAction SilentlyContinue)) { "$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) $fmt" |
New-EventLog -LogName $EventLog -Source $EventSource Out-File -FilePath $LogFile -Append -Encoding UTF8
} }
$msg = "TaskCategory:$TaskCategory | Message:$Message" catch {
Write-EventLog -LogName $EventLog -Source $EventSource -EntryType $etype -EventID $EventID -Message $msg Write-Host "[Warning] File log failed: $_" -ForegroundColor Yellow
} catch { Write-Host "[Warning] EventLog failed: $_" -ForegroundColor Yellow } }
} }
if ($PassThru) { return $entry } # ─── Event Log ──────────────────────────────────────────────────
} if ($LogToEvent) {
# 1) Ensure your custom source/log exist
if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) {
New-EventLog -LogName $EventLog -Source $EventSource
}
# 2) Map level to entry type
$entryType = if ($Level -in 'Warning','Error') { $Level } else { 'Information' }
# 3) Write to the Windows event log
try {
Write-EventLog `
-LogName $EventLog `
-Source $EventSource `
-EntryType $entryType `
-EventID $EventID `
-Message $fmt
}
catch {
Write-Host "[Warning] EventLog failed: $_" -ForegroundColor Yellow
}
}
# ─── PassThru ────────────────────────────────────────────────────
if ($PassThru) { return $Global:LogCache[-1] }
}
# ───────────────────────────────────────────────────────────────────────── # ─────────────────────────────────────────────────────────────────────────
# WRITE-LOG HYBRID (single definition, chooses at runtime if we use the # WRITE-LOG HYBRID (single definition, chooses at runtime if we use the
# Write-Log from the module or the built-in Write-LogHelper funtions ) # Write-Log from the module or the built-in Write-LogHelper funtions )
# ───────────────────────────────────────────────────────────────────────── # ─────────────────────────────────────────────────────────────────────────
function Write-LogHybrid { function Write-LogHybrid {
[CmdletBinding()] [CmdletBinding()]
param( param(
[Parameter(Mandatory=$true)][string]$Message, [Parameter(Mandatory=$true)][string]$Message,
[ValidateSet("Info","Warning","Error","Success","General")] [ValidateSet("Info","Warning","Error","Success","General")]
[string]$Level = "Info", [string]$Level = "Info",
[string]$TaskCategory = "GeneralTask", [string]$TaskCategory = "GeneralTask",
[switch]$LogToEvent [switch]$LogToEvent,
[string]$EventSource = "Script Automation Monkey",
[string]$EventLog = "SVS Scripting"
) )
if ( Get-Command -Name Write-Log -ErrorAction SilentlyContinue ) { if ( Get-Command -Name Write-Log -ErrorAction SilentlyContinue ) {
# SVSMSP module's Write-Log is available # Real Write-Log; pass through EventSource & EventLog too
Write-Log -Message $Message -Level $Level -TaskCategory $TaskCategory -LogToEvent:$LogToEvent Write-Log `
-Message $Message `
-Level $Level `
-TaskCategory $TaskCategory `
-LogToEvent:$LogToEvent `
-EventSource $EventSource `
-EventLog $EventLog
} }
else { else {
# fall back to your helper # Fallback helper: also forward EventSource & EventLog
Write-LogHelper -Message $Message -Level $Level -TaskCategory $TaskCategory -LogToEvent:$LogToEvent Write-LogHelper `
-Message $Message `
-Level $Level `
-TaskCategory $TaskCategory `
-LogToEvent:$LogToEvent `
-EventSource $EventSource `
-EventLog $EventLog
} }
} }
#endregion Write-Log #endregion Write-Log