microsoft / DSCParser

Allows the conversion of DSC scripts into PSObject for analysis purposes
MIT License
29 stars 21 forks source link

Variables in array of values are concatenated in error when parsing #30

Open HardingChris opened 1 year ago

HardingChris commented 1 year ago

When parsing a DSC resource that contains variables in the array of values for one of the keys, the variable name ends up concatenated with the next member of the array in error.

For example, parsing the ExcludeGroups of the AADConditionalAccessPolicy resource below (additional properties removed for brevity):

Node localhost
    {
        AADConditionalAccessPolicy CA01
        {
            ExcludeGroups = @($env:BreakGlassGroup,$env:MMDServiceAccountGroup,"MSG_DEV_CASpecificExclusion");
        }
    }

Results in parsed results like this:

    "ExcludeGroups":  [
                          "$env:BreakGlassGroup,env:MMDServiceAccountGroup",
                          "MSG_DEV_CASpecificExclusion"
                      ]

Rather than like this:

    "ExcludeGroups":  [
                          "$env:BreakGlassGroup",
                          "$env:MMDServiceAccountGroup",
                          "MSG_DEV_CASpecificExclusion"
                      ]

The problem appears to be with the Do Until loop at row 256 in Modules/DSCParser.psm1:

Do {
    $currentPropertyIndex++
    $ValueToSet += $group[$CurrentPropertyIndex].Content
} until (($group[$CurrentPropertyIndex + 1].Type -eq 'Operator' -and $group[$CurrentPropertyIndex + 1].Content -eq ',') -or $group[$currentPropertyIndex + 1].Type -eq 'GroupEnd')

Because this iterates once through the loop before checking the Until condition, it appends the ',' operator and the content of the following variable before it stops looping.

From testing it looks as though this can be solved by using a While loop instead:

While (-not (($group[$CurrentPropertyIndex + 1].Type -eq 'Operator' -and $group[$CurrentPropertyIndex + 1].Content -eq ',') -or $group[$currentPropertyIndex + 1].Type -eq 'GroupEnd')) {
    $currentPropertyIndex++
    $ValueToSet += $group[$CurrentPropertyIndex].Content
}

This checks the condition before looping, so encounters the ',' operator and ends the loop. It still supports the advanced variables such as $Test.Nested.Variable, which I think was the original intention of the Do Until loop.