dsccommunity / SqlServerDsc

This module contains DSC resources for deployment and configuration of Microsoft SQL Server.
MIT License
360 stars 227 forks source link

SqlAudit: Using the property `Reasons` with type `[Reason]` fails if two modules have type `[Reason]` #1831

Closed johlju closed 1 year ago

johlju commented 1 year ago

Problem description

If two resources, each for an indvidual module, both have the property Reasons a configuration that uses those two resources fail to parse. This is because the code [DscProperty()] [Reasons[]] $Reasons fails to parse when it finds the same class name loaded in two modules.

@nyanhp found and reproduce this. @nyanhp created a repository that shows the issue with both class-based resources and MOF-based resources: https://github.com/nyanhp/DscRepro

Verbose logs

At line:11 char:5
+     Import-DscResource -ModuleName SqlServerDsc
+     ~~~~~~~~~~~~~~~~~~
Exception occurred when post-parsing dynamic keyword 'Import-DscResource' with details 'System.Management.Automation.PSInvalidOperationException: Resource name 'Reason' is already 
being used by another Resource or Configuration.

DSC configuration

# Both resources must have the read-only property `Reasons`.
Configuration Example
{
    param
    (
        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $SqlAdministratorCredential
    )

    Import-DscResource -ModuleName ComputerManagementDsc
    Import-DscResource -ModuleName SqlServerDsc

    node localhost
    {
        PSResourceRepository 'Register PSGallery PSRepository'
        {
            Name    = 'PSGallery'
            Ensure  = 'Present'
            Default = $true
        }

        SqlAudit FileAudit_Server
        {
            Ensure       = 'Absent'
            ServerName   = 'localhost'
            InstanceName = 'SQL2022'
            Name         = 'FileAudit'
            Credential   = $SqlAdministratorCredential
        }
    }
}

Suggested solution

There is no fix for this as it is a external problem with PowerShell DSC. We must remove the property Reasons from the resource.

SQL Server edition and version

n/a

SQL Server PowerShell modules

n/a

Operating system

Windows Server

PowerShell version

WMF 5.x

SqlServerDsc version

v16.1.0-preview0009
johlju commented 1 year ago

I labelled this a breaking change, but don't think we should bump major version for this change. It is more of a bug, and will only affect users that actually started using the property, and that will fail as soon as their configuration will use another resource from another module that also uses Reasons.

johlju commented 1 year ago

There should be no need to remove the property Reasons. See more information in issue https://github.com/dsccommunity/DscResource.Base/issues/4.