Writing CMTrace format Log files with Powershell

Anyone who has administered a little SCCM in their time, would be be familiar with the tool CMTrace or SMSTrace. CMTrace is a tool that comes with System Center Configuration Manager. It allows you to view the myriad of logs files that SCCM generates in a consistent and easy to read format. Being used to the log format that CMTrace generates, I thought it would be a great idea to use that format for logging Powershell script actions and errors.

I hunted around the web and found a few versions of a log function using CMTRace. The best implementation that I found was from Russ Slaten at the System Center Blog site. It works really well but I wanted to add some different functionality.

CMTrace.exe is part of the System Center 2012 R2 Configuration Manager Toolkit, which I believe anyone can download. If you have System Center Configuration Manager installed in your environment, you can also grab it from the \\”SiteServer”\Program Files\Microsoft Configuration Manager\tools\cmtrace.exe.

There are a few different use cases for when logging is handy: -

  • Running Powershell scripts as Scheduled Tasks.
  • Auditing automation centrally in a team environment.
  • The output of a log could be used as part of change management processes.
  • Debugging locally and remotely

I’ll start off by posting the script. Below I’ll go into some information that might help in using the script.

**NOTE: This has only been tested on Powershell v5.0. The information stream is a new feature of Powershell v5.0.**

Here is the script: -

I’ve included a help file inline to help with calling the function.

Where does the log file get created?

The function by default will log to $Env:temp\powershell-cmtrace.log. You can however pass a file location to the function if you would like to the log file in a different location.

What does the output look like on the host?

Here is an example.

Why does Error only log one line back to the host, where is all the ErrorRecord informaiton?

I wanted to write back to the host in a consistent fashion. The error record is still there I’m just not passing it back to the host. The full error record gets put in the log file and you can view that information by clicking on the Error entry and looking at the window down the bottom of CMTrace. Also, you can still access the error information in the host by using the $Error variable. If it were the last error you would access it by using the first index of the $Error variable. eg. $Error[0].

What does the output to the log file look like?

It looks like this. Notice the Error information is in the box down the bottom.

I don’t want to see the output on the host, I just want it to log in the CMTrace format.

There is a parameter switch called WriteBackToHost. By default it’s set to True, but if you don’t want to see the output then set this to false.

How do I call this function?

To call this function, either dot source it, run it from ISE and then call it, put it in your profile or pop it in a module and Bob is your uncle.

Gimme some examples of how to use the Write-CMTracelog function.

Here are some examples of how you would use this advanced function that are in the help section of the function.

Examples:

The below example shows how to output a verbose message.

This example shows how to use the Preference variables with the Write-CMTracelog function. It should obey any preference variables that are set in the same scope from where the function has been called.

This example shows how to use the function with a terminating error by using the $Error variable in the message parameter.

 

Hope this function helps you out. Feel free to use and modify to suit your needs.

Sources:

http://blogs.msdn.com/b/rslaten/archive/2014/07/28/logging-in-cmtrace-format-from-powershell.aspx
https://www.microsoft.com/en-us/download/details.aspx?id=50012
http://blogs.technet.com/b/heyscriptingguy/archive/2015/07/04/weekend-scripter-welcome-to-the-powershell-information-stream.aspx

Connecting Word 2016 to WordPress - Step by Step Guide

Connecting Word 2016 to a WordPress Blog is suprisingly easy. It allows you to create your blogpost in Word, insert your pictures etc and then press the publish button and your post will published with all of your images uploaded to your blog site. It's a pretty handy feature if you prefer to use Word for blogging as opposed to something like Windows Live Writer or the updated open sourced version called Open Live Writer.

Below is a step by step guide of how I connected Word 2016 to my WordPress Blog.


Start by opening Word 2016, and clicking on the Blog Post template.

You will then be greeted with the Register a Blog Account wizard. Select Register Now.

Select WordPress and then click Next.

Populate the Blog Post URL with your blog URL. Make sure to leave the xmlrpc.php appended to the end of your URL. By default XML-RPC functionality is turned on by default since WordPress 3.5. In earlier version the user needed to turn it on in the blog settings.

Fill out your Username and Password and then select Picture Options.

Select the Picture Provider that is relevant for your site. For my site I'll pick My Blog Provider.

Click Yes. (Maybe not such a good idea if you are on public wifi, because if someone is sniffing packets they will get they username and password you are using to authenticate. Here is a link to an article about using Https and SSL To connect to WordPress)

 

Click OK.

Now that it's all connected. Submit a test post to check if everything is working OK.
Note: You can even set the category of the post by selecting Insert Category. A drop down list of all the categories you have specified on your blog will be shown.

Once you would like to send the post to your blog select Publish as Draft. Otherwise, your article will be posted.

If everything goes smoothly, you should get a confirmation that the post was published.

Log into your blog and you should see the post you have created.

That's all there is to it. Hope this helps.