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!

[CmdletBinding()]
Param(
    [string]$logFile="dsa.log"
)

#region: CMTraceLog Function formats logging in CMTrace style
function CMTraceLog {
    Param (
        [String]$Message,

        [String]$Component,

        [String]$ErrorMessage,

        [ValidateRange(1,3)]
        [Int]$Type,

        [Parameter(Mandatory=$true)]
        $LogFile
    )

    $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
}
#endregion

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

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

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…

Ttyl,

Urv

Advertisements

One 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:

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s