Last active
August 1, 2022 13:21
-
-
Save ljtill/f259a8dfc0dee5c1a7a42e4b980b7440 to your computer and use it in GitHub Desktop.
Provides the ability to write logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Write-Log { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory = $true)] | |
| [ValidateSet("Information", "Warning", "Error", "Verbose", "Debug")] | |
| [string]$Level, | |
| [Parameter(Mandatory = $false)] | |
| [string]$Topic, | |
| [Parameter(Mandatory = $true)] | |
| [string]$Message, | |
| [Parameter(Mandatory = $false)] | |
| [string]$Timestamp | |
| ) | |
| begin { | |
| if ($Timestamp) { | |
| $timestamp = Get-Date -Format "[MM/dd/yyyy - HH:mm]" | |
| $log = ($timestamp + " " + "($topic)" + " " + $message) | |
| } | |
| else { | |
| $timestamp = "" | |
| $log = ("($topic)" + " " + $message) | |
| } | |
| } | |
| process { | |
| switch ($level) { | |
| "Information" { | |
| Write-Information -MessageData $log -InformationAction Continue | |
| } | |
| "Warning" { | |
| Write-Warning -Message $log | |
| } | |
| "Error" { | |
| Write-Error -Message $log | |
| } | |
| "Verbose" { | |
| Write-Verbose -Message $log | |
| } | |
| "Debug" { | |
| Write-Debug -Message $log | |
| } | |
| } | |
| } | |
| end { } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment