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