简体   繁体   English

使用Powershell从批处理文件更新XML文件

[英]Update xml file with powershell from a batchfile

I want to update a xml file from a batch file using powershell command and think i'm close but have a syntax problem. 我想使用powershell命令从批处理文件更新xml文件,并认为我已经关闭,但是有语法问题。

This is the powershell command that is in a batch file: 这是批处理文件中的powershell命令:

powershell -Command "& {$xml = [xml](Get-Content "C:\Temp\Test\configuration.xml"); $xml.root.settings.installingUser.value = 'NewID'; $xml.Save("C:\Temp\Test\configuration_ny.xml")}"

Example on the configuration.xml: configuration.xml上的示例:

<?xml version="1.0" encoding="UTF-8"?>
<root>
     <test engine="2011.0.0" />
     <settings>
          <installDir value="C:\Program Files\App\Path" />
          <installingUser value="InstallID" />
     </settings>
</root>

The error message say it is missing End Parenthesis and would appreciate all help or suggestion i can get to solve this. 错误消息说它缺少结束括号,将不胜感激我能解决的所有帮助或建议。

To expand on PetSerAl's comment, The argument after -Command is a string surrounded by double quotes. 为了扩展PetSerAl的注释,-Command之后的参数是一个用双引号引起来的字符串。 That double-quoted string itself contains double-quote characters. 该双引号字符串本身包含双引号字符。 Therefore, you need to put a back-slash before the double-quotes surrounding your file names, so the command parser knows those double quote characters are actually part of the string data, and not string delimiters. 因此,您需要在文件名的双引号之前加上反斜杠,以便命令解析器知道这些双引号字符实际上是字符串数据的一部分,而不是字符串定界符。

powershell -Command "& {$xml = [xml](Get-Content "

When the command parser saw your original command, it thought that double-quote after Get-Content was the end of the command. 当命令解析器看到您的原始命令时,它认为Get-Content之后的双引号是命令的结尾。 That's why it said it couldn't find a closing parenthesis. 这就是为什么它说找不到圆括号。 It saw only the opening parenthesis before the Get-Content command. 在Get-Content命令之前,它仅看到左括号。

Thanks for the answers, after some test I found that I could use single quotas and this now works. 感谢您的回答,经过一些测试,我发现我可以使用单个配额,并且现在可以使用了。

powershell -Command "& {$xml = [xml](Get-Content '%InstConfXML%'); $xml.root.settings.installingUser.value = '%InstUsrXML%'; $xml.Save('%InstConfXML%')}"

Thanks 谢谢

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

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