拥有以下app.config:
<configuration>
<appSettings>
<add key="filepath" value="D:\Data" />
<add key="node2" value="..." />
...
</appSettings>
</configuration>
我想用新值替换具有'filepath'键的设置(在这种情况下通过替换'D:\ Data ..')。新值应作为参数传递给powershell脚本。感谢
答案 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")
=============================================== ============================================
$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")