updated the write-loghelper and write-loghybrid
This commit is contained in:
113
StackMonkey.ps1
113
StackMonkey.ps1
@@ -293,75 +293,118 @@
|
||||
|
||||
#region Write-Log
|
||||
|
||||
# This function is used as a fallback if the SVSMSP module is not installed
|
||||
# This function is used as a fallback if the SVSMSP module is not installed
|
||||
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, [string]$LogFile, [switch]$PassThru
|
||||
[ValidateSet("Info","Warning","Error","Success","General")]
|
||||
[string]$Level = "Info",
|
||||
[string]$TaskCategory = "GeneralTask",
|
||||
[switch]$LogToEvent,
|
||||
[string]$EventSource = "Script Automation Monkey",
|
||||
[string]$EventLog = "SVS Scripting",
|
||||
[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" }
|
||||
|
||||
# ─── 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
|
||||
|
||||
# cache
|
||||
if (-not $Global:LogCache) { $Global:LogCache = @() }
|
||||
$entry = [pscustomobject]@{ Timestamp=(Get-Date -Format "yyyy-MM-dd HH:mm:ss"); Level=$Level; Message=$fmt }
|
||||
$Global:LogCache += $entry
|
||||
# ─── In-Memory Cache ─────────────────────────────────────────────
|
||||
if (-not $Global:LogCache) { $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
|
||||
# ─── File Logging ────────────────────────────────────────────────
|
||||
if ($PSBoundParameters.LogFile) {
|
||||
try { "$($entry.Timestamp) $fmt" | Out-File $LogFile -Append -Encoding UTF8 }
|
||||
catch { Write-Host "[Warning] File log failed: $_" -ForegroundColor Yellow }
|
||||
}
|
||||
|
||||
# event log
|
||||
if ($LogToEvent) {
|
||||
$etype = if ($Level -in 'Warning','Error') { $Level } else { 'Information' }
|
||||
try {
|
||||
if (-not (Get-EventLog -LogName $EventLog -Source $EventSource -ErrorAction SilentlyContinue)) {
|
||||
New-EventLog -LogName $EventLog -Source $EventSource
|
||||
}
|
||||
$msg = "TaskCategory:$TaskCategory | Message:$Message"
|
||||
Write-EventLog -LogName $EventLog -Source $EventSource -EntryType $etype -EventID $EventID -Message $msg
|
||||
} catch { Write-Host "[Warning] EventLog failed: $_" -ForegroundColor Yellow }
|
||||
"$((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
|
||||
}
|
||||
}
|
||||
|
||||
if ($PassThru) { return $entry }
|
||||
}
|
||||
# ─── Event Log ──────────────────────────────────────────────────
|
||||
if ($LogToEvent) {
|
||||
# 1) Ensure your custom source/log exist
|
||||
if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) {
|
||||
New-EventLog -LogName $EventLog -Source $EventSource
|
||||
}
|
||||
|
||||
# 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: $_" -ForegroundColor Yellow
|
||||
}
|
||||
}
|
||||
|
||||
# ─── PassThru ────────────────────────────────────────────────────
|
||||
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 )
|
||||
# ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
function Write-LogHybrid {
|
||||
function Write-LogHybrid {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory=$true)][string]$Message,
|
||||
[ValidateSet("Info","Warning","Error","Success","General")]
|
||||
[string]$Level = "Info",
|
||||
[string]$TaskCategory = "GeneralTask",
|
||||
[switch]$LogToEvent
|
||||
[switch]$LogToEvent,
|
||||
[string]$EventSource = "Script Automation Monkey",
|
||||
[string]$EventLog = "SVS Scripting"
|
||||
)
|
||||
|
||||
if ( Get-Command -Name Write-Log -ErrorAction SilentlyContinue ) {
|
||||
# SVSMSP module's Write-Log is available
|
||||
Write-Log -Message $Message -Level $Level -TaskCategory $TaskCategory -LogToEvent:$LogToEvent
|
||||
# Real Write-Log; pass through EventSource & EventLog too
|
||||
Write-Log `
|
||||
-Message $Message `
|
||||
-Level $Level `
|
||||
-TaskCategory $TaskCategory `
|
||||
-LogToEvent:$LogToEvent `
|
||||
-EventSource $EventSource `
|
||||
-EventLog $EventLog
|
||||
}
|
||||
else {
|
||||
# fall back to your helper
|
||||
Write-LogHelper -Message $Message -Level $Level -TaskCategory $TaskCategory -LogToEvent:$LogToEvent
|
||||
# Fallback helper: also forward EventSource & EventLog
|
||||
Write-LogHelper `
|
||||
-Message $Message `
|
||||
-Level $Level `
|
||||
-TaskCategory $TaskCategory `
|
||||
-LogToEvent:$LogToEvent `
|
||||
-EventSource $EventSource `
|
||||
-EventLog $EventLog
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endregion Write-Log
|
||||
|
||||
|
||||
Reference in New Issue
Block a user