xmlpowershellazurecscfg

How do you add an element to an empty XML (CSCFG) node in PowerShell


I have an xml file (CSCFG File) like below

XML File:

 <Role name="Role1">
    <Instances count="2" />
    <ConfigurationSettings>
      <Setting name="Key1" value="123456" />
      <Setting name="Key2" value="1234567890" />
      <Setting name="Key3" value="true" />
    </ConfigurationSettings>
    <Certificates>
    </Certificates>
  </Role>

I need to add multiple certificate elements inside certificates node.

To be XML:

<Role name="Role1">
    <Instances count="2" />
    <ConfigurationSettings>
      <Setting name="Key1" value="123456" />
      <Setting name="Key2" value="1234567890" />
      <Setting name="Key3" value="true" />
    </ConfigurationSettings>
    <Certificates>
      <Certificate name="certificate1" thumbprint="12345678" />
      <Certificate name="certificate2" thumbprint="01234567" />
      <Certificate name="certificate3" thumbprint="09876543" />
    </Certificates>
  </Role>

I am trying to do it with the below code

$xmlDoc = [System.Xml.XmlDocument](Get-Content $configFile);
$xmlDoc.Role.Certificates.AppendChild($xmlDoc.CreateElement("Certificate"));
$newXmlCertificate.SetAttribute(“name”,$certName);
$newXmlCertificate.SetAttribute(“thumbprint”,$CertThumbprint);
$xmlDoc.save($configFile)

This is the exceptions am getting while doing it: Method invocation failed because [System.String] does not contain a method named 'AppendChild'.

Please help me out.


Solution

  • Thanks for that Keith, however i had some other issue as the cscfg file has a namespace.

    The below code made it work without any issues. But this is when the xml is tied up to a namespace, specially in a CSCFG File in Azure Deployments

    $xmlDoc = [System.Xml.XmlDocument](Get-Content $configFile);
    $ns = New-Object System.Xml.XmlNamespaceManager($xmlDoc.NameTable)
    $ns.AddNamespace("ns", $xmlDoc.DocumentElement.NamespaceURI)
    
    $node = $xmlDoc.SelectSingleNode("//ns:Certificates", $ns)
    $newNode = $xmlDoc.CreateElement("Certificate");
    $newNode.SetAttribute('name',"certificate1")
    $newNode.SetAttribute('value',"12345678")
    $node.AppendChild($newNode)
    $xmlDoc.save($configFile)
    

    Thanks!!