Nesting PowerShell DSC configurations from different files

If I nest DSC configurations in a single file like this, it works fine:

Configuration Secondary {
    Param ($SomeParameter)

    Environment Test {
        Name = $SomeParameter
        Value = "12345"
    }
}

Configuration MyConfiguration {
    Node localhost {
        Secondary TheSecondary {
            SomeParameter = "TestEnvVar"
        }
    }
}

MyConfiguration

Start-DscConfiguration .\MyConfiguration -Wait -Verbose

I want to split my configuration in to two separate files. One will dot-source the other to include the configuration.

Secondary.ps1:

Configuration Secondary {
    Param ($SomeParameter)

    Environment Test {
        Name = $SomeParameter
        Value = "12345"
    }
}

Primary.ps1:

. .\Secondary.ps1

Configuration MyConfiguration {
    Node localhost {
        Secondary TheSecondary {
            SomeParameter = "TestEnvVar"
        }
    }
}

MyConfiguration

Start-DscConfiguration .\MyConfiguration -Wait -Verbose

For some reason this doesn't pick up the parameter passed in to the secondary configuration and so results in the error:

Could not find mandatory property Name. Add this property and try again.
    + CategoryInfo          : ObjectNotFound: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : MI RESULT 6
    + PSComputerName        : localhost

It seems very strange that it works when in the same file but not when dot-sourcing. I thought that dot-sourcing was basically the same as including code in the same file. What am I missing here?


Solution 1:

If you want to reference a configuration from another configuration that is not defined in the same file, you need to use the composite resource pattern.

In a module, you'll create a DscResources folder. In that folder, you'll create a module to hold your composite configurations. The composite configuration will be defined in a file with the extension .schema.psm1. The file will require a module manifest pointing to the schema.psm1 file as the root module.

For more detail and an example, check out the PowerShell team blog - http://blogs.msdn.com/b/powershell/archive/2014/02/25/reusing-existing-configuration-scripts-in-powershell-desired-state-configuration.aspx

Solution 2:

Splatting the parameters helps - following amended Primary.ps1 should work:

. .\Secondary.ps1

Configuration MyConfiguration {
    Node localhost {
        $params = @{ SomeParameter = "TestEnvVar" }
        Secondary TheSecondary @params
    }
}

MyConfiguration

Start-DscConfiguration .\MyConfiguration -Wait -Verbose