Skip to content

Instantly share code, notes, and snippets.

@dancing-groot
Last active April 28, 2023 17:31
Show Gist options
  • Save dancing-groot/acd2faf51d6dacebf5af2005294cf1f7 to your computer and use it in GitHub Desktop.
Save dancing-groot/acd2faf51d6dacebf5af2005294cf1f7 to your computer and use it in GitHub Desktop.
Function for writing to a log file, either in plain or CMTrace format, with the option to write the information to the host as well (for debugging purposes). Based on https://www.wolffhaven45.com/powershell/write-cmtracelog-dropping-logs-like-a-boss/ and https://janikvonrotz.ch/2017/10/26/powershell-logging-in-cmtrace-format/
[CmdletBinding()]
param()
#region FUNCTIONS
function Write-Log
{
<#
.SYNOPSIS
Write information to a log file, optionally in CMTrace format
.LINK
https://gist.github.com/dancing-groot/acd2faf51d6dacebf5af2005294cf1f7
.NOTES
Version: 2022.12.09
Author: @dancing-groot
#>
[cmdletbinding()]
param (
[parameter(Mandatory = $true)]
[string]$Path,
[parameter(Mandatory = $true)]
[string]$Message,
[parameter(Mandatory = $true)]
[string]$Component,
[ValidateSet('Warning', 'Error', 'Verbose', 'Debug', 'Information', 'Info')]
[string]$Type = "Info",
[switch]$CMTrace,
[bool]$WriteBackToHost = $false
)
if ($Type -eq "Information") { $Type = "Info" }
if ($CMTrace)
{
switch ($Type)
{
"Info" { $severity = 1; break }
"Warning" { $severity = 2; break }
"Error" { $severity = 3; break }
"Verbose" { $severity = 4; break }
"Debug" { $severity = 5; break }
}
# Create a log entry
$content = "<![LOG[$($Type.ToUpper()): $Message]LOG]!>" + `
"<time=`"$(Get-Date -Format "HH:mm:ss.ffffff")`" " + `
"date=`"$(Get-Date -Format "M-d-yyyy")`" " + `
"component=`"$Component`" " + `
"context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " + `
"type=`"$severity`" " + `
"thread=`"$($PID)`" " + `
"file=`"$((Get-PSCallStack)[1].Command)`">"
}
else
{
$content = "$(Get-Date -UFormat '%Y.%m.%d %T')" + "`t" + `
$Component + "`t" + `
$Type + "`t" + `
$Message
}
# Write the line to the log file
Add-Content -Path $Path -Value $content
if ($WriteBackToHost)
{
switch ($Type)
{
"Info" { Write-Information "$Message" -InformationAction Continue; break }
"Warning" { Write-Warning $Message -WarningAction Continue; break }
"Error" { Write-Host "ERROR: $Message" -ForegroundColor Red; break }
"Verbose" { Write-Verbose $Message; break }
"Debug" { Write-Debug $Message; break }
}
}
} # Write-Log
#endregion FUNCTIONS
#region DECLARATION
$LogComponent = "Setup" # Used for grouping entries for what you are doing, especially important if you are doing multiple tasks in this script or if multiple scripts write to the same log file
$LogName = ($MyInvocation.MyCommand.Name).TrimEnd(".ps1")
$LogFile = "$PSScriptRoot\$LogName.log"
$LogParams = @{Path = $LogFile; Component = $LogComponent; WriteBackToHost = $true } # Messages will be written to the log and to the prompt
$LogFileCMTrace = "$PSScriptRoot\$LogName-CMTRACE.log"
$LogParamsCMTrace = @{Path = $LogFileCMTrace; Component = $LogComponent; CMTrace = $true; WriteBackToHost = $true } # Messages will be written to the log and to the prompt
#endregion DECLARATION
#region TESTING
Write-Log @LogParams -Message "This is an Information message"
Write-Log @LogParams -Message "This is a Debug message" -Type Debug
Write-Log @LogParams -Message "This is a Verbose message" -Type Verbose
Write-Log @LogParams -Message "This is a Warning message" -Type Warning
Write-Log @LogParams -Message "This is an Error message" -Type Error
Write-Log @LogParamsCMTrace -Message "This Information message is written to a log file in CMTrace format"
Write-Log @LogParamsCMTrace -Message "This Debug message is written to a log file in CMTrace format" -Type Debug
Write-Log @LogParamsCMTrace -Message "This Verbose message is written to a log file in CMTrace format" -Type Verbose
Write-Log @LogParamsCMTrace -Message "This Warning message is written to a log file in CMTrace format" -Type Warning
Write-Log @LogParamsCMTrace -Message "This Error is written to a log file in CMTrace format" -Type Error
#region TESTING
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment