Powershell脚本更新XML文件内容 [英] Powershell script to update XML file content

查看:46
本文介绍了Powershell脚本更新XML文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我创建一个Powershell脚本,该脚本将遍历XML文件并更新内容.在下面的示例中,我想使用脚本来拉出并更改Config.button.command示例中的文件路径.将C:\ Prog \ Laun.jar更改为C:\ Prog32 \ folder \ test.jar.请帮忙.谢谢.

Please help me create a Powershell script that will go through an XML file and update content. In the example below, I want to use the script to pull out and change the file path in the Config.button.command example. Change C:\Prog\Laun.jar to C:\Prog32\folder\test.jar. Please help. Thanks.

<config>
 <button>
  <name>Spring</name>
  <command>
     C:\sy32\java.exe -jar "C:\Prog\Laun.jar" YAHOO.COM --type SPNG --port 80
  </command>
  <desc>studies</desc>
 </button>
 <button>
  <name>JET</name>
    <command>
       C:\sy32\java.exe -jar "C:\Prog\Laun.jar" YAHOO.COM --type JET --port 80
    </command>
  <desc>school</desc>
 </button>
</config>

推荐答案

我知道这是一篇过时的文章,但这可能会对其他人有所帮助...

I know this is an old post but this may help others so...

如果您特别了解要查找的元素,则可以像这样简单地指定元素:

If you specifically know the elements that you are looking for then you can simply specify the element like so:

# Read the existing file
[xml]$xmlDoc = Get-Content $xmlFileName

# If it was one specific element you can just do like so:
$xmlDoc.config.button.command = "C:\Prog32\folder\test.jar"
# however this wont work since there are multiple elements

# Since there are multiple elements that need to be 
# changed use a foreach loop
foreach ($element in $xmlDoc.config.button)
{
    $element.command = "C:\Prog32\folder\test.jar"
}

# Then you can save that back to the xml file
$xmlDoc.Save("c:\savelocation.xml")

这篇关于Powershell脚本更新XML文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆