Update TGBeta.ps1

This commit is contained in:
2025-01-27 22:59:58 -05:00
parent 899840c596
commit 409fe66975

View File

@@ -13,16 +13,24 @@
### need to move the tweaks to the tweeks tab ### need to move the tweaks to the tweeks tab
#region Write-Log
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# 1) CREATE A GLOBAL LOG CACHE (NEW) # 1) CREATE A GLOBAL LOG CACHE (NEW)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# - Global log cache stores logs for session-wide accessibility.
# - Logs are stored in a [System.Collections.ArrayList] for easy JSON conversion.
# - Ensure log cache integrity during session restarts.
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 = New-Object System.Collections.ArrayList $Global:LogCache = New-Object System.Collections.ArrayList
} }
#region Write-LogHelper
# Check if the Write-Log function exists # ---------------------------------------------------------------------------
# 2) DEFINE THE Write-LogHelper FUNCTION
# ---------------------------------------------------------------------------
# - Write-LogHelper manages logging with customizable levels, task categories, and optional event logging.
# - Supported log levels: Info, Warning, Error, Success, General.
# - Task categories should match high-level operations (e.g., "On-boarding", "Off-boarding").
if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction SilentlyContinue)) { if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction SilentlyContinue)) {
# If the Write-Log function doesn't exist, create the Write-LogHelper function # If the Write-Log function doesn't exist, create the Write-LogHelper function
function Write-LogHelper { function Write-LogHelper {
@@ -37,7 +45,7 @@ if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction Silentl
[int]$CustomEventID # Optional custom Event ID [int]$CustomEventID # Optional custom Event ID
) )
# Simplified Event ID mapping # Simplified Event ID mapping for consistent logging
$EventID = switch ($Level) { $EventID = switch ($Level) {
"Info" { 1000 } "Info" { 1000 }
"Warning" { 2000 } "Warning" { 2000 }
@@ -55,7 +63,7 @@ if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction Silentl
"General" { ([char]0x1F4E6) } # Package icon "General" { ([char]0x1F4E6) } # Package icon
} }
# Map levels to colors # Map levels to colors for console output
$Color = switch ($Level) { $Color = switch ($Level) {
"Info" { "Cyan" } "Info" { "Cyan" }
"Warning" { "Yellow" } "Warning" { "Yellow" }
@@ -68,8 +76,9 @@ if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction Silentl
# Write-Host "$Icon [$Level] [$TaskCategory] $Message (Event ID: $EventID)" -ForegroundColor $Color # Write-Host "$Icon [$Level] [$TaskCategory] $Message (Event ID: $EventID)" -ForegroundColor $Color
# ------------------------------------------------------------------- # -------------------------------------------------------------------
# 2) ALSO STORE THE LOG IN OUR GLOBAL LOG CACHE (NEW) # 3) STORE LOGS IN GLOBAL CACHE
# ------------------------------------------------------------------- # -------------------------------------------------------------------
# - Cache format: Timestamp, Level, and Message for each log entry.
$logEntry = [PSCustomObject]@{ $logEntry = [PSCustomObject]@{
Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
Level = $Level Level = $Level
@@ -77,8 +86,11 @@ if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction Silentl
} }
[void]$Global:LogCache.Add($logEntry) [void]$Global:LogCache.Add($logEntry)
# ------------------------------------------------------------------- # -------------------------------------------------------------------
### TODO: Add support for exporting logs to a persistent file.
# Consider implementing a periodic export mechanism to save logs to a file.
# Example: Export-LogCacheToFile -Path "C:\Logs\TaskLogs.json"
# Optionally log to the Windows Event Log # Optional: Log to Windows Event Log for system-wide visibility.
if ($LogToEvent) { if ($LogToEvent) {
$EntryType = switch ($Level) { $EntryType = switch ($Level) {
"Info" { "Information" } "Info" { "Information" }
@@ -100,6 +112,8 @@ if (-not (Get-Command -Name Write-Log -CommandType Function -ErrorAction Silentl
} }
} }
### Hybrid Function:
# Wrapper for Write-LogHelper to simplify usage across modules.
function Write-LogHybrid { function Write-LogHybrid {
param ( param (
[string]$Message, [string]$Message,
@@ -138,10 +152,9 @@ else {
# Example usage of Write-LogHybrid # Example usage of Write-LogHybrid
Write-LogHybrid -Message "Starting SVS TaskGate" -Level "Info" -TaskCategory "SVSTaskGate" -LogToEvent:$true Write-LogHybrid -Message "Starting SVS TaskGate" -Level "Info" -TaskCategory "SVSTaskGate" -LogToEvent:$true
#endregion #endregion
#region SVS Module #region SVS Module
function Install-SVSMSP { function Install-SVSMSP {
param ( param (
# Cleanup flag # Cleanup flag
@@ -199,10 +212,7 @@ function Install-SVSMSP {
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $false)]
[string]$ApiSecretKey [string]$ApiSecretKey
) )
function Perform-Cleanup { function Perform-Cleanup {
Write-LogHybrid -Message "Cleanup mode enabled. Starting cleanup process..." -Level "Info" -LogToEvent Write-LogHybrid -Message "Cleanup mode enabled. Starting cleanup process..." -Level "Info" -LogToEvent