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.