June 20th, 2010

Hey, Scripting Guy! Weekend Scripter: More Fruity Fun with Windows PowerShell Enumerations

 

Bookmark and Share

 

Microsoft Scripting Guy Ed Wilson here. The neighbor’s dog never seems to sleep. It is up early this morning, singing for its breakfast. I have news for the creature: Its voice is not nearly as great as he thinks it is. Oh, well, for these past several days the hound has been a reliable alarm clock. I only wish his owner could figure out a way to adjust the time for the weekend. As long as I am up, I might as well make a pot of English Breakfast tea and get back to work on my enumeration project.

After I wrote the Weekend Scripter article yesterday, I decided to try an experiment to see if I could make the code a bit more efficient.

Instead of using an If construction and a contains statement such as I used yesterday, could I use a type constraint in the Param section for the function? If I supply a valid enum value to the parameter with the type constraint, it should allow it to pass. If not, it should throw an error. I decided to give it a try and see what would happen. The result is the Get-FruitEnumDemo.ps1 script that is shown here.

Get-FruitEnumDemo.ps1

Function Get-Fruit

{

Param([myspace.fruit]$enumvalue)

switch ([enum]::parse([type]”myspace.fruit”,$enumValue))

{

([myspace.fruit]::apple) { “Apples are good” }

([myspace.fruit]::pear) { “Pears are yummy” }

([myspace.fruit]::kiwi) { “Kiwis are great” }

} #end switch

} #end function

Get-Fruit “plum”

I have used type constraints on input parameters in many of my scripts. Usually, it is limited to a string or to an integer or perhaps a date. Until recently, I have not needed to check a more complex data type. The solution is as easy as using a type constraint for a string or an integer. Put the enumeration name in square brackets. This is shown here:

Param([myspace.fruit]$enumvalue)

When you call the function with a fruit that is not part of the myspace.fruit enumeration, an error is generated. This is shown in the following image.

Image of error generated when function is called with fruit not part of enumeration

Therefore, if a string is submitted to the Get-Fruit function that is not one of the enumeration values, an error is generated. The nice thing about the error is that it lists the allowed enumeration values in the third line of the error message when it says, “The possible enumeration values are “apple, pear, kiwi”.”

In addition to supplying the enumeration by string, I can also supply the value by number. When the Myspace.Fruit enumeration was created a couple weeks ago in a Weekend Scripter post, numeric values were assigned to each property.

By checking against an enumeration type, numeric values or string values can be supplied to the parameter. This is shown in the following image.

Image of supplying values to parameter

The remainder of the Get-FruitEnumDemo.ps1 script works exactly like the script from yesterday’s Weekend Scripter post. It uses the Switch statement to determine what action will be taken for each enumeration value. There is no need for a switch default statement, or an If/Else statement because an invalid enumeration value will not make it past the param statement.

 

Well, that is about it for working with enumerations. I hope you enjoy the rest of your weekend. I think that as long as I am awake, I will head out to my woodworking shop and make some saw dust.

Join us tomorrow when we will begin talking about…oh, wait a second. If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson and Craig Liebendorfer, Scripting Guys

 

Author

0 comments

Discussion are closed.