Skip to content

Instantly share code, notes, and snippets.

@ljtill
Last active August 1, 2022 13:21
Show Gist options
  • Select an option

  • Save ljtill/f259a8dfc0dee5c1a7a42e4b980b7440 to your computer and use it in GitHub Desktop.

Select an option

Save ljtill/f259a8dfc0dee5c1a7a42e4b980b7440 to your computer and use it in GitHub Desktop.
Provides the ability to write logs
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