1

I have the following content in a XML file. I'm trying to update the XML file using powershell script

<?xml version="1.0" encoding="utf-8"?>
<Topology>
  <Core>
    <SERVER1 desc="computer name server"></SERVER1>
    <SERVER2 desc="computer name server"></SERVER2>
    <CORE1 desc="application core"></CORE1>
    <CORE2 desc="application core"></CORE2>
  </Core>
    <Credentials>
          <AppCredentials>
              <USERNAME></USERNAME>
              <PASSWORD></PASSWORD>
          </AppCredentials>
          <ServicesCredentials>
              <SC_USERNAME></SC_USERNAME>
              <SC_PASSWORD></SC_PASSWORD>
              <SC_DOMAIN></SC_DOMAIN>
          </ServicesCredentials>
    </Credentials>
</Topology>

I have written the following so that the XML file will get updated. However, it is not working as expected.

$xmldata =[XML](Get-Content C:\Config.xml)
$xmldata. Core.SERVER1.InnerText = 'Admin'
$xmldata. Core.SERVER2.InnerText = 'Nimda'
$xmldata. AppCredentials.USERNAME.InnerText = 'Account'
$xmldata. AppCredentialsPASSWORD.InnerText = '123'
$xmldata. Save((Resolve-Path C:\Config.xml). Path)

I have received the following error.

The property 'InnerText' cannot be found on this object. Verify that the property exists and can be set. At C:\Users\Administrator\Documents\replacetokendistributedtest.ps1:2 char:1

  • $xmldata. Core.SERVER2.InnerText = 'Nimda'
  •   + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : PropertyNotFound
    
    

Finally, the XML file should look like as below when we run the above script

<?xml version="1.0" encoding="utf-8"?>
<Topology>
  <Core>
    <SERVER1 desc="computer name server">Admin</SERVER1>
    <SERVER2 desc="computer name server">Nimda</SERVER2>
    <CORE1 desc="application core"></CORE1>
    <CORE2 desc="application core"></CORE2>
  </Core>
    <Credentials>
          <AppCredentials>
              <USERNAME>Account</USERNAME>
              <PASSWORD>123</PASSWORD>
          </AppCredentials>
          <ServicesCredentials>
              <SC_USERNAME></SC_USERNAME>
              <SC_PASSWORD></SC_PASSWORD>
              <SC_DOMAIN></SC_DOMAIN>
          </ServicesCredentials>
    </Credentials>
</Topology>

Please let me know any additional changes are required for this to work

4

1 Answer 1

1
$xmldata = [XML](Get-Content C:\temp\config.xml)
$xmldata.Topology.Core.SERVER1.desc = 'Admin'
$xmldata.Topology.Core.SERVER2.desc = 'Nimda'
$xmldata.Topology.Credentials.AppCredentials.USERNAME = 'Account'
$xmldata.Topology.Credentials.AppCredentials.PASSWORD = '123'
$xmldata. Save("C:\temp\Config.xml")

But it's not a secure way, cause credentials are in clear text in a simple text file (a xml file is a text file).

Regards

1
  • +1 from me. I don't know why you got down-voted when your code works fine. Commented Sep 21, 2023 at 13:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .