Tag Archives: PowerShell

Friday Fun: Read Me a Story

announcer A few days ago, someone on Twitter humorously lamented the fact that I expected them to actually read a blog post. After the laughter subsided I thought, well why does he have to? Perhaps I can make it easier for him. Plus I needed something fun for today. So I put together a PowerShell function I call Invoke-BlogReader which I think you’ll have fun playing with. It isn’t 100% perfect and as with most Friday Fun posts, serves more as an educational device than anything.

The function uses the .NET System.Speech class which seems to be a bit easier to use than legacy COM alternative. Here’s a snippet you can test.

So basically, all I have to do is get the contents of a blog article and pass the text to the voice object. That is a bit easier said than done which you’ll see as you look through this code.

The function uses the Invoke-WebRequest cmdlet to retrieve the content from the specified web page and I then parse the HTML looking for the content.

This is the trickiest part because different blogs and sites use different tags and classes. There is a getElementsbyClassName method, but that seems to be hit and miss for me, so I’ve opted for the slower but consistent process of using Where-Object.

Once I have the content, I could simply pass the text, but I realized I may not want to listen to the entire post. Especially for my own which often have script samples. Those aren’t very pleasant to listen to. So I realized I needed to parse the content into sentences. Regular expressions to the rescue.

Don’t ask me to explain the regex pattern. I “found” it and it works. That’s all that matters. $Sentences is an array of regex match objects. All I need to do is pass the value from each match to the voice object. The other benefit is that I can include a parameter to also display the text as it is being read.

That’s all there is to it! If you want to try out all of the options here’s a sample command:

Where this can get really fun is using another function, which I’m not sure I ever posted here, to get items from an RSS feed.

Putting it all together you can get the feed, pipe it to Out-Gridview, select one and have the blog read to you!

So now you can have your blog and listen to! There are probably a number of ways this could be enhanced. Or perhaps you want to take some of these concepts and techniques in another direction. If so, I hope you’ll let me know where you end up. Have a great weekend.

Tracking Your Day with PowerShell

timer Not too long ago, I received an email with a snippet of PowerShell code and a request for assistance. The code snippet used a little .NET code to retrieve the process for the currently active window. The goal was to have a PowerShell script run, keeping track of how long a given window was active. At the end of the day you would have a report of how you spent your day, at least based on active Window titles. As you might expect, faster than you can say “shiny ball”, I was all over this.

The code to get the active window was the easiest part. But I found I needed to take into account situations where the active process was for something non-interactive. I also found you get some odd artifacts when using Alt-Tab to cycle between open applications.

Once I had a collection of objects representing the different active windows, I realized it would be helpful to have a set of tools for measuring and analyzing the data so I wrote a function for that. The end result is a PowerShell module I call MyMonitor.

The module includes a custom format file and the .psm1 file adds some custom type definitions. The main command is Get-WindowTime. This command will monitor your windows for either a specified number of minutes, until a specific datetime, or if a specific process is detected. For tracking over longer periods of time use the -AsJob parameter.

You should end up with data that looks like this:

You can then measure the data.

Or see how much time you spent on Facebook.

This is from a small sample.

My module also includes an about topic, which I’ll post here

I hope you’ll try it out and let me know what you think. Be sure to read help and examples for all of the commands.

Download the MyMonitor zip file and extract to your modules folder.

Update: this will not track Windows 8 apps like Weather or Foo & Drink.

Friday Fun Text to HTML

htmlI love being able 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.


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.