Update src/logging.fallback.ps1

This commit is contained in:
2026-01-31 19:08:56 -05:00
parent bb06a7265a
commit 64a856cf08

View File

@@ -289,46 +289,71 @@ function global:Write-LogHybrid {
param(
[Parameter(Mandatory = $true)]
[string]$Message,
[ValidateSet("Info", "Warning", "Error", "Success", "General")]
[string]$Level = "Info",
[string]$TaskCategory = "GeneralTask",
[switch]$LogToEvent,
[string]$EventSource = "SVSMSP_Module",
[string]$EventLog = "SVSMSP Events",
# New feature: only used by Write-LogHelper (fallback) unless the primary logger supports it
[ValidateSet('Repair', 'Unique', 'Follow')]
[string]$EventLogConflictPolicy = 'Repair',
[int]$CustomEventID,
[string]$LogFile,
[switch]$PassThru,
[ValidateSet("Black","DarkGray","Gray","White","Red","Green","Blue","Yellow","Magenta","Cyan")]
[string]$ForegroundColorOverride
)
$formatted = "[$Level] [$TaskCategory] $Message"
# Full parameter set we *might* send
$invokeParams = @{
Message = $Message
Level = $Level
TaskCategory = $TaskCategory
LogToEvent = $LogToEvent
EventSource = $EventSource
EventLog = $EventLog
EventLogConflictPolicy = $EventLogConflictPolicy
Message = $Message
Level = $Level
TaskCategory = $TaskCategory
LogToEvent = $LogToEvent
EventSource = $EventSource
EventLog = $EventLog
}
if ($PSBoundParameters.ContainsKey('CustomEventID')) { $invokeParams.CustomEventID = $CustomEventID }
if ($PSBoundParameters.ContainsKey('LogFile')) { $invokeParams.LogFile = $LogFile }
if ($PassThru) { $invokeParams.PassThru = $true }
# Only include the new param if the target supports it
$fallbackParams = $invokeParams.Clone()
$fallbackParams.EventLogConflictPolicy = $EventLogConflictPolicy
if ($PSBoundParameters.ContainsKey('ForegroundColorOverride')) {
Write-Host $formatted -ForegroundColor $ForegroundColorOverride
if (Get-Command Write-Log -ErrorAction SilentlyContinue) { Write-Log @invokeParams }
else { Write-LogHelper @invokeParams }
}
else {
if (Get-Command Write-Log -ErrorAction SilentlyContinue) { Write-Log @invokeParams }
else { Write-LogHelper @invokeParams }
$primary = Get-Command Write-Log -ErrorAction SilentlyContinue
if ($primary) {
# Filter to only parameters supported by Write-Log
$allowed = $primary.Parameters.Keys
$filtered = @{}
foreach ($k in $invokeParams.Keys) {
if ($allowed -contains $k) { $filtered[$k] = $invokeParams[$k] }
}
Write-Log @filtered
return
}
# Fallback logger supports EventLogConflictPolicy
Write-LogHelper @fallbackParams
}
#endregion Public: Write-LogHybrid