From 220ecd0842ceabca3607dc9ed7b64494913e82da Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Tue, 9 Dec 2025 23:25:14 -0500 Subject: [PATCH] Update module/Samy.Logging.ps1 --- module/Samy.Logging.ps1 | 56 +++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/module/Samy.Logging.ps1 b/module/Samy.Logging.ps1 index 45ba6d6..ffa3a62 100644 --- a/module/Samy.Logging.ps1 +++ b/module/Samy.Logging.ps1 @@ -1,10 +1,36 @@ -# Samy.Logging.ps1 -# Logging helpers and Write-LogHybrid fallback +<# +.SYNOPSIS + Core logging utilities for SAMY. + +.DESCRIPTION + Provides: + - Write-LogHelper : standalone logger with console, file, and Event Log support. + - Write-LogHybrid : wrapper that prefers toolkit Write-Log if present, else falls back. + + This module is loaded first so that other subsystems can safely call Write-LogHybrid. +#> + +# Ensure global log structures exist +if (-not $Global:LogCache -or -not ($Global:LogCache -is [System.Collections.ArrayList])) { + $Global:LogCache = [System.Collections.ArrayList]::new() +} + +if (-not $Global:EventSourceInitState) { + $Global:EventSourceInitState = @{} +} function Write-LogHelper { <# .SYNOPSIS - Simple logging utility with console, file, and Windows Event Log support. + Standardized logging utility with console/file output and Windows Event Log support. + + .DESCRIPTION + Mirrors the SVSMSP toolkit Write-Log so that Write-LogHybrid can safely fall back + when the module is not loaded. + + .NOTES + Default EventLog : SVSMSP Events + Default Source : SVSMSP_Module #> [CmdletBinding()] param ( @@ -29,6 +55,7 @@ function Write-LogHelper { [switch]$PassThru ) + # Event ID and console color $EventID = if ($CustomEventID) { $CustomEventID } else { switch ($Level) { "Info" { 1000 } @@ -47,9 +74,10 @@ function Write-LogHelper { default { "White" } } - $FormattedMessage = "[$Level] [$TaskCategory] $Message (Event ID: $EventID)" + $FormattedMessage = "[{0}] [{1}] {2} (Event ID: {3})" -f $Level, $TaskCategory, $Message, $EventID Write-Host $FormattedMessage -ForegroundColor $Color + # In-memory cache if (-not $Global:LogCache -or -not ($Global:LogCache -is [System.Collections.ArrayList])) { $Global:LogCache = [System.Collections.ArrayList]::new() } @@ -61,9 +89,10 @@ function Write-LogHelper { } [void]$Global:LogCache.Add($logEntry) + # Optional file output if ($LogFile) { try { - "$($logEntry.Timestamp) $FormattedMessage" | + "{0} {1}" -f $logEntry.Timestamp, $FormattedMessage | Out-File -FilePath $LogFile -Append -Encoding UTF8 } catch { @@ -71,8 +100,8 @@ function Write-LogHelper { } } + # Windows Event Log (with one-time init) if ($LogToEvent) { - if (-not $Global:EventSourceInitState) { $Global:EventSourceInitState = @{} } @@ -85,7 +114,7 @@ function Write-LogHelper { default { "Information" } } - $sourceKey = "$EventLog|$EventSource" + $sourceKey = "{0}|{1}" -f $EventLog, $EventSource if (-not $Global:EventSourceInitState.ContainsKey($sourceKey) -or -not $Global:EventSourceInitState[$sourceKey]) { @@ -93,6 +122,7 @@ function Write-LogHelper { try { if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) { + # Check if current token is admin $isAdmin = $false try { $current = [Security.Principal.WindowsIdentity]::GetCurrent() @@ -117,7 +147,7 @@ if (-not [System.Diagnostics.EventLog]::SourceExists('$EventSource')) { $tempPath = [System.IO.Path]::Combine( $env:TEMP, - "Init_${EventLog}_$EventSource.ps1".Replace(' ', '_') + ("Init_{0}_{1}.ps1" -f $EventLog, $EventSource).Replace(' ', '_') ) $helperScript | Set-Content -Path $tempPath -Encoding UTF8 @@ -152,7 +182,7 @@ if (-not [System.Diagnostics.EventLog]::SourceExists('$EventSource')) { if ($Global:EventSourceInitState[$sourceKey]) { try { - $EventMessage = "TaskCategory: $TaskCategory | Message: $Message" + $EventMessage = "TaskCategory: {0} | Message: {1}" -f $TaskCategory, $Message Write-EventLog -LogName $EventLog -Source $EventSource -EntryType $EntryType -EventId $EventID -Message $EventMessage } catch { @@ -167,6 +197,10 @@ if (-not [System.Diagnostics.EventLog]::SourceExists('$EventSource')) { } function Write-LogHybrid { + <# + .SYNOPSIS + Wrapper that prefers SVSMSP Write-Log if available, else falls back to Write-LogHelper. + #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] @@ -181,7 +215,7 @@ function Write-LogHybrid { [string]$EventSource = "SVSMSP_Module", - [string]$EventLog = "SVSMSP Events", + [string]$EventLog = "SVSMSP Events", [int]$CustomEventID, @@ -193,7 +227,7 @@ function Write-LogHybrid { [string]$ForegroundColorOverride ) - $formatted = "[$Level] [$TaskCategory] $Message" + $formatted = "[{0}] [{1}] {2}" -f $Level, $TaskCategory, $Message $invokeParams = @{ Message = $Message