Generating cool RTF output with PowerShell and WITHOUT Word–Part 5!

Now we continue forward with leveraging the ability of having Windows PowerShell build out an RTF files without the need for Microsoft Word. 

Just think.  You can NOW generate reports on your server that can be read my Managers and Directors with NO pre-requisite for any software OTHER than Windows PowerShell.

Now one of the things I decided to play with was generating content that’s typically only available in Microsoft Word into a generic RTF file.  Something like a Table.

If you look at the menu in Wordpad / Write you will see NOWHERE an option to “insert table”

Just look.

image

But we DO know that if you were to generate a simple blank table in Microsoft Word, copy and paste it into Wordpad, it will render just fine as can be seen below with our simple 3×3 table.

image

Now we just open up this document to find what a SINGLE row looks like.  We’ll use Notepad again but look for the words ‘Patch and Switch’ to see what the formatting looks like for that row.

image

We will then immediately ‘Permanently Borrow’ that code for out PowerShell script Smile

image

Now since really this my first crack at doing this, I decided to create an additional function for the columns since they will have multiple values.  (You know as I’m typing this, I’m already thinking of a few ways to redo it too….)

The Write-RTF column function will be in a simliar format.   It will output a Single line each time but accept three or four values (Hint: I cheated and ‘Permanently Borrowed’ the code for Four column output as well)

So as before we’ll have a string which contains the same content as the RTF code from the text file, but we’ll swap out the words like ‘Patch and Switch’, ‘Technet’ and ‘Big Guy’ with actual Powershell variables.

Function Write-RTFColumn
{
param($Style,$value1,$value2,$value3,$value4)
       
     switch($Style)
    {  
        # Regular Line – 3 column output
        ‘0’ { $output="trowdtrgaph108trleft5trbrdrlbrdrsbrdrw10 trbrdrtbrdrsbrdrw10 trbrdrrbrdrsbrdrw10 trbrdrbbrdrsbrdrw10 trpaddl108trpaddr108trpaddfl3trpaddfr3
clbrdrlbrdrw10brdrsclbrdrtbrdrw10brdrsclbrdrrbrdrw10brdrsclbrdrbbrdrw10brdrs cellx3121clbrdrlbrdrw10brdrsclbrdrtbrdrw10brdrsclbrdrrbrdrw10brdrsclbrdrbbrdrw10brdrs cellx6238clbrdrlbrdrw10brdrsclbrdrtbrdrw10brdrsclbrdrrbrdrw10brdrsclbrdrbbrdrw10brdrs cellx9355
pardintblwidctlparf1fs22 "+$value1+"cell "+$value2+"cell "+$value3+"cellrow
pardsa200sl276slmult1f2lang9`r`n" }
    Write-RTFDoc $output

}

 

If you look you’ll see I’ve highlighted where I’ve put the values in with Bold and Underscore.   If you compare the original code from the RTF file here are the values that were there originally.

clbrdrlbrdrw10brdrsclbrdrtbrdrw10brdrsclbrdrrbrdrw10brdrsclbrdrbbrdrw10brdrs cellx3121clbrdrlbrdrw10brdrsclbrdrtbrdrw10brdrsclbrdrrbrdrw10brdrsclbrdrbbrdrw10brdrs cellx6238clbrdrlbrdrw10brdrsclbrdrtbrdrw10brdrsclbrdrrbrdrw10brdrsclbrdrbbrdrw10brdrs cellx9355
pardintblwidctlparf1lang1033 Technetf2lang9cellf1lang1033 Big Guyf2lang9cellf1lang1033 Patch and Switchf2lang9cellrow

 

If you’re like my sample script so you can play for yourself without cutting and scraping screen bits together I’ve uploaded it to the Technet Script Gallery for all to share.  

It’s a pretty simple idea and it’s DEFINITELY something that can be expanded upon (especially in the processes).  

Other ways it could be improved upon would be options to select fonts and Pitch, Bold, Italics or even allowing data to be piped to a function.

Let’s just this “V1”, download and play with it.  At the least you can use it now to get data from a server and build an RTF file directly with it and use “SEND-MailMessage” to email those lovely reports!

Enjoy ! If you have any questions you can ping me anytime.  This was a fun one to pull together.

Sean
The Energized Tech

Leave a Reply