|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Logs messages with different severity levels to the console and optionally to a file. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | +The `Write-Message` function provides a unified way to log messages with levels such as Info, Error, Alert, Verbose, and Debug. |
| 7 | +It supports logging to the console with color-coded messages and optionally writing logs to a file with timestamps. |
| 8 | +
|
| 9 | +.PARAMETER Message |
| 10 | +The message to log. Supports pipeline input. |
| 11 | +
|
| 12 | +.PARAMETER Level |
| 13 | +Specifies the log level. Supported values are Info, Error, Alert, Verbose, and Debug. |
| 14 | +
|
| 15 | +.PARAMETER LogFile |
| 16 | +(Optional) Specifies a file path to write the log messages to. If not provided, messages are only written to the console. |
| 17 | +
|
| 18 | +.EXAMPLE |
| 19 | +Write-Message -Message "This is an info message." -Level Info |
| 20 | +
|
| 21 | +Logs an informational message to the console. |
| 22 | +
|
| 23 | +.EXAMPLE |
| 24 | +Write-Message -Message "Logging to file." -Level Info -LogFile "C:\Logs\MyLog.txt" |
| 25 | +
|
| 26 | +Logs an informational message to the console and writes it to a file. |
| 27 | +
|
| 28 | +.EXAMPLE |
| 29 | +"Pipeline message" | Write-Message -Level Alert |
| 30 | +
|
| 31 | +Logs a message from the pipeline with an Alert level. |
| 32 | +
|
| 33 | +.NOTES |
| 34 | +Author: Tiago Balabuch |
| 35 | +#> |
| 36 | + |
| 37 | +function Write-Message { |
| 38 | + [CmdletBinding()] |
| 39 | + param ( |
| 40 | + [Parameter(Mandatory, ValueFromPipeline)] |
| 41 | + [string]$Message, |
| 42 | + |
| 43 | + [Parameter()] |
| 44 | + [ValidateSet("Message","Info", "Error", "Warning","Critical", "Verbose", "Debug", IgnoreCase = $true)] |
| 45 | + [string]$Level = "Info", |
| 46 | + |
| 47 | + [Parameter()] |
| 48 | + [string]$LogFile |
| 49 | + ) |
| 50 | + |
| 51 | + process { |
| 52 | + try { |
| 53 | + # Format timestamp |
| 54 | + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" |
| 55 | + |
| 56 | + # Construct log message |
| 57 | + $logMessage = "[$timestamp] [$Level] $Message" |
| 58 | + |
| 59 | + # Write log message to console with colors |
| 60 | + switch ($Level) { |
| 61 | + "Message" { Write-Host $logMessage -ForegroundColor White } |
| 62 | + "Info" { Write-Host $logMessage -ForegroundColor Green } |
| 63 | + "Error" { Write-Host $logMessage -ForegroundColor Red } |
| 64 | + "Warning" { Write-Host $logMessage -ForegroundColor Yellow } |
| 65 | + "Critical" { Write-Host $logMessage -ForegroundColor Red } |
| 66 | + "Verbose" { Write-Verbose $logMessage } |
| 67 | + "Debug" { Write-Debug $logMessage } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + # Optionally write log message to a file |
| 72 | + if ($LogFile) { |
| 73 | + try { |
| 74 | + Add-Content -Path $LogFile -Value $logMessage -Encoding UTF8 |
| 75 | + } catch { |
| 76 | + # Catch and log any errors when writing to file |
| 77 | + Write-Host "[ERROR] Failed to write to log file '$LogFile': $_" -ForegroundColor Red |
| 78 | + } |
| 79 | + } |
| 80 | + } catch { |
| 81 | + Write-Host "[ERROR] An unexpected error occurred: $_" -ForegroundColor Red |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments