Update src/logging.fallback.ps1
This commit is contained in:
@@ -289,23 +289,35 @@ 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
|
||||
@@ -313,22 +325,35 @@ function global:Write-LogHybrid {
|
||||
LogToEvent = $LogToEvent
|
||||
EventSource = $EventSource
|
||||
EventLog = $EventLog
|
||||
EventLogConflictPolicy = $EventLogConflictPolicy
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user