Update StackMonkey.ps1
This commit is contained in:
@@ -187,37 +187,43 @@ function Write-LogHelper {
|
|||||||
[CmdletBinding()]
|
[CmdletBinding()]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)][string]$Message,
|
[Parameter(Mandatory)][string]$Message,
|
||||||
[ValidateSet("Info","Warning","Error","Success","General")]
|
[ValidateSet("Info","Warning","Error","Success","General")][string]$Level = "Info",
|
||||||
[string]$Level = "Info",
|
|
||||||
[string]$TaskCategory = "GeneralTask",
|
[string]$TaskCategory = "GeneralTask",
|
||||||
[switch]$LogToEvent,
|
[switch]$LogToEvent, [string]$EventSource="SVSMSP_Module", [string]$EventLog="Application",
|
||||||
[string]$EventSource = "SVSMSP_Module",
|
[int]$CustomEventID, [string]$LogFile, [switch]$PassThru
|
||||||
[string]$EventLog = "Application",
|
|
||||||
[int]$CustomEventID
|
|
||||||
)
|
)
|
||||||
$EventID = @{ Info=1000; Warning=2000; Error=3000; Success=4000; General=1000 }[$Level]
|
# IDs & colors
|
||||||
#$Icon = @{Info=[System.Char]::ConvertFromUtf32(0x1F4CB);Warning=[char]0x26A0;Error=[char]0x274C;Success=[char]0x2705;General=[char]0x1F4E6}[$Level]
|
$idMap = @{ Info=1000; Warning=2000; Error=3000; Success=4000; General=1000 }
|
||||||
$logEntry = [PSCustomObject]@{
|
$colMap= @{ Info="Cyan"; Warning="Yellow"; Error="Red"; Success="Green"; General="White" }
|
||||||
Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
|
$EventID = if ($PSBoundParameters.CustomEventID) { $CustomEventID } else { $idMap[$Level] }
|
||||||
Level = $Level
|
$color = $colMap[$Level]
|
||||||
Message = "$Icon [$Level] [$TaskCategory] $Message (EventID:$EventID)"
|
$fmt = "[$Level] [$TaskCategory] $Message (Event ID: $EventID)"
|
||||||
}
|
Write-Host $fmt -ForegroundColor $color
|
||||||
[void]$Global:LogCache.Add($logEntry)
|
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# file
|
||||||
|
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) {
|
if ($LogToEvent) {
|
||||||
|
$etype = if ($Level -in 'Warning','Error') { $Level } else { 'Information' }
|
||||||
try {
|
try {
|
||||||
if (-not (Get-EventLog -LogName $EventLog -Source $EventSource -ErrorAction SilentlyContinue)) {
|
if (-not (Get-EventLog -LogName $EventLog -Source $EventSource -ErrorAction SilentlyContinue)) {
|
||||||
New-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 }
|
||||||
}
|
}
|
||||||
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
|
|
||||||
|
|
||||||
|
if ($PassThru) { return $entry }
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# ─────────────────────────────────────────────────────────────────────────
|
# ─────────────────────────────────────────────────────────────────────────
|
||||||
|
|||||||
Reference in New Issue
Block a user