Home > Net >  How to navigate to and change value in XML config file with Powershell?
How to navigate to and change value in XML config file with Powershell?

Time:08-31

I am attempting to change Value UseFunction="false" Value="Production" to Value UseFunction="false" Value="Training" in the following XML:

<?xml version="1.0" encoding="utf-8"?>
<ParametersInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ParameterGroup TestRequired="false" Name="CommonSystemConfig" xmlns="http://www....">
        <Parameter Name="DefaultEnvironmentId" Text="DefaultEnvironmentId" ParameterType="List">
          <Value UseFunction="false" Value="Production">
            <ValueList>
              <string>Production</string>
              <string>Training</string>
              <string>Staging</string>
            </ValueList>
          </Value>
          <AutoSetValue UseFunction="false" Value="Production" />
          <IsUppercaseInput>false</IsUppercaseInput>
          <Description>The value can only be "Production" or "Training" or "Staging"</Description>
          <IsSetSuccessfully>true</IsSetSuccessfully>
          <Visible Type="0" Value="True" />
          <IsReadOnly Type="0" Value="False" />
          <AllowEmpty Type="0" Value="True" />
        </Parameter>

I was provided a script by u/Mathias R. Jessen, but then I noticed that I made some mistakes in my post and had to delete it. Here is where I am now, but the value is not being changed in the target XML file:

# load xml document from disk
$xmlFilePath = 'c:\Userdata\ParamBkUp.xml'
$rootedPath = Convert-Path $xmlFilePath

$xmlDoc = [xml]::new()
$xmlDoc.Load($rootedPath)

$nodesToChange = $xmlDoc |Select-Xml -XPath '//ParametersInfo/ParameterGroup[@Name="CommonSystemConfig"]/Parameter[@Name="DefaultEnvironmentId"]/Value[@Value="Production"]'

foreach($result in $nodesToChange){
    # update the Value attribute on the discovered `Value` nodes
    $result.Node.SetAttribute('Value', 'SomeOtherEnvironmentName')
}

# save changes to disk
$xmlDoc.Save($rootedPath)

CodePudding user response:

I solved issue with previous posting, but it was closed. Here is a solution with PowerShell and xml linq

using assembly System 
using assembly System.Linq
using assembly System.Xml.Linq 

$Filename = "c:\temp\test.xml"
$xDoc = [System.Xml.Linq.XDocument]::Load($Filename)

$descendants = $xDoc.Descendants()

$AutoSetValue = [System.Linq.Enumerable]::Where($descendants, [Func[object,bool]]{ param($x) $x.Name.LocalName -eq "AutoSetValue"})
Write-Host "AutoSetValue = " $AutoSetValue
$AutoSetValue.SetAttributeValue("Value","Training")
Write-Host "AutoSetValue = " $AutoSetValue

Here is code that should solve your issue

using assembly System 
using assembly System.Linq
using assembly System.Xml.Linq 

$Filename = "c:\temp\test.xml"
$xDoc = [System.Xml.Linq.XDocument]::Load($Filename)

$descendants = $xDoc.Descendants()

$DefaultEnvironmentId = [System.Linq.Enumerable]::Where($descendants, [Func[object,bool]]{ param($x) ($x.Name.LocalName -eq "Parameter") -and ([string]$x.Attribute("Name").Value -eq "DefaultEnvironmentId")})
Write-Host "DefaultEnvironmentId = " $DefaultEnvironmentId
$AutoSetValue = [System.Linq.Enumerable]::Where($DefaultEnvironmentId.Descendants(), [Func[object,bool]]{ param($x) $x.Name.LocalName -eq "AutoSetValue"})
Write-Host "AutoSetValue = " $AutoSetValue
$AutoSetValue.SetAttributeValue("Value","Training")
Write-Host "AutoSetValue = " $AutoSetValue
  • Related