简体   繁体   English

Powershell 脚本更新 XML 文件

[英]Powershell Script to Update XML file

Is there any way I can update my XML file using PowerShell:有什么方法可以使用 PowerShell 更新我的 XML 文件:

My Current file format:我当前的文件格式:

   <?xml version="1.0"?>
   <Configuration>
     <SPaths>
        <path>Share</path>
     </SPaths>

My output should be:我的 output 应该是:

   <?xml version="1.0"?>
   <Configuration>
     <SPaths>
        <path>My Share</path>
        <path>My Share/xyz</path>
        <path>My Share/abc</path>
     </SPaths>

I am using the below commands我正在使用以下命令

   $myXML = [xml](Get-Content 'C:\MyPath.config')
   $myXML.Configuration.SPaths.path = "My Share"
   $myXML.Save("C:\MyPath.config")

I need to add 2 more lines of code, Any Help much appreciated, Thank you.我需要再添加 2 行代码,非常感谢任何帮助,谢谢。

with the below-given commands, I am able to add new lines to my XML file.使用下面给出的命令,我可以在我的 XML 文件中添加新行。 Thanks Daniel谢谢丹尼尔

$myXML.Configuration.SPaths.path = "My Share"
$xmlElt = $myXML.CreateElement("path")
$xmlText = $myXML.CreateTextNode("My Share/xyz")
$xmlElt.AppendChild($xmlText)
$myXML.Configuration.SPaths.InsertBefore($xmlElt, $myXML.Configuration.SPaths.legacyUnhandledExceptionPolicy)
$xmlElt1 = $myXML.CreateElement("path")
$xmlText1 = $myXML.CreateTextNode("My Share/abc")
$xmlElt1.AppendChild($xmlText1)
$myXML.Configuration.SPaths.InsertBefore($xmlElt1, $myXML.Configuration.SPaths.legacyUnhandledExceptionPolicy)
$myXML.Save("C:\MyPath.config")```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM