Part 2 – The Troika : PowerShell, YAML and PowerShell Classes

In the last post The Troika : PowerShell, YAML and PowerShell Classes, we took YAML, converted it to PowerShell objects and then used PowerShell v5.0 to cast it to a class. This gave us runtime checking to see if we fat fingered entries in the YAML. This required very little coding, just some simple scaffold setup.

Once you do this, you’re a couple of steps from even more capability.

Note: Notice that using PowerShell v5.0 classes to do this work gives you intellisense in the console and editors.

Defaults and Types

In this example, the YAML has two Purchasers, and we left out Jane’s Age. When we convert, cast and print it, is shows up blank.


Lets set the type the $Age property in the Purchaser class to int.

Now, when we covert and cast, Jane’s age is now 0.


There’s another benfit to typing $Age. What if in the YAML we type a string for Jane’s age?

PowerShell handles checking the type and reporting a very useful error.

Doing Defaults

Going back the PowerShell class, you can set defaults for properties not defined in the YAML (Jane’s Age) and introduce new ones.

Note: Adding $DateCreated to the Purchaser class now makes it legal to use and overridden from the YAML.


Next steps

Now we’ve seen how we can convert YAML from a string (or file) into PowerShell and have a simple way to catch errors, correctly type the data, create defaults and easily introduce new properties.

In upcoming posts we’ll look at how to validate the YAML, for example, what if certain properties are required but not set?

New PowerShell DSC Resource Provider

I created a new PowerShell DSC (Desired State Configuration) Provider that compiles Elm Language files.

Elm is a functional programming language in your browser.

If you’re running PowerShell v5.0? It’s on the PowerShellGallery.
From the prompt PS C:\>Install-Module -Name ElmDSCResource

The source is up on GitHub HERE.

I built an interactive Buzzword Bingo page.


PowerShell Enum and the Three Stooges

The Enum

PowerShell v5.0 comes with a new keyword, enum. An enum is a distinct type that consists of a set of named constants called the enumerator list.

Here, there are two constants, None and Sort. There can be many more.

The Update Function

When specifying parameters in a function, it is easier to constrain the parameter to a valid list so that the user cannot accidentally pass an invalid information. Casting the $action parameter to the [Action] enum lets PowerShell handle invalid input, saving you time from writing error handling code.

Plus, now that $action is of type [Action], you and your users can benefit from intellisense. You don’t need to look up the values, and tab completion works.

In Action

Setup up a list of names:

Now lets call update with an an invalid action.

PowerShell throws a nicely detailed error, even telling you what are valid values for the action parameter. This approach makes your scripting more bullet proof and cuts down emails with questions. All you need to do is create an enum and cast your parameters.

update : Cannot process argument transformation on parameter 'action'. Cannot convert value "BadAction" to type "Action". Error: "Unable to match the identifier name BadAction to a valid enumerator name. Specify one of the following enumerator names and try again: None, Sort"
At line:1 char:8
+ update BadAction $names
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [update], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,update

Calling the update function with the valid Sort option, returns the sorted list of names.

The Full PowerShell Script