I have another question. I have an XML document that I would like to update a couple of nodes. I'm looking to copy/clone the device name node to a new node. For a more of an understanding here is the sample XML document:
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Enclave id="OLD">
<device>
<name>G-VDS-GooD</name>
<type>VoIP</type>
<vlan>Voice2Network</vlan>
<inform>PEM</inform>
<outform>PEM</outform>
<RequireCert>0</RequireCert>
</device>
<device>
<name>G-VDS-ENC001</name>
<type>VoIP</type>
<vlan>Voice2Network</vlan>
<inform>PEM</inform>
<outform>PEM</outform>
<RequireCert>0</RequireCert>
</device>
<device>
<name>G-VDS-ENC002</name>
<type>VoIP</type>
<vlan>Voice2Network</vlan>
<inform>PEM</inform>
<outform>PEM</outform>
<RequireCert>0</RequireCert>
</device>
</Enclave>
</Configuration>
Here is what I'm trying to get to:
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Enclave id="OLD">
<device>
<name>G-VDS-GooD</name>
<type>VoIP</type>
<vlan>Voice2Network</vlan>
<inform>PEM</inform>
<outform>PEM</outform>
<RequireCert>0</RequireCert>
</device>
<device>
<name>G-VDS-ENC001</name>
<type>VoIP</type>
<vlan>Voice2Network</vlan>
<cert>
<name>G-VDS-ENC001</name>
</cert>
<inform>PEM</inform>
<outform>PEM</outform>
<RequireCert>0</RequireCert>
</device>
<device>
<name>G-VDS-ENC002</name>
<type>VoIP</type>
<vlan>Voice2Network</vlan>
<cert>
<name>G-VDS-ENC002</name>
</cert>
<inform>PEM</inform>
<outform>PEM</outform>
<RequireCert>0</RequireCert>
</device>
</Enclave>
</Configuration>
Here is my code:
$LoadType = "OLD"
$FileName = "C:\name.xml"
[xml]$FileOriginal = Get-Content $FileName
$Pattern = $FileOriginal.SelectNodes("/Configuration/Enclave[@id = `"$LoadType`"]/device[name[contains(text(), 'G-VDS-ENC')]]")
foreach($Pat in $Pattern) {
$device = $FileOriginal.SelectSingleNode("//Configuration/Enclave[@id = `"$LoadType`"]/device[name[contains(text(), 'G-VDS-ENC')]]")
$devicename = $device.name;
$name = $FileOriginal.CreateElement('name')
foreach($name in $device) {
$devicename = $device.name;
$name = $FileOriginal.CreateElement('name')
$name.InnerText = "$devicename"
}
$cert = $FileOriginal.CreateElement('cert')
$cert.AppendChild($name)
$Pat.InsertAfter( $cert, $Pat.SelectSingleNode('vlan') )
}
$FileOriginal.Save($FileName)
Here was the result:
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Enclave id="OLD">
<device>
<name>G-VDS-GooD</name>
<type>VoIP</type>
<vlan>Voice2Network</vlan>
<inform>PEM</inform>
<outform>PEM</outform>
<RequireCert>0</RequireCert>
</device>
<device>
<name>G-VDS-ENC001</name>
<type>VoIP</type>
<vlan>Voice2Network</vlan>
<cert>
<name>G-VDS-ENC001</name>
</cert>
<inform>PEM</inform>
<outform>PEM</outform>
<RequireCert>0</RequireCert>
</device>
<device>
<name>G-VDS-ENC002</name>
<type>VoIP</type>
<vlan>Voice2Network</vlan>
<cert>
<name>G-VDS-ENC001</name>
</cert>
<inform>PEM</inform>
<outform>PEM</outform>
<RequireCert>0</RequireCert>
</device>
</Enclave>
</Configuration>
I tried to accomplish this through a foreach syntax but I don't know or think thats the correct approach. I also tried a different approach in using the CloneNode cmdlet but I couldn't get that to work either. Anyone have an idea on how to make this work?