6

I have a xml-file with a structure as following and I would like to edit this file from the command line.

<server>
    <service>
        <Connector port="8080" password="password1234"/>
    </service>
</server>

I would like to change the password or the port-number.

Does cmd provide this option or do I need an extra tool? I know PowerShell can do it but that's not the best solution for me. (Besides I didn't get it run with powershell :( ).

It would be also ok to search for "password1234" and replace it, because there is a default password in my file which is always the same and this must be replaced.

7
  • You can make the port and password parameters %1 %2. Commented Jul 6, 2015 at 12:44
  • Sorry, can you describe how to do it a bit more detailed? Besides: It's not necessary to geh the port or the password value. It would be also ok to search for "password1234" and replace it, because there is a default password in my file which is always the same and this must be replaced. Commented Jul 6, 2015 at 13:00
  • I found some solutions for the for /f command, but this doesn't include the replacement :( Commented Jul 6, 2015 at 13:04
  • you might get more eyes on your problem by including a tag for your OS (I guessing Windows7(8?)). Note the # of followers on each of your tags. xml people will want you to install xmlstarlet, but then you'd have to learn that too. Search/Replace for password1234 sounds like the "good enough" approach for this problem. Good luck. Commented Jul 6, 2015 at 13:54
  • Good to have added Powershell. That changes the game significantly. Did you try search here for search replace [powershell]. I would expect quite a few solutions and I'm surprised that no PS users have offered some ideas. Read thru a few highly rated PS (with XML) question/questions and see if there is any additional info you need to add to make your question stand out. Good luck. Commented Jul 8, 2015 at 15:26

1 Answer 1

6

To demonstrate one method I use let first create the xml file in your example:

Define a Variable for the XML Filename

$xmlFile = "C:\temp\myconfig.xml"

Define an XML String to save to the file

$xmlFromString = [xml]@"
<server>
<service>
<Connector port="8080" password="password1234"/>
</service>
</server>
"@

Save the xml contents to the file

$xmlFromString.Save($xmlFile)

Resulting file content

Get-Content -Path $xmlFile
<server>
  <service>
    <Connector port="8080" password="password1234" />
  </service>
</server>

Here is the PowerShell code to change the values Get XML content from file

$xml = [xml](Get-Content -Path $xmlFile)

Finds the Element / Node and change the attribute values

$node = $xml.selectSingleNode('//server/service/Connector')
$node.port = "9090"
$node.password = "MyNewPassord4321"

Save the XML Contents Back out

$xml.Save($xmlFile)

Results

Get-Content -Path $xmlFile
<server>
  <service>
    <Connector port="9090" password="MyNewPassord4321" />
  </service>
</server>

Save the commands to a PowerShell ps1 file and execute/run it via PowerShell.

We'll need additional details on what exactly your trying to accomplish such as:

  • What rights will the user / account running the script has?
  • Where will the script be running from? Local PC or server?
  • One or multiple servers/work stations?
  • Executed via Windows Scheduler Task?

Hope that was helpful. - Brooks

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.