Getting an error but still changing value of key in app.config with powershell

I’m trying to do a very simple script to change a configuration key in my app.config using a powershell script like this

dir *.config -Recurse -PipelineVariable webconfig |
    ForEach {
        $doc = [xml] (Get-Content $_)
        $environment = $doc.configuration.appSettings.add | where {$_.Key -eq 'Environment'} 
        ($environment).value = "ci"
        $doc.Save($webconfig)     
    } 

The problem is that when I run it I’m getting

The property 'value' cannot be found on this object. Verify that the property exists and can be set.

At line:5 char:9

But if after running the script and getting that error I run the same script without the assignment, i.e.

dir *.config -Recurse -PipelineVariable webconfig |
    ForEach {
        $doc = [xml] (Get-Content $_)
        $environment = $doc.configuration.appSettings.add | where {$_.Key -eq 'Environment'} 
        ($environment).value
        $doc.Save($webconfig)     
    } 

It actually displays the value properly changed (which is confirmed in the actual file).

So I’m lost here,

Thanks


Source: New Questions

Leave a Reply