Tag Archives: Scripting

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.

Friday Fun – Does Anyone Really Know What Day It Is?

eternalclock_150x150 This week’s Friday Fun I think epitomizes how much I think about PowerShell. But I also think it will serve as a useful learning device if not something you might actually want to use. For some reason, I thought it would be useful to quickly display a monthly calendar in PowerShell. Sure, I could click over to the clock on the start bar and navigate to a calendar. But then I have to take my hands off the keyboard and click around with a mouse. I’d much rather type a quick command, maybe give it a date and see the results. So this is what I came up with: Get-Calendar.

The script will define an alias, gcal, for the function.

Because calendars are culture (i.e. language) specific, I wanted to write something that anyone could use, and not something just for North American users. That meant retrieving culture settings so I could use language specific versions of days of the week. I think. It is very hard to fully test this without a system running completely in a different culture.

I wanted my calendar to display abbreviated week day names in the correct culture.

After quite a bit of experimenting, I realized the best way to create the calendar was to write each line as a separate string. This meant one line would be the abbreviated week day names.

Then each week would be a string of numbers. But this was the tricky part. It was easy enough to find out how many days were in the month, and what the first day of the month would be.

Here’s the challenge: I can figure out that the first day of the month might be on a Thursday. Which means I need to align ‘1’ under the THU heading, and then add enough other days until I reach the end of the week.

So I created an array, $Week1, to hold a set of values. I use a FOR loop to check if the date’s short day name matches the array of abbreviated day names from the UI culture, I know that is the first day of the month. Otherwise, I simply add a 2 space “filler” to the array. Then I need to add the rest of the days for the first week.

With that, I can begin constructing a text representation of the calendar using a here string.

The clever part is using the -Join operator to essentially concatenate all of the values in the week 1 array, separating each by a space. From here, I repeat the process for each week until I reach the end of the month.

At the end of the process $mycal is a here string with the calendar. I could simply write $mycal to the pipeline and it would look just fine. However, because you might want to save the calendar to a text file, I found it best to split the here string back into an array.

Now you can create a text file calendar for next year:

Or run the command at the prompt.
get-calendar

The function defaults to the current month or you can specify any date. You can even write it to the host and add a little color.

get-calendar2

If you run PowerShell under something other than en-US, I’d love to hear how this works for you. Hope you have a little bit of fun with this.

More PowerShell Toolmaking Fun

blacksmith I am having so much fun making my own PowerShell tools that I just can’t stop. I’ve been using my Get-Commandmetadata function that I wrote about a few weeks ago. A driving force in all of this is to have a toolset that I can use efficiently from the console. Or to put it in terms you might appreciate: the biggest bang for the least amount of typing. Today, I have another example. I took a common PowerShell expression and simplified it into its own command. And even though the final code looks like a lot, it didn’t take me very long because my toolmaking tool “wrote” most of the script for me.

So the common task is finding services that match a certain state such as running or stopped. You’ve most likely seen this expression:

Not the most onerous command to type, but even I have mistyped and had to correct. If this is a common task, why not create an easy to use tool?

Yes, I could have written my own function that called Get-Service, but then I’d have to write all of the code to accommodate all of the parameters for Get-Service. With the proxy command, all I need to do is insert my own code.

In other words, I let Get-Service do its thing and then filter the result. Now I can easily do this:

Or, because I’m typing this, I could use my own alias.

One thing I want to point out, is that for the new Status property, you’ll notice I didn’t cast it as a string.

I could have made it a string, but by using the actual enumeration, PowerShell will autocomplete possible values.
get-myservice

How did I know what to use? I used Get-Member.

It is really not that difficult or time consuming. I spent more time writing this blog post than I spent creating the function.

I hope this will inspire you to create your own PowerShell tools and that you will share the fruits of your labors.