我试图找到一种方法,通过更新 web.config 中 appSettings 元素的 configSource 来更新不同环境的 web.config。
这是我知道的方法。
$xml.get_DocumentElement().appSettings.configSource = $replaced_test
问题是我想要一个基本脚本,我可以将不同的节点传递给我想要更改和更新的脚本,但我不确定如何做到这一点。
例如,我希望能够像这样调用powershell脚本
changeWebConfig.ps1 nodeToChange newValueofNode
我希望这已经足够清楚了。
这是我现在的代码。
$webConfigPath = "C:\web.config"
# Get the content of the config file and cast it to XML
$xml = [xml](get-content $webConfigPath)
#this was the trick I had been looking for
$root = $xml.get_DocumentElement()."system.serviceModel".client.configSource = $replace
# Save it
$xml.Save($webConfigPath)
我遇到的问题是配置节点
我不得不改变它
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
this to
<configuration>
我还不确定如何找到配置节点处于原始状态的节点,但我已经越来越接近了。
function Set-ConfigAppSetting
([string]$PathToConfig=$(throw 'Configuration file is required'),
[string]$Key = $(throw 'No Key Specified'),
[string]$Value = $(throw 'No Value Specified'))
{
if (Test-Path $PathToConfig)
{
$x = [xml] (type $PathToConfig)
$node = $x.SelectSingleNode("//client[@configSource]")
$node.configSource = $Value
$x.Save($PathToConfig)
}
}
set-configappsetting "c:\web.config" CurrentTaxYear ".\private$\dinnernoworders" -confirm