1

I want to edit the DefaultValue for the Property name = "UseThis" using PowerShell script.

Xml file :

<?xml version="1.0"?>
<ConfigFile xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" IsOverride="false">
  <Section Name="ClientConfiguration" Type="Repository.Config.Configuration">
    <Property Name="Base">
      <DefaultValue>https://app.net:190</DefaultValue>
    </Property>
    <Property Name="Subject">
      <DefaultValue>Box</DefaultValue>
    </Property>
    <Property Name="ApiVersion">
      <DefaultValue>2017-06-15</DefaultValue>
    </Property>
    <Property Name="UseThis">
      <DefaultValue>false</DefaultValue>
    </Property>
    <Property Name="Configuration">
      <DefaultValue>ServiceConfiguration</DefaultValue>
    </Property>
  </Section>
 <Section Name="ClientConfiguration1" Type="Repository.Config.Configuration1">
    <Property Name="Base">
      <DefaultValue>https://app.net:1900</DefaultValue>
    </Property>
    <Property Name="Subject">
      <DefaultValue>Box</DefaultValue>
    </Property>
    <Property Name="ApiVersion">
      <DefaultValue>2011-08-15</DefaultValue>
    </Property>
    <Property Name="UseThis">
      <DefaultValue>false</DefaultValue>
    </Property>
    <Property Name="Configuration">
      <DefaultValue>ServiceConfiguration</DefaultValue>
    </Property>
  </Section>
</ConfigFile>


   
$path = "filepath"
$xml = New-Object xml
$xml.Load($path)

$nodes =  $xml.SelectNodes("/Section/Property")
foreach($Property in $nodes)
{
    switch($Property.name)
    {
        "UseThis" 
        {
            $property.DefaultValue.Value ="True"
            $configuration.save('filepath')
        }
    }
}

The above script is not working as no value is printed for the $nodes. I am very new to powershell and any help would be very helpful. Thanks!

6
  • Please provide valid XML that can be used to reproduce the issue. Commented Nov 15, 2022 at 11:12
  • @zett42 I have edited the xml file, could you please check this once? Commented Nov 15, 2022 at 14:23
  • Thanks for fixing the XML, but it looks like you have accidentally removed the PowerShell code. Commented Nov 15, 2022 at 14:36
  • Sorry, I have added the powershell code. Commented Nov 15, 2022 at 14:42
  • Wouldn't that be .SelectNodes("/ConfigFile/Section/Property") and $property.DefaultValue ="True"? Commented Nov 15, 2022 at 15:51

1 Answer 1

1

It looks like simple coding errors. "/ConfigFile" was missing, and ".Value" shouldn't be there. $configuration should be $xml. It's good to have the full path for these .net methods. They get confused about the current directory.

$path = "file.xml"
$xml = New-Object xml
$xml.Load("$pwd\$path")

$nodes =  $xml.SelectNodes("/ConfigFile/Section/Property")
foreach($Property in $nodes)
{
    switch($Property.name)
    {
        "UseThis" 
        {
            $property.DefaultValue ="True"
            $xml.save("$pwd\$path")
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @js2010, this was very helpful! The below code worked for me : $path = "filepath" $xml = New-Object xml $xml.Load("$path") $nodes = $xml.SelectNodes("/ConfigFile/Section/Property") foreach($Property in $nodes) { switch($Property.name) { "UseThis" { $property.DefaultValue ="True" $xml.save("$path") } } } If I try to add $pwd then I get this error : Exception calling "Save" with "1" argument(s): "The given path's format is not supported."
Inside double quotes like I've done it?
Yes the same way.

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.