September 2nd, 2015

Adding Enums to PowerShell 5 Classes

Doctor Scripto
Scripter

Summary: Ed Wilson, Microsoft Scripting Guy, talks about adding enums to Windows PowerShell 5.0 classes in Windows 10.

Microsoft Scripting Guy, Ed Wilson, is here. It is my birthday week…almost. It is the kind of thing that one needs to begin making plans and preparations for. I know that the Scripting Wife, aka Teresa, is busy working to make this birthday week one of the best ever—at least, I hope that is what she is up to.

The cool thing is that after my birthday week, it will be time for her birthday week, so we take pretty much the entire month of September to celebrate birthdays. Then it is October, and there are a couple of conferences we are going to, and then there is November, and then December. Yep, the last four months of the year are always something special.

Then there is January and February and March…well, you get the idea. If you are into Windows PowerShell, every day of every month of every year is special—and that is what makes it fun and exciting. In fact, we have been talking to my good friend and Microsoft Evangelist, Blaine Barton, about some way cool stuff for next year in the sunshine state.

Adding enums

The advantage of using an enum with your Windows PowerShell 5.0 class is that you have automatic parameter validation in Windows PowerShell. This is essential for solving the old-fashioned problem of the “garbage in garbage out” maxim of data processing. When applied as a type constraint for a property in a Windows PowerShell class, an enum will generate an error if the value supplied is not a valid enumeration.

   Note  This is the third post in the series about creating classes in Windows PowerShell 5.0. You should read the earlier posts
   prior to reading today’s:

   Another Note  I talked about enums last week. If you need to review enums, you should read:

In my car example, it makes sense to use enums for the color of the car and for the maker of the car. I mean afterall, there are only so many colors of cars and so many makers of cars. So if I want to be able to search by color or make, I need to constrain my input. I can say blue and I can find all of my blue cars. But if I have dusty blue, sky blue, ocean blue, greenish blue, light blue, lite blue, lte blue, it quickly becomes a looser proposition.

With Windows PowerShell classes, I cannot actually define my enum inside the Windows PowerShell class, but I can do it outside the class—and in the same script, I will be able to use the enum inside the class. What do I mean?

Well, let's quickly define two enums: MakeOfCar and ColorOfCar:

Enum MakeOfCar

{    

    Chevy = 1

    Ford = 2

    Olds = 3

    Toyota = 4

    BMW = 5

}

Enum ColorOfCar

{

    Red = 1

    Blue = 2

    Green = 3

}

How do I use these enums in my Windows PowerShell 5.0 class? I use it as a type/constraint for the property in my class property section. This is shown here:

[MakeOfCar]$make

[ColorOfCar]$color

Here is my complete script, which includes the Car class and the two enums:

Class Car

{

    [String]$vin

    static [int]$numberOfWheels = 4

    [int]$numberOfDoors

    [datetime]$year

    [String]$model

    [MakeOfCar]$make

    [ColorOfCar]$color

}

Enum MakeOfCar

{    

    Chevy = 1

    Ford = 2

    Olds = 3

    Toyota = 4

    BMW = 5

}

Enum ColorOfCar

{

    Red = 1

    Blue = 2

    Green = 3

}

I run the script, and I create a new instance of the Car class. I store the returned Car object in a variable. I use the NEW() static method from the Car class. The syntax is shown here:

$a = [car]::New()

This code and the returned object are shown here:

Image of command output

Now I assign values to the properties. To do this, I use the numeric values of the enumerations. As you can see, it is a straightforward value assignment:

PS C:\> $a.color = 1

PS C:\> $a.color

Red

PS C:\> $a.make = 2

PS C:\> $a 

vin           : 1234

numberOfDoors : 0

year          : 1/1/0001 12:00:00 AM

model         :

make          : Ford

color         : Red

This is what it looks like in the ISE:

Image of command output

I can also use the “noun” of the enumeration instead of the numeric value. This is shown here:

PS C:\> $a.make = "bmw"

PS C:\> $a

vin           : 1234

numberOfDoors : 2

year          : 1/1/1977 12:00:00 AM

model         : mustang

make          : BMW

color         : Red

But, if I use a value that does not exist in the enumeration, it generates an error, as shown here:

Image of command output

That is all there is to adding enums to Windows PowerShell 5.0 classes. Windows PowerShell Classes Week will continue tomorrow when I will talk about more way cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.