removed duplicate write-loghelper and hybrid

This commit is contained in:
2025-11-26 21:20:09 -05:00
parent 0b54fee99d
commit 45913df719

154
samy.ps1
View File

@@ -408,8 +408,6 @@ $ConfirmPreference = 'None'
#endregion SVS Module
#region Write-Log
# Fallback logger used when the SVSMSP module (and its Write-Log) is not available.
@@ -690,158 +688,6 @@ if (-not [System.Diagnostics.EventLog]::SourceExists('$EventSource')) {
#endregion Write-Log
# This function is used as a fallback if the SVSMSP module is not installed
# Should change this "[string]$EventLog = "Application", => [string]$EventLog = "SVS Scripting", "
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 = "Script Automation Monkey",
[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)"
# ─── Console Output ─────────────────────────────────────────────
Write-Host $fmt -ForegroundColor $color
# ─── In-Memory Cache ─────────────────────────────────────────────
# ─── In-Memory Cache ─────────────────────────────────────────────
if (-not $Global:LogCache -or -not ($Global:LogCache -is [System.Collections.ArrayList])) {
$Global:LogCache = [System.Collections.ArrayList]::new()
}
$Global:LogCache.Add([pscustomobject]@{
Timestamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')
Level = $Level
Message = $fmt
}) | Out-Null
# ─── File Logging ────────────────────────────────────────────────
if ($PSBoundParameters.LogFile) {
try {
"$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) $fmt" |
Out-File -FilePath $LogFile -Append -Encoding UTF8
}
catch {
Write-Host "[Warning] File log failed: $_" -ForegroundColor Yellow
}
}
# ─── Event Log ──────────────────────────────────────────────────
if ($LogToEvent) {
try {
# 1) Ensure your custom source/log exist
if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) {
New-EventLog -LogName $EventLog -Source $EventSource -ErrorAction Stop
}
} catch {
Write-Host "[Warning] Could not create event log '$EventLog' or source '$EventSource': $($_.Exception.Message)" -ForegroundColor Yellow
return
}
# 2) Map level to entry type
$entryType = if ($Level -in 'Warning','Error') { $Level } else { 'Information' }
# 3) Write to the Windows event log
try {
Write-EventLog `
-LogName $EventLog `
-Source $EventSource `
-EntryType $entryType `
-EventID $EventID `
-Message $fmt
}
catch {
Write-Host "[Warning] EventLog failed: $($_.Exception.Message)" -ForegroundColor Yellow
}
}
if ($PassThru) { return $Global:LogCache[-1] }
}
# ─────────────────────────────────────────────────────────────────────────
# WRITE-LOG HYBRID (single definition, chooses at runtime if we use the
# Write-Log from the module or the built-in Write-LogHelper funtions )
# Should chanfge this "[string]$EventLog = "Application"," => "[string]$EventLog = "SVS Scripting","
# ─────────────────────────────────────────────────────────────────────────
function Write-LogHybrid {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)][string]$Message,
[ValidateSet("Info","Warning","Error","Success","General")]
[string]$Level = "Info",
[string]$TaskCategory = "GeneralTask",
[switch]$LogToEvent,
[string]$EventSource = "Script Automation Monkey",
[string]$EventLog = "Application",
[ValidateSet("Black","DarkGray","Gray","White","Red","Green","Blue","Yellow","Magenta","Cyan")]
[string]$ForegroundColorOverride
)
$formatted = "[$Level] [$TaskCategory] $Message"
if ($PSBoundParameters.ContainsKey('ForegroundColorOverride')) {
# 1) print to console with the override color
Write-Host $formatted -ForegroundColor $ForegroundColorOverride
# 2) then forward the call (sans the override) to Write-Log or Write-LogHelper
$invokeParams = @{
Message = $Message
Level = $Level
TaskCategory = $TaskCategory
LogToEvent = $LogToEvent
EventSource = $EventSource
EventLog = $EventLog
}
if (Get-Command Write-Log -ErrorAction SilentlyContinue) {
Write-Log @invokeParams
}
else {
Write-LogHelper @invokeParams
}
}
else {
# No override: let Write-Log / Write-LogHelper handle everything (including console color)
if (Get-Command Write-Log -ErrorAction SilentlyContinue) {
Write-Log `
-Message $Message `
-Level $Level `
-TaskCategory $TaskCategory `
-LogToEvent:$LogToEvent `
-EventSource $EventSource `
-EventLog $EventLog
}
else {
Write-LogHelper `
-Message $Message `
-Level $Level `
-TaskCategory $TaskCategory `
-LogToEvent:$LogToEvent `
-EventSource $EventSource `
-EventLog $EventLog
}
}
}
#endregion Write-Log
#region building the Menus
# Define every task once here: