Update the same node with the same value that appears multiple times in xml doc

I have multiple config.xml files and I want to iterate through them all with the following:

  1. nodes to be updated with the same value ‘Someones Account Details’
    This element appears multiple times in all docs

  2. Using variables, replace repeated strings values.

Example of config attached – HOW DO I ATTACH AN EXAMPLE OR PASTE???

Here is the powershell code so far:

$path=“C:\Users\Bex\Documents\Client Projects\QA Refresh\RTI”
$node1="/RTIAgent/Instructions/Instruction/HostAccount"

Get-ChildItem -Path $path -recurse | ForEach-Object {
$configFile = [ xml ](Get-Content $_.fullname)
#rites out all the values of the specified node
#$configFile.RTIAgent.Instructions.Instruction.HostAccount

Select-Xml -xml $configFile -XPath ‘//HostAccount’ |
ForEach-Object {$.Node.’#text’ = ‘some other new Account Details’}
$configFile.Save($
.fullname)
}
read-host “Script Complete - Press ENTER to exit”

It does actually update the HostAccount node, but also throws an error message:

Exception setting “#text”: “The property ‘#text’ cannot be found on this object.
Verify that the property exists and can be set.”
At C:\Users\Bex\Documents\Client Projects\QA Refresh\Dataload\Update RTI Config
Files.ps1:10 char:17

  • ForEach-Object {$_.Node.’#text’ = ‘some other new Account Details’}
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:slight_smile: [], SetValueInvocationException
    • FullyQualifiedErrorId : ExceptionWhenSetting

Why does it throw this error if it works? And also, how can I use the variable rather than specify the xml structure.

And finally, how can I update the AgentVariables (I want to update the text using -replace as the element names will change? (so search for ‘MyDatabase’ and ‘StagingDatabaseName’ as opposed to the node names)

Thank you all

I want to attach a file or paste in an example - but don’t know how???

You will need to use https://gist.github.com/ to post XML

Thank you Rob. I have created a public repository:

https://github.com/rebeccastarr/Config-File-Example.git

Sod it. The xml looks like this:

RTIAgent
  AgentVariables
     SomeServer  MyServer-Want to replace this string /SomeServer
     SomeDatabase  MyDatabase-Want to replace this string  /SomeDatabase (these node names change so not to use these as a means to replace - just the server and database names as they stay consistent)
  /AgentVariables
  Instructions
    Instruction
      HostAccount   HERE I WANT TO EDIT /HostAccount
    /Instruction
    Instruction
      HostAccount /
    /Instruction
    Instruction
      HostAccount  EDIT THIS TOO /HostAccount
    /Instruction
    /Instructions
  /RTIAgent

Why does it throw this error if it works?

The error occurs because you have an empty HostAccount node in your XML. If you populate it, the error doesn’t occur. i.e. there is no InnerText property for the empty node.

And also, how can I use the variable rather than specify the xml structure.

I don’t understand this question, can you clarify?

And finally, how can I update the AgentVariables (I want to update the text using -replace as the element names will change? (so search for ‘MyDatabase’ and ‘StagingDatabaseName’ as opposed to the node names)

If you only have the two nodes, why not just reference FirstChild and LastChild?

$configFile.RTIAgent.AgentVariables.FirstChild.InnerText = 'newServer Value'
$configFile.RTIAgent.AgentVariables.LastChild.InnerText = 'newDatabase Value'