Friday Fun – Take a Chance with PowerShell

Last week I showed you my PowerShell Bingo game. While the game itself might be a fun way to pass the time, the real goal was to teach you some PowerShell techniques and concepts without you realizing it. This week, I thought I’d keep with the gaming theme and take up chance. Specifically let’s roll some dice and flip some coins. These aren’t especially complicated tasks in PowerShell, and it is hard to say what problem they might solve, but let’s have some fun anyway.

First up, flipping coins.

Flipping a coin is essentially a True/False game. Or another way to look at is Even/Odd. My favorite way is to use the modulo operator.

Those values can be represented as Booleans.

So all I need is a random number and perform a modulo operation. Here’s the function I created.

I used a valid verb but will also define a more user-friendly alias.

Because you may want several different types of output, not necessarily the best idea for a function but this might be an exception. And remember this is supposed to be educational not necessarily practical. My function uses parameter sets and depending on the set, decides what type of result to write to the pipeline.

And it seems to work pretty well.

Next, let’s roll some dice. Again I’ll use Get-Random . A single dice roll is as simple as this:

To roll multiple dice, you could do this:

Naturally I wrote a function with a few more bells and whistles because I like shiny toys.

This function lets you choose the number of dice to roll. Notice I use a ValidateRange attribute to limit the number of dice. And in case you are rolling for a game of Dungeons and Dragons which has some extra-sided dice, I gave you that option as well.

In some games, you need the total so I added that as well. One thing that I changed was my rolling technique. In this version I am pre-generating an array of all possible values and then get the specified number of random values.

Not that it is super critical, but this technique is faster.

Here’s the result

If you are debating between different techniques in a script or function, use Measure-Command to see how they perform.

Now you have some tools to build your own PowerShell games and maybe learn something new in the process. If you do, I hope you’ll share. Enjoy!

Friday Fun: A Random PowerShell Console

crayonsThis week I thought we’d have a little fun with the PowerShell console and maybe pick up a few scripting techniques along the way. Today I have a function that changes the foreground and background colors of your PowerShell console to random values. But because you might want to go back to your original settings without completing restarting PowerShell the function allows you to reset to your original values. Oh, and it also supports -Whatif. Here’s what I came up with.

The function will not work properly in the PowerShell ISE so I’ve included some code at the beginning to see if it is running in the ISE. If so, the command displays a warning and bails out. This is a scenario where using Return is completely acceptable as I want to return out of the pipeline without doing anything. I could have used a command like this: Return “This command only works in the PowerShell console.” but that would have written a string object to the pipeline and I don’t want anything to go to the pipeline. Plus, I prefer to use Write-Warning for messages like this.

When you run the command, it tests for the existence of some variables, $savedfg and $savedbg, that are defined in the global scope. You’ll notice the use of the global: prefix. If they are not defined, then the assumption is that this is the first time the command has been run and the variables will be defined with the current values of the $host.ui.rawui.foregroundcolor and $host.ui.rawui.backgroundcolor values. Later, if you use -Reset, the command will use these values to restore your original settings.

Otherwise, the command gets a random color for the System.ConsoleColor enumeration for the background and then another random color for the foreground, that is different than the randomly selected background color.

When you look at the code, you will see that I am specifying in the cmdletbinding attribute that the function supports ShouldProcess. That could also be written [cmdletbinding(SupportsShouldProcess=$True)] but that seems redundant to me. Anyway, the command to make the change, $host.ui.rawui.backgroundcolor= $bg, by itself doesn’t know anything about ShouldProcess and -WhatIf. But I can add my own code using an If statement.

The value for the ShouldProcess() method is the text you see as part of the “…performing operation on target…” message. The text is the target.
set-randomconsol-whatif

All I’ve done is define a variable, $msg, to make the line of code easier to read. As you can see, it is not that difficult to add your own support for -WhatIf. And now, if you get a little bored, mix it up for a fresh perspective. I’ve even included an alias, src, in case you have to type in a console where you can’t see what you’re typing.

Have a terrific weekend.

Friday Fun PowerShell PowerBall Numbers

Like many of you, I dream about hitting the lottery and retiring to live the good life. Unfortunately I rarely play so I guess my odds are winning are pretty slim. But for the latest installment of Friday Fun, I thought I would have PowerShell help me pick some numbers for PowerBall. It is not as easy as you might think and you might pick up a thing or two about objects, arrays and loops. Continue reading

Friday Fun Randomness Abounds

I find it a little ironic that although I like efficiency, structure and order I’m fascinated with randomness. In today’s Friday Fun I want to explore a few ways you might incorporate randomness into your PowerShell scripting. Perhaps you need a random number between 100 and 500. Or a string for file name. There are many paths so let’s (randomly) take this one. Continue reading

St. Patrick’s Bar Order

Let’s have a little fun with St. Patrick’s Day and perhaps even learn a little PowerShell. Continue reading