Tag Archives: PowerShell

The Altaro PowerShell Hyper-V Cookbook

powershell-cookbook-ebook1 For awhile now I’ve been contributing to the Altaro Hyper-V blog. Many of my articles centered on managing Hyper-V with PowerShell. I’ve gone through my library of management tools and written an eBook around them. All of the PowerShell scripts and functions have been polished up a bit. The eBook briefly discusses how to you use each recipe. It is a cookbook after all.

All of the recipes have been tested, but even so you should test everything in a non-production environment. Every Hyper-V environment has its own quirks. Or you can use the recipes as jumping-off points for your own PowerShell and Hyper-V. When you download the eBook be sure to get the accompanying zip file of code samples.

Visit http://www.altaro.com/hyper-v/new-ebook-altaro-powershell-hyper-v-cookbook/ to learn more and download your free copy. I hope you’ll let me know what you think

Friday Fun: Pick Up Milk

sticky-graphic Sometimes when working on a PowerShell problem, you might have to come to the conclusion that PowerShell is not the right tool for the job. There are some tasks or applications that simply don’t lend themselves to automation or PowerShell. This isn’t necessarily a limitation in PowerShell, nor would I say it is a deficiency in the application at hand. It is just the nature of IT.

That said, there may be situations where you still want to use PowerShell, even if the experience is less than optimal. So let’s have some fun with this today. For a number of versions of Windows client’s there has been a little utility called Sticky Notes.
new-stickynote

This is an electronic version of Post-It Notes. Wouldn’t it be nice to be able to use PowerShell to take advantage of the utility? Sadly, this application is written in such a way that nothing is exposed that can be automated, either through COM or .NET. So we’re left with a drastic measure of sending keys to the application.

As in the days of VBScript, sending keys is a risky proposition because you can’t always guarantee that the application in question will have focus. But let’s try. First, we need to start the program simply by invoking the command stikynot.

To send keys I’m going to use the .NET Framework. So to be on the safe side, I’m going to add some necessary assemblies just in case. It won’t matter if they are already loaded.

Now to activate the window. You can do this by the window title.

Or the process ID. I’ll use the title.

While the window has focus, I can send keys to it.

And that’s all there is to it! Sticky Notes have a number of keyboard shortcuts you can send using this technique. I found a page at http://www.door2windows.com/list-of-all-keyboard-shortcuts-for-sticky-notes-in-windows-7/ that listed many of them.

But you know I’m not going to stop here. I built a module called StickyNotes.

The module includes several functions for creating, adding, setting and removing sticky notes. The tricky part was getting the timing right. You might need to adjust some sleep values, or be prepared to re-run a command. I would not build any mission critical processes around anything that uses send keys techniques as it isn’t reliable. But it sure is fun!

You might even use it like this:

At the end of the command, a sticky note pops up letting you know the deed is done. You might see an error about a missing process, but you can ignore it. Or maybe you simply need a reminder to pickup that loaf of bread or your child on the way home.

Sometimes PowerShell isn’t the best tool for the job, but I think it can always be fun. Let me know what you think of my little project.

Have a great weekend.

Sorting Hash Tables

letterjumbleOver the weekend I received a nice comment from a reader who came across an old post of mine on turning an object into a hash table.  He wanted to add a comment but my blog closes comments after a period of time. But I thought it was worth sharing, especially for those of you still getting started with PowerShell. The comment was on how to sort a hash table.

Let’s say you have a simple hash table like this:

And is displayed like this:

To sort on the keys, you can use the GetEnumerator() method which is part of every hash table object. This method creates a System.Collections.DictionaryEntry object for each item in the hash table.

This means you can sort on any property.

By the way, starting in PowerShell 3.0, you could “pre-sort” the hash table by defining it as ‘ordered’.

Now the hash table will always be sorted in the order you defined the entries.

Although, if you want to sort you still can.

Have a great week and I hope you get things sorted out.