Simplifying my PowerShell version 5 Class Based DSC Resource for Configuring Remote Desktop

Last week I wrote a blog article about a “PowerShell Desired State Configuration Class Based Resource for Configuring Remote Desktop“. Since then I’ve discovered and learned a couple of new things about enumerations in PowerShell that can be used to simply the code even further.

My original code used a couple of enumerations which I’ve removed to show how they can be used to further simply the code:

The code shown in the previous example uses switch statements to translate the numeric values returned from the registry into human readable names. There are several different ways to instantiate a copy of the RemoteDesktop class and call the Get() method:


I’ll remove the switch statements to show that numeric values are indeed returned without them:


My original code from the previously referenced blog article used enumerations instead of ValidateSet, but I’ve since learned that enumerations in PowerShell offer a lot more functionality than just input validation.


By simply using the enumerations, the values are automatically translated from their numeric values returned from the registry into their human readable names that are defined in the enumeration so there’s no need to perform the translation with switch statements.

The only problem at this point is the incorrect value is being return by the Ensure enumeration. By default the first item in the enumeration is 0, the second one is 1, and so on. I could simply swap the order of the two items in the Ensure enumeration to correct this problem:

A better option is to be more declarative and define the value of each item in the enumerations so the order doesn’t matter:

Although the code was already simple, the end result after refactoring it is even simpler code and less of it:

As shown in the previous code example, I also decided to move the code from the SetAuthSetting() and SetTSSetting() methods back into the actual Set() method since other methods don’t call them which means that there’s no redundant code eliminated by separating them and separating them adds complexity.

The updated version of the PowerShell Desired State Configuration Class Based Resource for Configuring Remote Desktop shown in this blog article can be downloaded from my DSC repository on GitHub.


1 Comment

  1. David Wallis

    Can you not further simplify this as you would in C#..

    Return ($this.Ensure -eq $this.GetTSSetting())


Leave a Reply

%d bloggers like this: