1

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.

2
  • 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\")}" Commented Oct 8, 2015 at 20:40
  • The problem is using the -Command parameter for anything other than a simple script you will run into issues where characters such as curly braces and quotes will be misinterpreted by the command prompt before the are they are passed to Powershell. You could could tie yourself in knots by adding several layers of escaping or there is a simpler way - use the -EncodedCommand parameter instead. See this answer Commented Oct 8, 2015 at 23:06

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.