Behavior of Test-SPDscParameterState is flawed: it should return false if a key in CurrentValues is missing in DesiredValues
See original GitHub issueDetails of the scenario you tried and the problem that is occurring
I discovered this while writing SPTrustedSecurityTokenIssuer: Test-SPDscParameterState returns true in the scenario below, while IMO it should return false:
Import-Module -Name "C:\Dev\Github\SharePointDsc\Modules\SharePointDsc\Modules\SharePointDsc.Util\SharePointDsc.Util.psm1"
# Very simplified example
$currentValues = @{Ensure="Present"; IsTrustBroker=$true}
$desiredValues = @{Ensure="Present"}
# Test-SPDscParameterState will return $true, while it should return $false
Test-SPDscParameterState -CurrentValues $currentValues `
-DesiredValues $desiredValues `
-ValuesToCheck @("Ensure", "IsTrustBroker")
Use case: IsTrustBroker is an optional parameter in DSC resource SPTrustedSecurityTokenIssuer. If it is not in DSC configuration, but it is returned by the Get-TargetResource (in $currentValues), then Test-SPDscParameterState should return false, so that Set-TargetResource function is called to update configuration. But currently it returns true and Set-TargetResource function is not called.
Suggested solution to the issue
Test-SPDscParameterState should return false if a key in CurrentValues is missing in DesiredValues
I suspect that updating Test-SPDscParameterState to change this would impact many resources in the module, and potentially break many.
As a workaround, in SPTrustedSecurityTokenIssuer I do the following:
- Explicitly add IsTrustBroker in Test-TargetResource if it is missing
- Explicitly set IsTrustBroker to false in both Test-TargetResource and Get-TargetResource if it’s not true (instead of no value at all)
Workaround in Test-SPDscParameterState, before calling Test-SPDscParameterState:
if ($PSBoundParameters.ContainsKey("IsTrustBroker") -eq $false)
{
$PSBoundParameters.Add("IsTrustBroker", $false)
}
The operating system the target node is running
OsName : Microsoft Windows Server 2019 Datacenter
OsOperatingSystemSKU : DatacenterServerEdition
OsArchitecture : 64-bit
WindowsVersion : 1809
WindowsBuildLabEx : 17763.1.amd64fre.rs5_release.180914-1434
OsLanguage : en-US
OsMuiLanguages : {en-US}
Version of SharePoint that is used (e.g. SharePoint 2016)
SharePoint 2019
Version and build of PowerShell the target node is running
PSVersion 5.1.17763.316
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.316
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Version of the DSC module that was used (‘dev’ if using current dev branch)
dev
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
This is an issue that is common across several DSC resources. IMHO Test should only test required and bound parameters.
Is there any option available to make this test more generic and get rid of the problem of implementing a workaround for each resource?
Pseudo-Code for a helper function to get some information:
Ok great, but then the code does not work correctly. You also specified the following
So if the IsTrustBroker isn’t specified, PS sets it to True as default value. However because default values are not included into the PSBoundParameters variable, you set it to false here. Same is true in the Set method.
I will submit a PR correct the issue