替换powershell中的app.config项节点值

时间:2012-10-22 10:24:07

标签: powershell app-config

拥有以下app.config:

<configuration>
<appSettings>
 <add key="filepath" value="D:\Data" />
 <add key="node2" value="..." />
 ...
</appSettings>
</configuration>

我想用新值替换具有'filepath'键的设置(在这种情况下通过替换'D:\ Data ..')。新值应作为参数传递给powershell脚本。感谢

1 个答案:

答案 0 :(得分:6)

根据评论更新:

$xml = [xml]'<configuration>
<appSettings>
 <add key="filepath" value="D:\Data" />
 <add key="filename" value="test.log" />
</appSettings>
</configuration>'

$xml.configuration.appSettings.add | foreach { if ($_.key -eq 'filepath') { $_.value = "C:\Data" } }
$xml.Save("C:\dell\NewApp.config")

=============================================== ============================================

OLD ANSWER

$xml = [xml]'<configuration>
<appSettings>
 <add key="filepath" value="D:\Data" />
</appSettings>
</configuration>'

$xml.configuration.appSettings.add.value = "C:\Data"
$xml.Save("NewApp.config")

$xml = [xml](Get-Content App.config)
$xml.configuration.appSettings.add.value = "C:\Data"
$xml.Save("NewApp.Config")
相关问题