question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Behavior of Test-SPDscParameterState is flawed: it should return false if a key in CurrentValues is missing in DesiredValues

See original GitHub issue

Details 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:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
andikruegercommented, May 11, 2020

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:

function Get-BoundAndDefaultParameters
{
    param($BoundParameters)

    return $BoundParameters.Keys + `RequiredParameters used by the resource` + `DefaultParameters set in the method`
}
0reactions
ykuijscommented, May 15, 2020

Ok great, but then the code does not work correctly. You also specified the following

    if ($PSBoundParameters.ContainsKey("IsTrustBroker") -eq $false)
    {
        $PSBoundParameters.Add("IsTrustBroker", $false)
    }

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Behavior of Test-SPDscParameterState is flawed: it should ...
Behavior of Test-SPDscParameterState is flawed: it should return false if a key in CurrentValues is missing in DesiredValues #1093.
Read more >
Entity Framework creating incorrect foreign keys
I seem to be a victim of making things complicated for myself. I went back over my classes and noticed that I had...
Read more >
Breaking changes in EF Core 5.0
Complete list of breaking changes introduced in Entity Framework Core 5.0.
Read more >
Changing Foreign Keys and Navigations - EF Core
How to change relationships between entities by manipulating foreign keys and navigations.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found