This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
Set-StrictMode behavior breaks valid Configurations from (x)PSDesiredStateConfiguration #188
Labels
discussion
The issue is a discussion.
TLDR: PSDscResources should disable StrictMode in all of its Resources and optionally create parameters in them to enable this setting.
Details of the scenario you tried and the problem that is occurring
I recently needed to write some new Configurations and discovered that PSDscResources was the successor to PSDesiredStateConfiguration and xPSDesiredStateConfiguration. I wrote the new Configurations with PSDscResources and updated one or two (but not all) others to use it as well.
I deployed these changes to a test server (let's call this
Host1
) and had no issues until I provisioned a new host (Host2
). OnHost2
, suddenly many of my long-stable Mofs were failing to apply, whileHost1
had no issues.Only by removing the compliant settings on
Host1
so that the TestScripts would return$false
, was I able to replicate my issues fromHost2
.Some notes:
Set-StrictMode -Off
in the Test and Set bypassed the issue. This option is not available from other Resources such as Registry.Thankfully, uninstalling the PSDscResources Module returned my Mofs to working as expected on both
Host1
andHost2
.After some more research, I found that this behavior is due to
Set-StrictMode -Version 'Latest'
in every PSDscResources Resource. See below for logs and example Configurations.Verbose logs showing the problem
Some individual lines and their accompanying code that caused the error:
The DSC configuration that is used to reproduce the issue (as detailed as possible)
Both of the examples below failed regardless of which module was imported, as long as PSDscResources was installed. Both executed successfully with expected results before using PSDscResources.
Example1:
Error:
Example2:
Error:
Suggested solution to the issue
From what I can tell, no version of Powershell has StrictMode on by default. See documentation here.
This means that for any reasonable person working with Powershell on a regular basis, they would likely not be accustomed to writing scripts to confirm to StrictMode. Instead, many are likely writing code that works in the default PS environment, not necessarily what is perfectly correct.
Additionally, StrictMode was not originally on by default except for a few files (
CompositeResourceHelper, GroupResource, UserResource, RunAsHelper
). And it is explicitly disabled inPSDesiredStateConfiguration.psm1
.For someone migrating from PSDesiredStateConfiguration to PSDscResources, this behavior of defaulting to
Set-StrictMode -Version 'Latest'
is a breaking change. This will prevent others like myself from migrating to PSDscResources due to the effort required to revalidate and redeploy a potentially large number of DSC Mofs. If PSDscResources did not override PSDesiredStateConfiguration system-wide, then this might not be such a large issue as the work could be done one Configuration at a time, but that is not the case.Finally, I would argue that StrictMode being enabled is plainly non-standard for Powershell. It is a nice option, but should not be the default and definitely should not be a forced default.
PSDscResources should disable StrictMode in all of its Resources and optionally create parameters in them to enable this setting.
The operating system the target node is running
Version and build of PowerShell the target node is running
Version of the DSC module that was used ('dev' if using current dev branch)
2.12.0.0
The text was updated successfully, but these errors were encountered: