gpt4 book ai didi

asp.net - 通过传入参数使用Powershell更新web.config中XML元素的configSource

转载 作者:行者123 更新时间:2023-12-02 23:06:22 24 4
gpt4 key购买 nike

我试图通过更新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">

这个到
<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

最佳答案

终于明白了。

$root = $xml.get_DocumentElement().SelectSingleNode("//client[@configSource]").configSource = "test"

当然,我将用变量替换“// client [@configSource]”,这样我就可以传入不同的节点作为参数来创建基本脚本。

关于asp.net - 通过传入参数使用Powershell更新web.config中XML元素的configSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1328490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com