From 45913df7191db8d5e8c4a00447d5c28655eb916f Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Wed, 26 Nov 2025 21:20:09 -0500 Subject: [PATCH] removed duplicate write-loghelper and hybrid --- samy.ps1 | 156 +------------------------------------------------------ 1 file changed, 1 insertion(+), 155 deletions(-) diff --git a/samy.ps1 b/samy.ps1 index e1dfa2a..4803a49 100644 --- a/samy.ps1 +++ b/samy.ps1 @@ -408,9 +408,7 @@ $ConfirmPreference = 'None' #endregion SVS Module - - - #region Write-Log + #region Write-Log # Fallback logger used when the SVSMSP module (and its Write-Log) is not available. # Mirrors the behaviour of the toolkit Write-Log (v1.5), including: @@ -689,158 +687,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