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.
How do you add an element to an empty XML node in PowerShell?
Other than that, you've avoided the biggest issue by showing us what you've tried in a succinct manner. – Keith Hill