topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday December 13, 2024, 5:39 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Searching and updating XML file in PowerShell  (Read 6605 times)

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,190
    • View Profile
    • Donate to Member
Searching and updating XML file in PowerShell
« on: March 22, 2022, 07:54 AM »
I have the following bit in a Powershell script:

Code: PowerShell [Select]
  1. [xml]$xmlConfig = Get-Content "odata.xml"
  2. $validNodes = $xmlConfig | Select-Xml -XPath '//variable' | %{$_ | ?{((($_.Node.Property.Name -eq "id") -and ($_.Node.Property.Value -eq "this-is-the-node")))}}
  3. if ($validNodes.Count -gt 0)
  4. {
  5.     $validNodes.development = $encryptedcredentials
  6. }

And it's not returning valid nodes, even though the node is there.

The XML file looks like the following:

Code: Text [Select]
  1. <configuration>
  2.   <variable>
  3.     <id>this-is-not-the-node</id>
  4.     <development>2</development>
  5.     <staging>0</staging>
  6.     <production>0</production>
  7.   </variable>
  8.   <variable>
  9.     <id>this-is-the-node</id>
  10.     <development>2</development>
  11.     <staging>0</staging>
  12.     <production>0</production>
  13.   </variable>
  14.   <variable>
  15.     <id>this-is-not-the-node-either</id>
  16.     <development>2</development>
  17.     <staging>0</staging>
  18.     <production>0</production>
  19.   </variable>
  20. </configuration>

I want to select the valid node, then edit the value for development under that node.

Any ideas what I'm doing wrong?


Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,629
    • View Profile
    • Donate to Member
Re: Searching and updating XML file in PowerShell
« Reply #1 on: March 22, 2022, 04:28 PM »
Wouldn't it be easier to use a simple xpath expression to extract the matching element(s)?
Select-Xml -XPath "//variable[id='this-is-the-node']"

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,190
    • View Profile
    • Donate to Member
Re: Searching and updating XML file in PowerShell
« Reply #2 on: March 22, 2022, 05:59 PM »
The problem with that is that I don't want that node, I want the parent variable node so I can update the development value. I was under the impression that wouldn't help me, but I'll try it.

So I tried it, and it did indeed give me the subsection I was looking for- how would I then get the development subnode to change it?

And thanks for the help... this has been stumping me for a few days.

UPDATE: I think I found out how to modify the node, but even though when I expand it shows the node as modified, when I save, it never gets to the file.

Code: PowerShell [Select]
  1. [xml]$xmlConfig = Get-Content "odata.xml"
  2. $validNodes = $xmlConfig | Select-Xml -XPath &#39;//variable[id="odata-keys"]&#39;
  3. if ($validNodes.Count -gt 0)
  4. {
  5.   $validNodes.Node.development = "0"
  6.   $xmlConfig.Save("odata.xml")
  7. }

What am I doing wrong now?
« Last Edit: March 22, 2022, 08:53 PM by wraith808 »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,190
    • View Profile
    • Donate to Member
Re: Searching and updating XML file in PowerShell
« Reply #3 on: March 23, 2022, 01:26 PM »
In case anyone else is wondering, the problem was the assumption on the path.

I added

Code: PowerShell [Select]
  1. $outPath = (Resolve-Path odata.xml).Path
  2. # saves odata.xml in your current working directory
  3. $xmlConfig.Save($outPath)

and it started working.

Thanks for your help!