Using Powershell to create a Rich Text Format document–Part 2

Unlike in the movie “Annie” tomorrow turned out to be more than a day away, in my case a few months.  Yes work has been busy but this has been a fun side project for me.  So now back to the fun! 

Last time we took a quick look into making an RTF or “Rich Text Format” file from Powershell.  We discovered that all we need to get started is a header and some basic information for the start of the file as well as a Terminator at the end of all the text

So now the “challenge part”, making this into a useful script that could actual like a tiny Word Mail Merge.  

So in Windows Powershell we all know that if you place a variable within a quoted string, it will expand internally.

$Name=”Ernie”
$SillyString=”Hello $Ernie”
WRITE-HOST $SillyString

This will produce the following output

Hello Ernie

Knowing this feature is something we can leverage within Windows Powershell and create a simple Mail Merge type script.   In our case, I would actually like to produce an RTF document where I can substitute the needed data and produce the new document.   Just where am I going with this?  WATCH Smile

First let’s get the script started.  A simple script prompting for a filename and we’ll supply some static variables to drop into the script.

param (
[string]$Filename,
)

# Static Variables

$Firstname=”John”
$Lastname=”Smith”
$Accountname="CONTOSO\jsmith"
$Password="LousyPass123"

Now like before we’re going to define our header content for the RTF file.  I’ve made some additions.  You’ll note that each line ends with

`r`n

There are certain parts of an RTF that MUST end with a “Carriage Return / Line Feed” combination.   If you remember what a typewriter was and … (Oh oh! Just dated myself!)

Suffice it to say without embarrassing myself further that certain lines in the header MUST (and I mean MUST MUST MUST) end with a Carriage Return / LineFeed.   Also each line within your message must terminate with this sequence.

You’ll also note that rather than adding individual lines, I’m building a single string of content for the ADD-CONTENT Cmdlet

# Our RTF Header using a Consolas font

$Header+="{rtf1ansiansicpg1252deff0nouicompatdeflang1033{fonttbl{f0fnilfcharset0 Consolas;}}`r`n"
$Header+="{*generator Riched20 6.2.8102}viewkind4uc1 `r`n"
$Header+="pardsl276slmult1f0fs22lang9 par`r`n"

As before, your paragraph for an RTF must end with “par” and again each new line must also terminate with `r`n (Carriage Return / Linefeed) in the string.

$Message+="Hello $Firstname $Lastname and Welcome to ABCpar`r`n"
$Message+="Corporation.par`r`n"
$Message+="par`r`n"
$Message+="Your User ID is $Accountnamepar`r`n"
$Message+="Your Temporary Password is $Passwordpar`r`n"
$Message+="par`r`n"
$Message+="Do not share this information and remember,par`r`n"
$Message+="We are watching….par`r`n"
$Message+="`r`n"

And of course we add the footer

# And finally our footer to the RTF file

$Footer="}`r`n"

We need now add the variables together into a single string.

# Build the content together

$Content=$Header+$Message+$Footer

Now we’ll take this and place it into a single file, the name was provided by the $Filename parameter

ADD-CONTENT -path $Filename -value $Content –force

When you’re done you’ll end up with this simple script

param (
[string]$Filename,
)

# Static Variables

$Firstname=”John”
$Lastname=”Smith”
$Accountname="CONTOSO\jsmith"
$Password="LousyPass123"

# Our RTF Header using a Consolas font

$Header+="{rtf1ansiansicpg1252deff0nouicompatdeflang1033{fonttbl{f0fnilfcharset0 Consolas;}}`r`n"
$Header+="{*generator Riched20 6.2.8102}viewkind4uc1 `r`n"
$Header+="pardsl276slmult1f0fs22lang9 par`r`n"

# the Content of the message

$Message+="Hello $Firstname $Lastname and Welcome to ABCpar`r`n"
$Message+="Corporation.par`r`n"
$Message+="par`r`n"
$Message+="Your User ID is $Accountnamepar`r`n"
$Message+="Your Temporary Password is $Passwordpar`r`n"
$Message+="par`r`n"
$Message+="Do not share this information and remember,par`r`n"
$Message+="We are watching….par`r`n"
$Message+="`r`n"

$Footer="}`r`n"

# Build the content together

$Content=$Header+$Message+$Footer

ADD-CONTENT -path $Filename -value $Content –force

Run this script with a parameter of test123.rtf for the filename.

./NEWRTFFILE.PS1 –filename test123.rtf

Here is the exact document it will produce.

Hello John Smith and Welcome to ABC
Corporation.

Your User ID is CONTOSOjsmith
Your Temporary Password is LousyPass123

Do not share this information and remember,
We are watching….

Can you picture using this in your organization now?  As you create new staff within Windows Powershell, a lovely letter to hand off to the manager.  Or perhaps you have a system that needs to generate reports?  Within Windows Powershell you can leverage a well recognized format like RTF to produce very GUI looking documents from with the native Windows Powershell environment.

You’ll also notice one other thing I did.  Take a look at the variable $Accountname

$Accountname=”CONTOSO\jsmith”

Did you see the double backslash?  That’s intentional.  It is a part of the specifications for an RTF document.   Much like there are certain characters that require an “Escape” to be seen in Windows Powershell, certain characters like the backslash are important to the RTF interpreter and must be preceded by a backslash to be seen in the actual document.

Play with this and see what you can do with it.   I know what I’m doing at work tomorrow!

The Power of Shell is in YOU

Sean
The Energized Tech

Leave a Reply