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

Ste*_*asz 5 powershell app-config

拥有以下app.config:

<configuration>
<appSettings>
 <add key="filepath" value="D:\Data" />
 <add key="node2" value="..." />
 ...
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

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

rav*_*nth 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")
Run Code Online (Sandbox Code Playgroud)

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

老答复

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

$xml.configuration.appSettings.add.value = "C:\Data"
$xml.Save("NewApp.config")
Run Code Online (Sandbox Code Playgroud)

要么

$xml = [xml](Get-Content App.config)
$xml.configuration.appSettings.add.value = "C:\Data"
$xml.Save("NewApp.Config")
Run Code Online (Sandbox Code Playgroud)