0

I've got the following powershell code that updates xml nodes with values from Octopus Deploy.

    foreach($key in $OctopusParameters.Keys)
    {
        $myXPath = "$nodeXPath/$($key.Replace(".", "/"))"
        try{
            $node = $doc.SelectSingleNode($myXPath)
        } catch { <# sometimes Octopus passes in crappy data #> }
        if($node){
            Write-Host "Overriding node: '$key'`t`t With value: $($OctopusParameters["$key"])"
            $node.'#text' = $OctopusParameters["$key"]
        }
    }

It's working great, but only if the original xml node has a value.

<something>Replaced_by_octopus</something>  <!-- I work -->
<something></something>                     <!-- I fail -->

The failure is on the line $node.'#text' = $OctopusParameters["$key"] with the following error message

Get-EnvironmentSettings : Exception setting "#text": "The property '#text' cannot be found on this object. Verify that the property exists and can be set."

What do I need to do to be able to update that node regardless of if it's empty or not?

Liam
  • 27,717
  • 28
  • 128
  • 190
Chase Florell
  • 46,378
  • 57
  • 186
  • 376

1 Answers1

0

Instead of setting $node.'#text', you can use the property InnerText

    foreach($key in $OctopusParameters.Keys)
    {
        $myXPath = "$nodeXPath/$($key.Replace(".", "/"))"
        try{
            $node = $doc.SelectSingleNode($myXPath)

            if($node){
                Write-Host "Overriding node: '$key'`t`t Value: '$($OctopusParameters["$key"])'"
                $node.InnerText = $($OctopusParameters["$key"])
            }
        } catch { 
            <# sometimes Octopus passes in crappy data #> 
        } finally {
            $node = $null
        }
    }
Chase Florell
  • 46,378
  • 57
  • 186
  • 376