首页 > 解决方案 > 使用 Powershell 将值写入 xml 文件

问题描述

我有一个 PS 脚本来从 XML 文件中读取值:

PS D:\> $xml = [xml](Get-Content C:\Users\myuser\.VirtualBox\VirtualBox.xml)
PS D:\> $VMFolder = $xml.VirtualBox.Global.SystemProperties.defaultMachineFolder
PS D:\> echo $VMFolder
D:\VirtualboxVMs

xml文件示例:

<VirtualBox xmlns="http://www.virtualbox.org/" version="1.12-windows">
  <Global>
    <SystemProperties defaultMachineFolder="D:\VirtualboxVMs" defaultHardDiskFormat="VDI" VRDEAuthLibrary="VBoxAuth" webServiceAuthLibrary="VBoxAuth" LogHistoryCount="3" exclusiveHwVirt="false"/>
  </Global>
</VirtualBox>

谁能给我一个例子,如何使用 PowerShell 将 defaultMachineFolder="D:\VirtualboxVMs" 的值更改为 "D:\newlocation"?

标签: xmlpowershell

解决方案


就像是

$xml.VirtualBox.Global.SystemProperties.defaultMachineFolder = "D:\VirtualboxVMs"
$xml.Save("C:\Users\myuser\.VirtualBox\VirtualBox.xml")

应该做的伎俩


推荐阅读