CMTrace LogFile style

There will come a time you’ll need detailed and extensive logging information.

I got this tip from a colleague scripter of mine, Eelco Labordus.

I haven’t done much with SCCM, so I wasn’t familiar with the CMTrace.exe tool
The pro is that it has color coding, so an error stands out (Default color of an error is red).

I found the original link to the script. It’s by Ephing Admin. Gotta give him his proper props!!! 😉

Ok it was a lively discussion as to the pro’s and cons’ of using this style. I’m always cautious of using third party tools. In this case I shouldn’t bother too much, still… I have had tools in the past that were abandoned and was left hanging to dry.

So this logging style if I understood correctly, will help you pinpoint where a script left off if it suddenly stopped executing. Ok the script failed, isn’t it safer to restart? Don’t get me wrong I’m all for proactive actions, but it should have added value. I’ve seen transcripts been used as logfiles. Goodluck trying to decipher that!!! And if you depend on ISE then your out of luck!

I mainly use verbose for verification. Mind you, the more you log the more bogged down you script becomes. Running a script in verbose mode will slow things down a bit (Not as much as Write-Host, I don’t know why it’s something I’ve noticed…)

When I don’t use verbose then Write-Progress is my next best friend. It’s always good practice to have a general idea what your script is up to…

So having an extra step along the way… Hmmm… Let’s roll with it!


#region: CMTraceLog Function formats logging in CMTrace style
function CMTraceLog {
    Param (





    $Time = Get-Date -Format "HH:mm:ss.ffffff"
    $Date = Get-Date -Format "MM-dd-yyyy"

    if ($ErrorMessage -ne "") {$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

#region: verify that the logFile exists
if(!(test-path "$pwd\log\$logFile")) {
    New-Item "$pwd\log\$logFile" -ItemType File

#region: Create hash CMTrace for splatting
$hshCMTrace = @{
    Message = ""
    Component = $MyInvocation.MyCommand.Name
    ErrorMessage = ""
    Type = 1
    LogFile = "$pwd\log\$logFile"

So I created a dsa.log for anything DSA related. Adding the scriptname to component lets me know which script was used to generate the Message, so far so good. This will come in handy gathering all scripts running dsa related stuff! 🙂

Oh and I created a hash table for splatting! Splatting is one of those techniques that you get used to real fast!

If i need to write a line to logfile updating the input is quite easy:

First I’ll update the key I need:
$hshCMTrace.Message = “Importing csv file $csvFile”

Next the message type:
$hshCMTrace.Type = 1

And then splat!!!
CMTraceLog @hshCMTrace

Notice that the $ becomes a @ when splatting. Splatting has a lot of value, one of which I’ll get into another time.

Well that my tip for for logging CMTrace style with a lil’ help from splat! The difficult part is formulating the right Message and Error message… Remember, added value…

Hope it’s worth something to you…




1 thought on “CMTrace LogFile style

  1. Pingback: Move and disable AD users past expiration date | pshirwin

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s