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.

Friday Fun – A Popup Alternative

windowpane-thumbnail In the past I’ve written and presented about different ways to add graphical elements to your PowerShell scripts that don’t rely on Windows Forms or WPF. There’s nothing wrong with those techniques, but they certainly require some expertise and depending on your situation may be overkill. So let’s have some fun today and see how you can display a popup message in a graphical form, using something you already know how to use.

It may not be sexy, but how about using Notepad. Here’s how. First, you need some content to display. Using a here string is good approach as it makes it easy to create a multi-line block of text.

With a here string you could also insert variables.

Next, create a temporary file with the text.

To display the message all you need to do is run Notepad.

This will return your PowerShell prompt but you want to make the popup more integral to your script, in which case I’d suggest using Start-Process.

The major advantage is that when you close Notepad your script continues so you can then delete the temp file. While we’re mentioning the temp file, if you tried my demo you’ll notice that the file name is displayed in the title bar. There’s no requirement that your file have an extension.

Notice my use of Join-Path to create a file name. I suggest you use this cmdlet instead of trying to concatenate pieces together. But now I get something a little nicer.

notepad-popup

With Notepad you could save the file elsewhere or even print it. I even created a re-usable function for you.

This function will automatically delete the temp file for you. If you have another text file viewer that you can launch from the command line you could use that instead of Notepad. As I mentioned at the beginning this is hardly exciting PowerShell but I hope you picked up something new and useful. Have a great weekend.

Advice, solutions, tips and more for the lonely Windows administrator with too much to do and not enough time.

%d bloggers like this: