Powershell – CMTrace Log Function

1 minute read

I’ve always been a huge fan of having detailed log files when running scripts at work. I originally wrote a log function to output date & time & message to a file, but it would be hard to read. I decided to write a function in CMTrace format so I could open these files with CMTrace and quickly see what I wanted to see (for instance, Errors show up in red!)

Function:

function Log {
    Param (
		[Parameter(Mandatory=$false)]
		$Message,
 
		[Parameter(Mandatory=$false)]
		$ErrorMessage,
 
		[Parameter(Mandatory=$false)]
		$Component,
 
		[Parameter(Mandatory=$false)]
		[int]$Type,
		
		[Parameter(Mandatory=$true)]
		$LogFile
	)
<#
Type: 1 = Normal, 2 = Warning (yellow), 3 = Error (red)
#>
	$Time = Get-Date -Format "HH:mm:ss.ffffff"
	$Date = Get-Date -Format "MM-dd-yyyy"
 
	if ($ErrorMessage -ne $null) {$Type = 3}
	if ($Component -eq $null) {$Component = " "}
	if ($Type -eq $null) {$Type = 1}
 
	$LogMessage = "<![LOG[$Message $ErrorMessage" + "]LOG]!><time=`"$Time`" date=`"$Date`" component=`"$Component`" context=`"`" type=`"$Type`" thread=`"`" file=`"`">"
	$LogMessage | Out-File -Append -Encoding UTF8 -FilePath $LogFile
}

Parameters are:

Message – Part of the log message ErrorMessage – Gets put in the log message. If not null, makes the line appear red in CMTrace. Component – CMTRace has a component column, this will appear in that column Type – Valid numbers are 1 (Normal), 2 (Warning – Yellow), 3 (Error – Red) LogFile – The file you want to output to

Usage:

$LogFile = "C:\TestFolder\TestLog.Log"
Log -LogFile $LogFile
Log -Message "This is a normal message" -ErrorMessage $Error -LogFile $LogFile
Log -Message "This is a warning" -Type 2 -Component "Test Component" -LogFile $LogFile
Log -Message "This is an Error!" -Type 3 -Component "Error Component" -LogFile $LogFile

Output in CMTrace:

Leave a Comment