In Sitecore 9, one can set up an instance into a specific 'role' that also takes predefined configurations. Further ahead, you may keep your numerous custom configurations next to each other targeting different 'roles' - that avoids clumsy config pathing and keeps settings functionally together in order to simplify maintenance. There is also 'localenv' setting that helps you to distinguish various groups of servers from the same role, but residing in the different environments. Not a nice way of changing role and adding environment by simply replacing a string occurrence:
1 | (Get-Content Web.config).replace( ' <add key="role:define" value="Standalone" />' , '<add key="role:define" value="ContentManagement" /><add key="localenv:define" value="UAT" />' ) | Set-Content Web.config
|
Just a string replacement? Errrghh.... Not a nice solution! Let's make it better, by relying on XML namespace (thanks,
Neil):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | $RptKeyFound=0;
$xml = (get-content $webConfigPath) -as [Xml];
$root = $xml.get_DocumentElement();
foreach($item in $root.appSettings.add)
{
if ($item.key - eq "localenv:define" )
{
$RptKeyFound=1;
}
}
if ($RptKeyFound - eq 0)
{
$newEl=$xml.CreateElement( "add" );
$nameAtt1=$xml.CreateAttribute( "key" );
$nameAtt1.psbase.value= "localenv:define" ;
$newEl.SetAttributeNode($nameAtt1);
$nameAtt2=$xml.CreateAttribute( "value" );
$nameAtt2.psbase.value= "$localEnvName" ;
$newEl.SetAttributeNode($nameAtt2);
$xml.configuration[ "appSettings" ].AppendChild($newEl);
}
$xml.Save($webConfigPath)
|
This same approach can be taken for any other XML-based transforms and replacements.