moved the Write-LogHelper function to the top
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user