Tag Archives: Scripting

Friday Fun: A PowerShell Macro

Today’s Friday Fun is a little different in that it showcases two things I use almost every day: Microsoft Word and PowerShell. I am writing new articles and material almost daily and of course very often the content is PowerShell related. Usually I use the blog post template in Word to make it easier to upload. This works great because I can insert links in the Word document and they will be maintained when copied to WordPress. One of the steps I’ve started taking in my writing is to include a link to online help for a cmdlet.

For example, if I am writing about Get-WinEvent I’ll include a link on the cmdlet name. Of course I’m not going to manually get the link, copy it and create the hyperlink in Word. So I created a Word macro that calls a PowerShell script to get the online link and insert it as a hyperlink. Here’s how this all works.

There are a few ways to get the online help link for a given cmdlet. You could retrieve the HelpUri with Get-Command.

Or you can retrieve it using Get-Help.

As you can see the links are different, even though the online content is very similar. But since the latter is what you would get if you ran Get-Help Get-Service –online, I decided to go with that. I put together a simple script.

The function writes the link to the pipeline if found, otherwise it writes $Null. I’ll explain why in a moment.

With this script, I now turn to Word and created this macro.

I couldn’t find a way to capture the output of the PowerShell command so I ended up creating a temporary file that contains the link. If nothing was found then the file will have a 0 length. I found this easier because the macro reads the file and saves the contents to a variable, link. If the length of link is > 0 then Word inserts the hyperlink, complete with a constructed screen tip.

I stored the macro in Normal.dot so I always have it available.

I even gave it a keyboard shortcut under File – Options –Customize Ribbon

Finding ways to automate the dreary tasks from my day is very rewarding, plus I almost always learn something new. Hopefully you did to.

PowerShell for Newbies

As you might imagine I get a lot of questions about how to get started with PowerShell, especially what books and material they can get their hands on. While I earn living creating books, training videos and offering live training, I’m aware that many people are on a budget so if you are looking for a free resource, allow me to direct you to an ebook/white paper I wrote for Veeam last year titled PowerShell for Newbies: Getting Started with PowerShell 4.0.

This is a 30 page crash course on everything I think you need to get started with PowerShell. There’s no way you can learn PowerShell in 30 pages, but this should get you started on the right path. I’d love for you to get a copy and let me know what you think. Head over here to grab your copy. You will need to register to download the paper. When you are finished come back here and take a look at my Books and Training page for your next steps.

Enjoy.

Friday Fun: Get PowerShell User Groups

The other day Don Jones tweeted about find a PowerShell user group. In case you didn’t know, just about every user group associated with PowerShell can be found online at http://powershellgroup.org. This is a terrific resource for finding a user group near you. Of course, Twitter being what it is someone joked about the lack of a Get-PSUserGroup cmdlet. So, taking the joke as a challenge I built on. Don also was going to build something but I haven’t seen what he came up with. I suspect it will be similar to mine.

Because we there is a web site, we can scrape it with Invoke-WebRequest.

Instead of trying to parse the document object model (DOM), the resulting object has a property called AllElements which is exactly what the name implies. I had to look at the source HTML on the page to identify the elements I needed to reference. Since it seemed I could get what I want via an HTML class, I grouped the elements by class and turned it into a hashtable.

I opted for a hashtable to make it easier to get all the elements for a given class. For example, I knew the ‘views-field views-field-title’ class would give me the name of each group.

Likewise I knew the ‘views-field views-field-description’ class would provide a description.

Knowing what each object looked like, you could also pipe to Get-Member to discover property names, made it easy to extract the relevant information.

Once I had the core concepts down, I built an advanced function around them called Get-PSUserGroup.

The command doesn’t take any parameters and simply returns high level information about each group.

The pipelined object includes a link to the group’s page which means you could try something like this:

All of the groups are sent to Out-GridView.

Select one or more groups and each link should open in your browser. Now you have no excuse for not finding a PowerShell User Group. And if here isn’t one near you, start it!

I have some other interesting things on this topic, but I’ll save those for another day. Enjoy!!