0

I have the following XML:

 <?xml version="1.0" encoding="utf-8"?>
 <Topology>
 <MyProjectserver1 desc="name of the server"></MyProjectserver1>
 </Topology>

I have tried the following, but it did not get update

$path = 'C:\newfolder\data.xml'
$MyProjectserver1 ='XYZAB'
$xml =[xml](Get-Content -Path $path)
$node =$xml.Topology.MyProjectserver1 |
Where {$_.desc -eq 'name of the server'}
$node =$MyProjectserver1
$xml.Save($path)

Once the above PowerShell script is executed, it did not update the XML content.

Ideally, the PowerShell script should update the server name in XML content as below:

 <?xml version="1.0" encoding="utf-8"?>
 <Topology>
 <MyProjectserver1 desc="name of the server">XYZAB</MyProjectserver1>
 </Topology>
8
  • Why are you using PowerShell instead of simply editing the file with a text editor?
    – Gantendo
    Commented Apr 28, 2023 at 7:29
  • Simply editing the file can be done and it is a manual activity. I'm looking to automate this manual activity using powershell, so that script can be scheduled and gets the xml updated Commented Apr 28, 2023 at 7:32
  • The XML above seems to be incorrect but that may be a superuser.com problem. The line that contains "MyProjectserver1" is malformed XML
    – Gantendo
    Commented Apr 28, 2023 at 7:35
  • In the powershell code, Toplogy should be Topology methinks
    – Gantendo
    Commented Apr 28, 2023 at 7:36
  • 1
    Keep in mind you should include minimal reproductible code examples. Your code still has mistakes, even in the 1st line. Please make sure it runs it before submitting next edit.
    – Destroy666
    Commented Apr 28, 2023 at 9:46

1 Answer 1

0

Looking at your example, you need to set InnerText property of the XML node:

$node.InnerText = $MyProjectserver1

See XmlNode.InnerText documentation

4
  • This I have already tried using InnerText and it doesnt update Commented May 2, 2023 at 12:50
  • How so? Show your entire code after applying the change. It's impossible for it to not work, unless e.g. your 'name of the server' string is incorrect.
    – Destroy666
    Commented May 2, 2023 at 12:53
  • It does not work with the code I have posted. Have you tried before answering here and is it worked ? I believe it does not work Commented May 3, 2023 at 7:45
  • Yes, it worked fine and still works fine. Except your code in question has still an obvious breaking quoting bug, because 2 people have not accepted my edit correcting it and you have not corrected it yourself as requested.
    – Destroy666
    Commented May 3, 2023 at 11:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .