Appending attribute value in a XML document via Powershell

N997

I have been trying to develop a script in powershell that could update / append new attribute values ( do not change existing values ) separated by " , " up to 4 values.

The format of the file is

    <configuration>
      <appSettings>        
         <add key="servername" value="server1"/>
      </appSettings>          
   </configuration>

Desired result is

   <configuration>
      <appSettings>        
         <add key="servername" value="server1,server2,server3,server3,server4"/>
      </appSettings>          
   </configuration>

I can add new value but cannot retain the old one by below code

$file = "C:\Users\test\Desktop\server.exe.config"
$xml = [xml] (Get-Content $file)
$XPpath = "/configuration/appSettings/add[@key='servername']"
$nodes = $xml.SelectNodes($XPpath)

foreach ( $n in $nodes ) {
        $n.value = $n.value = 'server2'
        }

I read help documents / searched online but could not find any tip on how to achieve the desired result. Anything I am missing? Your help is appreciated.

Daniel

You're close. Value is a string so just append what you want to it like you would any other string. I use += below to do this.

$xml = [xml]@'
<configuration>
<appSettings>
   <add key="servername" value="server1"/>
</appSettings>
</configuration>
'@

$XPpath = "/configuration/appSettings/add[@key='servername']"
$nodes = $xml.SelectNodes($XPpath)

foreach ( $n in $nodes ) {
    $n.value += ',server2,server3,server4,server5'
}

# or
# $xml.configuration.appSettings.add.value += ",server2,server3,server4"


$xml.configuration.appSettings.add.value
# output: server1,server2,server3,server4,server5

$xml.Save(".\new.xml")

# new.xml:
# <configuration>
#   <appSettings>
#     <add key="servername" value="server1,server2,server3,server4,server5" />
#   </appSettings>
# </configuration>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Appending a value in PowerShell

Search XML attribute value using powershell

Parsing XML with PowerShell: Getting the value of an element with an attribute

Updating an xml attribute with boolean value using powershell

powershell xml select attribute value where clause

Java appending an element to XML document

XML - attribute pairing / value checking via another attribute

Add New value to XML via powershell

Getting selected attribute value of an XML Document using C#

select value of specific attribute of xml document with multiple elements stored in SQL

NullReferenceException thrown when appending child to xml document

Outputting an XML document in Swift after appending an element into it

Appending multiplelines or "nodes" to an XML document correctly

Appending to attribute values of an xml file using Groovy

How to sort xml child via attribute value PhP

Can you use a forward slash "/" in an XML attribute value via DTD?

How to change the value of XML Element attribute using PowerShell?

Powershell: How to update specific character of attribute value of XML node

Appending another child element to xml using powershell

Writing values from xml file accessed via document() by matching attribute values from input file and accessed file

Appending node from an existing XML file onto a new XML document

Appending a function return value to a string variable in Powershell

Reading XML via PowerShell

How I can find any element in XML by matching via pattern attribute and attribute value

How can I find and update a node attribute value in an XML document based on match of a different attribute value in the same node?

Getting the value of an attribute in XML

Deserialization of xml with attribute AND value

Golang XML attribute and value

XML Node Value with Attribute