Friday Fun: A PowerShell Nap

antique-watch-150x225I’m hoping that I’m not the only one who feels their butt dragging by mid to late afternoon. Let’s say that’s because we’ve been thundering through the day and by 3:00 we’re a bit out of gas. Yeah, I’ll go with that.

I find myself wanting to close my eyes for only a few minutes to recharge and or at least take the tired edge off.  In other words, I just want a quick nap like we had in kindergarten.  But I need to make sure I wake up! Since I always have a PowerShell console, I can use it as a quick and dirty alarm clock.

For starters I can use the Start-Sleep cmdlet to wait for X number of seconds. So if I want say a 10 minute timer I can run:

But if my eyes are closed how will know when time is up? A quick solution is to make my computer beep using the [console] .NET class.

So to run this all at once I can enter a command like this:

The semi-colon is the end of command marker so what I’m really doing here is executing 2 commands. PowerShell will sleep for 600 seconds and when that command completes, then the Beep() method will run.

But of course it is Friday so let’s have a bit more fun with this.

The Beep() method can also take 2 parameters. The first is a tone frequency, and the second is a duration in milliseconds.

With this in mind I put together a PowerShell napping script.

The script takes a parameter for the number of minutes you need to nap. It then writes a message to anyone who might be walking by your desk to keep quiet.  The script then does a countdown of sorts by calculating a timespan between the target end time and the current time. I’m writing the value as a string so that I can strip off the milliseconds.

nap in progress
nap in progress

At the end of the countdown I’ve recreated a well known chime, at least for those of you in the United States and of a certain age. I’ll let you try it out for yourself.

nap complete
nap complete

So get a bit more work done today,  and when you’re ready, take a quick PowerShell Power nap.

Enjoy and sweet dreams.

Friday Fun: Number Crunching

Earlier this week I was looking at the GoFundMe website in the midst of debating a new project. One of the considerations I have for sites like this is the expense involved. Certainly I don’t expect this type of service to be free. But I started wondering about what net donations might look like. According to GoFundMe, assuming I am interpreting this correctly, the site takes 5% of the donation right off the top. In addition to that there is a processing fee of 2.9% of the donation plus 30 cents. At least for the US. So for a $10 donation the net donation would be $8.91.

10 – (10*.05) – (10*.029 +.30)

Alright then.  This should be simple enough to turn into a PowerShell function.

This also should make for a fun learning opportunity.

The function takes a parameter for the donation amount. I cast it as a [double] in the event someone might donate $10.25. If used [int] PowerShell would turn it into $10.  I’ll come back to the other parameter in a bit.

I define the rates from GoFundMe as variables.  This makes it easier to calculate some values.

You’ll note that I’m using the Round() method of the .NET Math class. This is so that the result is formatted to 2 decimal points.  This is what I want to see, not necessarily how the site operates. With these values I can create a custom object.

Here’s a simple demonstration, and yes I know I’m not using a standard function name but this is for fun.

image

I also wrote the function as an advanced function so that I can pipe values into it. If you look at the parameter definition for $Donation you’ll see that I have a setting for ValueFromPipeline.

image

This makes it pretty clear about what happens with each donation.  But what did I end up with?  My function writes objects to the pipeline so that I can use other cmdlets, like Measure-Object.

image

Here’s the fun part: I can measure multiple properties.

image

I thought that was pretty handy.

I know that the values are in dollars.  But let’s say I was creating a report and I wanted to make it pretty and include the $ sign, or whatever my currency symbol might be.

If you recall, I included a switch parameter called UseCurrency along with a parameter alias of currency.

If I run the function with –UseCurrency, then this parameter will have a value of $True. That is how a Switch parameter works. In my function I can test the parameter value and it it is true, then I’ll create the same custom object, except that I will use the –F operator to format the value as a string using the currency symbol.

This operator is used to format strings in the .NET Framework. You can read more about this online at http://go.microsoft.com/fwlink/?LinkID=166450. But in short, the {0} on the left side of the operator is a numbered place holder. The c is the modifier which in this case indicates to use a currency format string and I’m limiting it to 2 places.  I probably don’t need that since I’m already rounding but I left it in for the sake of education.  On the right side of the –f operator is a comma separated list of values that will “plug in” to the place holders. The net result is this:

image

That looks pretty, but be aware that these values are strings.

image

This means my previous method of getting a sum will fail.  Instead I need to use numbers and then format the result.

image

Now I have the best of everything. Although if I truly want to make a pretty report, I can use Format-Table so that I can specify an alignment on the custom property.

image

I hope you found this fun and informative.  If you have any questions about what I did or why, please drop them in the comments.

Testing PowerShell HashTables

So I’ve been watching the PowerShell Toolmaking Fundamentals course on Pluralsight authored by Adam Bertram.  You may be surprised that I watch other PowerShell related courses, but I always pick up something I didn’t know about, find a new teaching technique or something else that makes me say, “that was cool.” I have found a few of these in Adam’s course so far.

One of the tricks he demonstrated was using a hashtable as a parameter value for an advanced function that could then be splatted to Set-ADuser.  For the sake of what I want to demonstrate here’s my simplified version of such a function.

The function gets the specified user and then updates the user with hashtable of parameters from Set-ADUser.  If you know all the parameter names this works just fine.

image

I’ve easily updated the user account.

image

But what if I make a mistake with the hashtable of settings?

image

There is no parameter called FirstName for Set-ADUser. It should be GivenName. One thing you could do, and this is the point of this article,  is to  validate the hashtable keys against parameter names from Set-ADuser.

You can use Get-Command to list all of the parameter names.

image

What we need to do is make sure that all of the keys in the settings hashtable are in this list. Here’s a quick test.

image

I can use code like this to test if the keys from $s are also in $p:

image

Although I may be more interested in the cases where they don’t match.

This won’t give any results because nothing matches the filter. But if I modify the hashtable with a bogus entry it will.

image

With this concept in mind I can revise the function.

image

Certainly you could add other code to list the available parameters, suggest corrections or whatever. But now the function won’t attempt to run and gracefully handles bad keys.

Once corrected, the function works as expected.

image

And please don’t take any of this as an indication that Adam missed something in his course. Far from it.  No course can teach you absolutely everything you need to know to build effective PowerShell tools. You need to build what works for you and add error handling that you feel is appropriate. In this case I thought this would offer a nice learning opportunity for you to learn about hashtable keys and a few operators.

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