Tag Archives: Scripting

Friday Fun Text to HTML

htmlI love being to create HTML documents from PowerShell commands. The Convertto-HTML cmdlet will happily turn any object into an HTML table using whatever properties you specify. Plus you can do all sorts of fancy things such as embedding a style sheet in the header, creating HTML fragments and inserting additional HTML elements. Today’s Friday Fun takes ConvertTo-HTML a bit further. As always what I have isn’t necessarily a production-worthy script but hopefully it will serve as a learning tool.

My idea was to take a text file such as a script and turn it into an HTML document using alternate shaded rows for the style. I already had a style header from some other scripts so that part was easy. My first attempt was to simply get the content of the file and convert to HTML.

But there is a problem which you’ll see if you try this at home. ConvertTo-HTML is looking for a set of object properties, and the the contents of a file don’t have any. Or to put it another way, I need to turn the contents of the file into an object. Since I knew I wanted to include the line number, I figured I could use the [pscustomobject] type that was introduced in PowerShell 3.0.

When run, I each line is turned into a custom object with a property for the Line number and a property for the value of each line. Normally, you would want to give your property a name, but in this case I don’t want a property name so I’m using a blank. You’ll see why eventually. Now I can pipe this to ConvertTo-HTML and get the output I’m expecting. That’s the crux of the script. Here’s the complete function, ConvertTo-HTMLListing

This function takes a file name and turns the text contents into an HTML document.

convertto-htmllisting

The function includes an embedded style sheet which is what allows me to use different fonts for the different sections as well as the alternating rows. The only other “trick” is that I’m replacing spaces in the original text with the HTML equivalent.

This is why it is helpful for PowerShell cmdlets to only do one thing. If ConvertTo-HTML automatically wrote to a file I’d have to jump through more hoops. But it doesn’t. This function as well only writes the HTML to the pipeline. You will still need to pipe it to Out-File to save. There are some examples in the comment-based help.

You’ll notice in the output that the table has a header for the line number and nothing for the line of text. That’s why I used a space for the property name in my pscustomobject.

The only other features I have are the options to skip blank lines and line numbers.

Now before you start saying, “Yeah but what about…”, I know there are other techniques and ISE add-ons to turn scripts into complete and colorized HTML files. You can also simply open the file in a browser as well and have it displayed as text. My function is really aimed as a teaching tool, although it could be kind of cool to have an HTML based library of your PowerShell scripts.

So was this fun? Did you learn something new? If not, there’s always next week. Enjoy your weekend.

A Timely PowerShell Prompt

021913_2047_WordTest1.pngDuring the course of writing a few scripts that refresh a specific part of the console, such as the recent Read-Host alternative, I realized that flashing colors wasn’t always necessary. The fact that I could update the same space on the screen meant I could write the same content with minor changes and it would look like the the screen as “flipping”. Essentially I was thinking of a clock.

So I thought it might be handy to have a clock as part of my PowerShell prompt. PowerShell has a built-in function called Prompt but you can replace it with your own version. The function will only last for as long as your PowerShell session so if you don’t like it, exit and restart PowerShell.

This is for the most part the basic function that shows PS and your current location. This prompt function will not work properly in the PowerShell ISE. The magic happens by always setting the cursor to the same coordinates in the PowerShell shell console. I use the same type of While loop I used in my other functions, only this time I’m waiting for the user to press any key, which would indicate the start of typing a command. Once that has been detected, the looping stops and the time ceases to be refreshed in the prompt.

You really need to see this live but here’s a screenshot example.
time-prompt-1

Then I thought it might be helpful to have the clock stand out so I added a little color.

time-prompt-2

The only issue I’ve found with these prompts, is that if you need to scroll in the console window, you’ll need to press the spacebar or type something so that the clock stops refreshing. Otherwise you are scrolling while PowerShell is trying to write to the console.

Instead of clock you could use a countdown timer. Or perhaps some sort of performance counter. For a prompt though, you need to make sure you can get and display the information in a few hundred milliseconds, otherwise the prompt will feel sluggish and unresponsive.

Enjoy and let me know where this leads you.

More Flashing Fun

talkbubbleI received a lot of interest in my Invoke-Flasher script. One comment I received on Twitter was for a way to use it interactively in a script. In essence, he wanted a flashing Read-Host so I took my original concept and tweaked it until I came up with a Read-Host alternative I simply call Read-Host2. This function will only work in the PowerShell console, NOT the PowerShell ISE.

The main tweak I made was to collect all the typed keys until Enter is pressed. I have a Switch construct to also eliminate the Shift key. The assumption is that you are writing text so this should be the only non alphanumeric key you would use. The message prompt will keep flashing until you start typing. I also emulated echoing text to the screen, including password masking if you use AsSecureString. The last change is a new parameter to allow you to flash the foreground color instead of the background color. There are several examples in the comment-based help.

Here are some screen shots.
Read-Host2-01

Read-Host2-02

I hope you’ll let me know what you think.