Writing a single-instance DSC resource (best practice)

6/12/20173 min to readContributors

In this article

Note: This topic describes a best practice for defining a DSC resource that allows only a single instance in a configuration. Currently, there is no built-in DSC feature to do this. That might
change in the future.

There are situations where you don't want to allow a resource to be used multiple times in a configuration. For example, in a previous implementation of the
xTimeZone resource, a configuration could call the resource multiple times, setting the time zone to a different setting in each resource block:

This is because of the way DSC resource keys work. A resource must have at least one key property. A resource instance is considered unique if the combination of the values of all of
its key properties is unique. In its previous implementation, the xTimeZone resource had only one property--TimeZone, which was required
to be a key. Because of this, a configuration such as the one above would compile and run without warning. Each of the xTimeZone resource blocks is considered unique. This would cause the
configuration to be repeatedly applied to the node, cycling the timezone back and forth.

To ensure that a configuration could set the time zone for a target node only once, the resource was updated to add a second property, IsSingleInstance, that became the key property.
The IsSingleInstance was limited to a single value, "Yes" by using a ValueMap. The old MOF schema for the resource was:

Notice that the TimeZone property is no longer a key. Now, if a configuration attempts to set the time zone twice (by using two different xTimeZone blocks with different TimeZone
values), attempting to compile the configuration will cause an error: