Update StackMonkey.ps1

This commit is contained in:
2025-06-29 01:10:51 -04:00
parent d77d46fe54
commit 4c3a935aaf

View File

@@ -183,41 +183,47 @@ if (-not $Global:LogCache -or -not ($Global:LogCache -is [System.Collections.Arr
}
# Core Write-Log function (advanced with event-log support)
function Write-LogHelper {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Message,
[ValidateSet("Info","Warning","Error","Success","General")]
[string]$Level = "Info",
[string]$TaskCategory = "GeneralTask",
[switch]$LogToEvent,
[string]$EventSource = "SVSMSP_Module",
[string]$EventLog = "Application",
[int]$CustomEventID
)
$EventID = @{ Info=1000; Warning=2000; Error=3000; Success=4000; General=1000 }[$Level]
#$Icon = @{Info=[System.Char]::ConvertFromUtf32(0x1F4CB);Warning=[char]0x26A0;Error=[char]0x274C;Success=[char]0x2705;General=[char]0x1F4E6}[$Level]
$logEntry = [PSCustomObject]@{
Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
Level = $Level
Message = "$Icon [$Level] [$TaskCategory] $Message (EventID:$EventID)"
}
[void]$Global:LogCache.Add($logEntry)
function Write-LogHelper {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Message,
[ValidateSet("Info","Warning","Error","Success","General")][string]$Level = "Info",
[string]$TaskCategory = "GeneralTask",
[switch]$LogToEvent, [string]$EventSource="SVSMSP_Module", [string]$EventLog="Application",
[int]$CustomEventID, [string]$LogFile, [switch]$PassThru
)
# IDs & colors
$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] }
$color = $colMap[$Level]
$fmt = "[$Level] [$TaskCategory] $Message (Event ID: $EventID)"
Write-Host $fmt -ForegroundColor $color
if ($LogToEvent) {
try {
if (-not (Get-EventLog -LogName $EventLog -Source $EventSource -ErrorAction SilentlyContinue)) {
New-EventLog -LogName $EventLog -Source $EventSource -ErrorAction SilentlyContinue
}
Write-EventLog -LogName $EventLog -Source $EventSource `
-EntryType $Level -EventId $EventID `
-Message $Message
} catch {
Write-Host "$([System.Char]::ConvertFromUtf32(0x26A0))$([System.Char]::ConvertFromUtf32(0xFE0F)) [Warning] [EventLog] Failed to write to Event Log: $($_.Exception.Message)" -ForegroundColor Yellow
# cache
if (-not $Global:LogCache) { $Global:LogCache = @() }
$entry = [pscustomobject]@{ Timestamp=(Get-Date -Format "yyyy-MM-dd HH:mm:ss"); Level=$Level; Message=$fmt }
$Global:LogCache += $entry
# file
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 {
if (-not (Get-EventLog -LogName $EventLog -Source $EventSource -ErrorAction SilentlyContinue)) {
New-EventLog -LogName $EventLog -Source $EventSource
}
$msg = "TaskCategory:$TaskCategory | Message:$Message"
Write-EventLog -LogName $EventLog -Source $EventSource -EntryType $etype -EventID $EventID -Message $msg
} catch { Write-Host "[Warning] EventLog failed: $_" -ForegroundColor Yellow }
}
if ($PassThru) { return $entry }
}
# ─────────────────────────────────────────────────────────────────────────