From 57400ac8ff8b7050fbc5f3f83f4d27740f365c1c Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Wed, 25 Jun 2025 00:18:04 -0400 Subject: [PATCH] moved the Write-LogHelper function to the top --- StackMonkey.ps1 | 75 +++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/StackMonkey.ps1 b/StackMonkey.ps1 index 9bdc881..e03e98f 100644 --- a/StackMonkey.ps1 +++ b/StackMonkey.ps1 @@ -119,6 +119,45 @@ function Get-DattoApiCredentials { return $null } } + +# Core Write-Log function (advanced with event-log support) +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 = "SVSMSP_Module", + [string]$EventLog = "Application", + [int]$CustomEventID + ) + $EventID = @{ Info=1000; Warning=2000; Error=3000; Success=4000; General=1000 }[$Level] + #$Icon = @{Info=[System.Char]::ConvertFromUtf32(0x1F4CB);Warning=[char]0x26A0;Error=[char]0x274C;Success=[char]0x2705;General=[char]0x1F4E6}[$Level] + $logEntry = [PSCustomObject]@{ + Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") + Level = $Level + Message = "$Icon [$Level] [$TaskCategory] $Message (EventID:$EventID)" + } + [void]$Global:LogCache.Add($logEntry) + + if ($LogToEvent) { + try { + if (-not (Get-EventLog -LogName $EventLog -Source $EventSource -ErrorAction SilentlyContinue)) { + New-EventLog -LogName $EventLog -Source $EventSource -ErrorAction SilentlyContinue + } + Write-EventLog -LogName $EventLog -Source $EventSource ` + -EntryType $Level -EventId $EventID ` + -Message $Message + } catch { + Write-Host "$([System.Char]::ConvertFromUtf32(0x26A0))$([System.Char]::ConvertFromUtf32(0xFE0F)) [Warning] [EventLog] Failed to write to Event Log: $($_.Exception.Message)" -ForegroundColor Yellow + + + } + } +} + # ───────────────────────────────────────────────────────────────────────── # 3) MAIN LOGIC (Toolkit vs Datto vs UI) # ───────────────────────────────────────────────────────────────────────── @@ -230,43 +269,7 @@ if (-not $Global:LogCache -or -not ($Global:LogCache -is [System.Collections.Arr $Global:LogCache = [System.Collections.ArrayList]::new() } -# Core Write-Log function (advanced with event-log support) -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 = "SVSMSP_Module", - [string]$EventLog = "Application", - [int]$CustomEventID - ) - $EventID = @{ Info=1000; Warning=2000; Error=3000; Success=4000; General=1000 }[$Level] - #$Icon = @{Info=[System.Char]::ConvertFromUtf32(0x1F4CB);Warning=[char]0x26A0;Error=[char]0x274C;Success=[char]0x2705;General=[char]0x1F4E6}[$Level] - $logEntry = [PSCustomObject]@{ - Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") - Level = $Level - Message = "$Icon [$Level] [$TaskCategory] $Message (EventID:$EventID)" - } - [void]$Global:LogCache.Add($logEntry) - if ($LogToEvent) { - try { - if (-not (Get-EventLog -LogName $EventLog -Source $EventSource -ErrorAction SilentlyContinue)) { - New-EventLog -LogName $EventLog -Source $EventSource -ErrorAction SilentlyContinue - } - Write-EventLog -LogName $EventLog -Source $EventSource ` - -EntryType $Level -EventId $EventID ` - -Message $Message - } catch { - Write-Host "$([System.Char]::ConvertFromUtf32(0x26A0))$([System.Char]::ConvertFromUtf32(0xFE0F)) [Warning] [EventLog] Failed to write to Event Log: $($_.Exception.Message)" -ForegroundColor Yellow - - - } - } -} # Hybrid wrapper: uses your module's Write-Log if available, else falls back if (Get-Command Write-Log -ErrorAction SilentlyContinue) {