简体   繁体   中英

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.

This is the powershell command that is in a batch file:

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:

<?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. 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. That's why it said it couldn't find a closing parenthesis. It saw only the opening parenthesis before the Get-Content command.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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