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.
To update the specific XML node using PowerShell, we first need to select that node with the attribute with SelectSingleNode() method. We have below the XML file from the link stored in SampleXml. XML on C:\Temp location. The above commands will load the XML file and select node with attribute value 'bk102'.
Another way to use PowerShell to parse XML is to convert that XML to objects. The easiest way to do this is with the [xml] type accelerator. By prefixing the variable names with [xml] , PowerShell converts the original plain text XML into objects you can then work with.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With