3

OK, I'm feeling like a really big idiot here. I've been working with Powershell for a little while more for administrative purposes at work. That said, scripting is not my strong suit.

Right now, I'm trying to write a PS script to add a section to an XML on a bunch of machines to add settings to workaround an issue we've been having with a certain application

The XML file looks as follows

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
      <add Name="testdata"/>
  </configSections>
  <connectionStrings>
      <add Name="testdata"/>
  </connectionStrings>
  <ProvidersConfiguration>
    <Providers>
      <add Name="testdata"/>
    </Providers>
  </ProvidersConfiguration>
  <FacadeSettings>
    <Providers>
      <add Name="testdata"/>
    </Providers>
  </FacadeSettings>
</configuration>

now, I've been googling and searching for hours and there's something i'm just not getting. because i can load the file in my script, navigate all the settings, even modify existing values, but that's not what I need to do.

I need to add a section like this

<NewSettings>
    <add Name="setting"/>
  </NewSettings>

so that my config file looks like this

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
      <add Name="testdata"/>
  </configSections>
  <connectionStrings>
      <add Name="testdata"/>
  </connectionStrings>
  <ProvidersConfiguration>
    <Providers>
      <add Name="testdata"/>
    </Providers>
  </ProvidersConfiguration>
  <FacadeSettings>
    <Providers>
      <add Name="testdata"/>
    </Providers>
  </FacadeSettings>
   <NewSettings>
      <add Name="setting"/>
   </NewSettings>
</configuration>

it's the NewSettings section I can't figure out and I'm sure that when I wrap my head around it, I'll go "OH...", but right now I'm banging my head on the wall and could use some help

1

1 Answer 1

9

Try this:

# Create xml doc - assumes your xml is file E:\Scratch\test.xml
# If it's already in a variable, use $xml = [xml]$myVariable

$xml = [xml](Get-Content E:\Scratch\test.xml)

# Create new element for <NewSettings>
$newSettings = $xml.CreateElement("NewSettings")

# Create new element for <add>
$add = $xml.CreateElement("add")

# Create attribute "Name", and set its value
$settingsAttribute = $xml.CreateAttribute("Name")
$settingsAttribute.Value = "setting"

# Add attribute to <add>
$add.Attributes.Append($settingsAttribute)

# Add <add> to <NewSettings>
$newSettings.AppendChild($add)

# Add <NewSettings> to <configuration>
$xml.configuration.AppendChild($newSettings)

# Save to file
$xml.Save("E:\Scratch\new.xml")
Sign up to request clarification or add additional context in comments.

2 Comments

ah....So I was trying to do it backwards I think. If I understand this, we're creating the XML table on it's own, and then appending it to the existing XML where I was trying to add to the existing from the start, trying to work my way up(down?) the tree.
OK, tried it out after some editing to make it more human readable for what I want and it's working. I was on the right path to begin with trying to create elements but wasn't quite grasping the part about appending them. I knew I was missing something and now it makes sense. Thanks a LOT Kevin

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.