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,23 +289,35 @@ function global:Write-LogHybrid {
param( param(
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
[string]$Message, [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 = "SVSMSP_Module", [string]$EventSource = "SVSMSP_Module",
[string]$EventLog = "SVSMSP Events", [string]$EventLog = "SVSMSP Events",
# New feature: only used by Write-LogHelper (fallback) unless the primary logger supports it
[ValidateSet('Repair', 'Unique', 'Follow')] [ValidateSet('Repair', 'Unique', 'Follow')]
[string]$EventLogConflictPolicy = 'Repair', [string]$EventLogConflictPolicy = 'Repair',
[int]$CustomEventID, [int]$CustomEventID,
[string]$LogFile, [string]$LogFile,
[switch]$PassThru, [switch]$PassThru,
[ValidateSet("Black","DarkGray","Gray","White","Red","Green","Blue","Yellow","Magenta","Cyan")] [ValidateSet("Black","DarkGray","Gray","White","Red","Green","Blue","Yellow","Magenta","Cyan")]
[string]$ForegroundColorOverride [string]$ForegroundColorOverride
) )
$formatted = "[$Level] [$TaskCategory] $Message" $formatted = "[$Level] [$TaskCategory] $Message"
# Full parameter set we *might* send
$invokeParams = @{ $invokeParams = @{
Message = $Message Message = $Message
Level = $Level Level = $Level
@@ -313,22 +325,35 @@ function global:Write-LogHybrid {
LogToEvent = $LogToEvent LogToEvent = $LogToEvent
EventSource = $EventSource EventSource = $EventSource
EventLog = $EventLog EventLog = $EventLog
EventLogConflictPolicy = $EventLogConflictPolicy
} }
if ($PSBoundParameters.ContainsKey('CustomEventID')) { $invokeParams.CustomEventID = $CustomEventID } if ($PSBoundParameters.ContainsKey('CustomEventID')) { $invokeParams.CustomEventID = $CustomEventID }
if ($PSBoundParameters.ContainsKey('LogFile')) { $invokeParams.LogFile = $LogFile } if ($PSBoundParameters.ContainsKey('LogFile')) { $invokeParams.LogFile = $LogFile }
if ($PassThru) { $invokeParams.PassThru = $true } 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')) { if ($PSBoundParameters.ContainsKey('ForegroundColorOverride')) {
Write-Host $formatted -ForegroundColor $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 } $primary = Get-Command Write-Log -ErrorAction SilentlyContinue
else { Write-LogHelper @invokeParams } 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 #endregion Public: Write-LogHybrid