1
votes

We are developing a Windows DSC based deployment system for our in-house .NET application. As part of the current install instruction there is a step to edit the .NET machine.config file. (Yes, I know it is bad that our app requires this, but I can't fix that at the moment).

I'm wondering how other people manage 'common' configuration files with DSC? I can't see any way to do this nicely, but would be delighted to be shown how.

The only method I can think of is to create a couple of custom DSC modules for managing parts of INI files and XML config files.

I'm proposing modules with the following specification:

xIniSetting [string] #ResourceName
{
    IniFile = [string]
    Group = [string]
    Key = [string]
    [ Value = [string] ] # Only applicable if Ensure = Present
    [ Ensure = [string] { Absent | Present } ] 
    [ DependsOn = [string[]] ]
}

and

xXmlConfigElement [string] #ResourceName
{
    ConfigFile = [string]
    ParentSelector = [string] # XPath Selector of element parent
    ElementSelector = [string] # XPath Selector of element itself
    [ ElementXml = [string] ] # Only applicable if Ensure = Present
    [ Ensure = [string] { Absent | Present } ] 
    [ DependsOn = [string[]] ]
}

(Note: I don’t know of any way of getting an arbitrary XPath selector and de-composing it to get a selector for the parent element when the target might not exist so I’ve had to specify both parent and target elements)

To add a custom settings group to machine.config I would then declare something like the following:

xXmlConfigElement CompanySettingsDefinition
{
    ConfigFile = "$env:windir\ \Microsoft.NET\Framework64\v4.0.30319\Config\machine.config"
    ParentSelector = "//configuration/configSections"
    ElementSelector = "//configuration/configSections/section[@name='CompanySettings']"
    ElementXml = "<section name=\"CompanySettings\" type=\"System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\" />"
    Ensure = Present
}

xXmlConfigElement CompanySettingsConfiguration
{
    ConfigFile = "$env:windir\ \Microsoft.NET\Framework64\v4.0.30319\Config\machine.config"
    ParentSelector = "//configuration"
    ElementSelector = "//configuration/CompanySettings"
    ElementXml = "<CompanySettings><add key=\"SettingsKey\" value=\"SettingsValue\" /></CompanySettings>"
    Ensure = Present
}

Is creating these modules the 'best' approach to solve this problem? Have I missed something about DSC that is the 'correct' way to do this?

Any advice would be greatly appreciated.

2

2 Answers

0
votes

When it comes to .config files, there is no one-size-fits-all kind of solution. Did you check the xWebAdministration module in the resource kit? It has a xWebConfigKeyValue that can be used to update application configuration files. If that does not solve the problem you are looking at, you can write a custom resource in the same lines.

0
votes

That's exactly what I have done in our scenarios.

If you had more complicated logic for what goes into the configuration then I would build a resource that encapsulates that logic.