7

So I have an .exe.config file that I am trying to search for a specific attribute within, and then edit it using Powershell Version 4.0 in Windows 7, and I am having issues. I have tried several things, and am not having any success. Here is a trimmed down version of the config file that I am using.

<configuration>
  <Config1>
    <section name="text" type="text, text, text=text" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" requirePermission="true" />
  </Config1>
  <Config2>
    <module debugLogLevel="Debug" Version="1.0.0.0" />
    <Interested-Item attribute-1="text-text" attribute2="0">
    </Interested-Item>
    <modules>
      <add name="something1" />
      <add name="something2" />
      <add name="something3" />
      <add name="something4" />
      <add name="something5" />
      <add name="something6" />
      <add name="something7" />
    </modules>
  </Config2>        
</configuration>

How would I go about changing the attribute-1 under Interested-Item, using Powershell? Any help would be greatly appreciated.

Here are a few examples of things I have unsuccessfully attempted.

$File = Get-Content $FileLocation
$XML = [XML]$File

foreach ($attribute in $XML.Config2.Interested-Item)
{
     $attribute = Interested-Item.attribute-1 = "Updated Texted"
}
XML.Save($FileLocation)

This does nothing for me. It doesn't edit the File at all.

$File = Get-Content $FileLocation
$node = $File.SelectSingleNode("/Config2/Interetested-Item[@attribute-1]")
$node.Value = "New-Value"
$File.Save($FileLocation)

This returns the following error.

The property 'Value' cannot be found on this object. Verify that the property exists and can be set.At line:5 char:1
+ $node.Value = "New-Value"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

I tried to implement it using -Xpath's from Get-Help Select-XML and was also unsuccessful with that as well.

The only thing that I have had any success with, which won't work in practice is the following.

(Get-Content $FileLocation) | ForEach-Object{$_ -replace "text-*", "NewText"} | Set-Content $FileLocation

This will forcibly work for the first time, and then afterwards won't be able to update the parameter, due to setting a new value. My intention is to run this script multiple times in order to update a group of config files.

1
  • 2
    If you show what code you have tried, someone could possibly point out what went wrong there. This would be more helpful, I guess. Commented Jul 21, 2014 at 13:36

1 Answer 1

5

There are many ways. For example, you could use XPath:

$File = Get-Content $FileLocation
$XML = [XML]$File

$XPpath = "/configuration/Config2/Interested-Item[@attribute-1]"

# Selecting all nodes that match our $XPath (i.e. all
# '/configuration/Config2/Interested-Item' nodes that have attribute 
# 'attribute-1'.
$nodes = $XML.SelectNodes($XPpath)

# Updating the attribute value for all selected nodes.
$nodes | % { $_.SetAttribute("attribute-1", "foo") }

$XML.OuterXml | Out-File $FileLocation

More info here and generally w3schools.com is your friend when you're dealing with HTML or XML.

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

1 Comment

Thanks alot, that does work! The only portion that I did change was the $XML.OUterXML | Out-File $FileLocation to $XML.Save($FileLocation) only to keep the formatting of the file, when I look at the file to verify the changes. I was struggling with this all day Friday and this morning, so thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.