Update module/Samy.Logging.ps1
This commit is contained in:
@@ -1,10 +1,36 @@
|
|||||||
# Samy.Logging.ps1
|
<#
|
||||||
# Logging helpers and Write-LogHybrid fallback
|
.SYNOPSIS
|
||||||
|
Core logging utilities for SAMY.
|
||||||
|
|
||||||
|
.DESCRIPTION
|
||||||
|
Provides:
|
||||||
|
- Write-LogHelper : standalone logger with console, file, and Event Log support.
|
||||||
|
- Write-LogHybrid : wrapper that prefers toolkit Write-Log if present, else falls back.
|
||||||
|
|
||||||
|
This module is loaded first so that other subsystems can safely call Write-LogHybrid.
|
||||||
|
#>
|
||||||
|
|
||||||
|
# Ensure global log structures exist
|
||||||
|
if (-not $Global:LogCache -or -not ($Global:LogCache -is [System.Collections.ArrayList])) {
|
||||||
|
$Global:LogCache = [System.Collections.ArrayList]::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not $Global:EventSourceInitState) {
|
||||||
|
$Global:EventSourceInitState = @{}
|
||||||
|
}
|
||||||
|
|
||||||
function Write-LogHelper {
|
function Write-LogHelper {
|
||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Simple logging utility with console, file, and Windows Event Log support.
|
Standardized logging utility with console/file output and Windows Event Log support.
|
||||||
|
|
||||||
|
.DESCRIPTION
|
||||||
|
Mirrors the SVSMSP toolkit Write-Log so that Write-LogHybrid can safely fall back
|
||||||
|
when the module is not loaded.
|
||||||
|
|
||||||
|
.NOTES
|
||||||
|
Default EventLog : SVSMSP Events
|
||||||
|
Default Source : SVSMSP_Module
|
||||||
#>
|
#>
|
||||||
[CmdletBinding()]
|
[CmdletBinding()]
|
||||||
param (
|
param (
|
||||||
@@ -29,6 +55,7 @@ function Write-LogHelper {
|
|||||||
[switch]$PassThru
|
[switch]$PassThru
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Event ID and console color
|
||||||
$EventID = if ($CustomEventID) { $CustomEventID } else {
|
$EventID = if ($CustomEventID) { $CustomEventID } else {
|
||||||
switch ($Level) {
|
switch ($Level) {
|
||||||
"Info" { 1000 }
|
"Info" { 1000 }
|
||||||
@@ -47,9 +74,10 @@ function Write-LogHelper {
|
|||||||
default { "White" }
|
default { "White" }
|
||||||
}
|
}
|
||||||
|
|
||||||
$FormattedMessage = "[$Level] [$TaskCategory] $Message (Event ID: $EventID)"
|
$FormattedMessage = "[{0}] [{1}] {2} (Event ID: {3})" -f $Level, $TaskCategory, $Message, $EventID
|
||||||
Write-Host $FormattedMessage -ForegroundColor $Color
|
Write-Host $FormattedMessage -ForegroundColor $Color
|
||||||
|
|
||||||
|
# In-memory cache
|
||||||
if (-not $Global:LogCache -or -not ($Global:LogCache -is [System.Collections.ArrayList])) {
|
if (-not $Global:LogCache -or -not ($Global:LogCache -is [System.Collections.ArrayList])) {
|
||||||
$Global:LogCache = [System.Collections.ArrayList]::new()
|
$Global:LogCache = [System.Collections.ArrayList]::new()
|
||||||
}
|
}
|
||||||
@@ -61,9 +89,10 @@ function Write-LogHelper {
|
|||||||
}
|
}
|
||||||
[void]$Global:LogCache.Add($logEntry)
|
[void]$Global:LogCache.Add($logEntry)
|
||||||
|
|
||||||
|
# Optional file output
|
||||||
if ($LogFile) {
|
if ($LogFile) {
|
||||||
try {
|
try {
|
||||||
"$($logEntry.Timestamp) $FormattedMessage" |
|
"{0} {1}" -f $logEntry.Timestamp, $FormattedMessage |
|
||||||
Out-File -FilePath $LogFile -Append -Encoding UTF8
|
Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
@@ -71,8 +100,8 @@ function Write-LogHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Windows Event Log (with one-time init)
|
||||||
if ($LogToEvent) {
|
if ($LogToEvent) {
|
||||||
|
|
||||||
if (-not $Global:EventSourceInitState) {
|
if (-not $Global:EventSourceInitState) {
|
||||||
$Global:EventSourceInitState = @{}
|
$Global:EventSourceInitState = @{}
|
||||||
}
|
}
|
||||||
@@ -85,7 +114,7 @@ function Write-LogHelper {
|
|||||||
default { "Information" }
|
default { "Information" }
|
||||||
}
|
}
|
||||||
|
|
||||||
$sourceKey = "$EventLog|$EventSource"
|
$sourceKey = "{0}|{1}" -f $EventLog, $EventSource
|
||||||
|
|
||||||
if (-not $Global:EventSourceInitState.ContainsKey($sourceKey) -or
|
if (-not $Global:EventSourceInitState.ContainsKey($sourceKey) -or
|
||||||
-not $Global:EventSourceInitState[$sourceKey]) {
|
-not $Global:EventSourceInitState[$sourceKey]) {
|
||||||
@@ -93,6 +122,7 @@ function Write-LogHelper {
|
|||||||
try {
|
try {
|
||||||
if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) {
|
if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) {
|
||||||
|
|
||||||
|
# Check if current token is admin
|
||||||
$isAdmin = $false
|
$isAdmin = $false
|
||||||
try {
|
try {
|
||||||
$current = [Security.Principal.WindowsIdentity]::GetCurrent()
|
$current = [Security.Principal.WindowsIdentity]::GetCurrent()
|
||||||
@@ -117,7 +147,7 @@ if (-not [System.Diagnostics.EventLog]::SourceExists('$EventSource')) {
|
|||||||
|
|
||||||
$tempPath = [System.IO.Path]::Combine(
|
$tempPath = [System.IO.Path]::Combine(
|
||||||
$env:TEMP,
|
$env:TEMP,
|
||||||
"Init_${EventLog}_$EventSource.ps1".Replace(' ', '_')
|
("Init_{0}_{1}.ps1" -f $EventLog, $EventSource).Replace(' ', '_')
|
||||||
)
|
)
|
||||||
|
|
||||||
$helperScript | Set-Content -Path $tempPath -Encoding UTF8
|
$helperScript | Set-Content -Path $tempPath -Encoding UTF8
|
||||||
@@ -152,7 +182,7 @@ if (-not [System.Diagnostics.EventLog]::SourceExists('$EventSource')) {
|
|||||||
|
|
||||||
if ($Global:EventSourceInitState[$sourceKey]) {
|
if ($Global:EventSourceInitState[$sourceKey]) {
|
||||||
try {
|
try {
|
||||||
$EventMessage = "TaskCategory: $TaskCategory | Message: $Message"
|
$EventMessage = "TaskCategory: {0} | Message: {1}" -f $TaskCategory, $Message
|
||||||
Write-EventLog -LogName $EventLog -Source $EventSource -EntryType $EntryType -EventId $EventID -Message $EventMessage
|
Write-EventLog -LogName $EventLog -Source $EventSource -EntryType $EntryType -EventId $EventID -Message $EventMessage
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
@@ -167,6 +197,10 @@ if (-not [System.Diagnostics.EventLog]::SourceExists('$EventSource')) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Write-LogHybrid {
|
function Write-LogHybrid {
|
||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Wrapper that prefers SVSMSP Write-Log if available, else falls back to Write-LogHelper.
|
||||||
|
#>
|
||||||
[CmdletBinding()]
|
[CmdletBinding()]
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory = $true)]
|
[Parameter(Mandatory = $true)]
|
||||||
@@ -193,7 +227,7 @@ function Write-LogHybrid {
|
|||||||
[string]$ForegroundColorOverride
|
[string]$ForegroundColorOverride
|
||||||
)
|
)
|
||||||
|
|
||||||
$formatted = "[$Level] [$TaskCategory] $Message"
|
$formatted = "[{0}] [{1}] {2}" -f $Level, $TaskCategory, $Message
|
||||||
|
|
||||||
$invokeParams = @{
|
$invokeParams = @{
|
||||||
Message = $Message
|
Message = $Message
|
||||||
|
|||||||
Reference in New Issue
Block a user