0

This should be rather easily accomplished but I am having issues writing a powershell script to edit an XML file generated by HP's teaming software export function. I believe the issue stems from creating elements who's only contents are sub elements.

I am struggling with AppendChild in the wrong spot, and InnerTexts being empty under the vlan element. Can someone help me with the powershell xml necessary to accomplish adding these 4 lines of xml to the team.xml below?

<vlan>
    <property id='VlanId' value='3'/>
    <property id='VlanName' value='MISC'/>
</vlan>

Here is the abbreviated content of team.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<teamingconfig>
<version UtilityVersion='9.90.0.17' ScriptVersion='3.1'/>
<!-- <team> element 1 -->
<team relnics='1 2'>
<property id='TeamName' value='HP Network Team #1'/>
<property id='OpMode' value='FailOnFault'/>
<!-- <vlan> element 1 -->
<vlan>
       <property id='VlanId' value='1'/>
       <property id='VlanName' value='MGMT'/>
</vlan>
  <!-- <vlan> element 2 -->
<vlan>
       <property id='VlanId' value='2'/>
       <property id='VlanName' value='APPS'/>
</vlan>
</team>
</teamingconfig>

Sorry for the multiple edits- Got what I wanted. Syntax a little strange-

$vlan2=$xml.SelectSingleNode("//property[@value='407'] [@id='VlanId']")
$vlan2.value="100"

New issue, how come if I add a line like at the top of my script:

write-host "Num Args:" $args.length

and then pass the ps1 a variable, it seems to mess with the SelectSingleNode saying the system.object[] doesn't contain a method named selectsinglenode? I am not touching the $xml variable with the args. I am basically trying to just have the vlanid be a command line argument for instance. I have tried making a function and using param but it yields the same problem with the code.

Got it- $xmldata = xml Thanks to all!

1
  • 1
    Can you post the PowerShell you've written already? Commented Jan 23, 2012 at 16:21

1 Answer 1

2

You can clone one and add it back:

$vlan = $xml.SelectSingleNode("//vlan").clone()
$vlan.property[0].value = "3"
$vlan.property[1].value = "MISC"
[void]$xml.SelectSingleNode("//team").AppendChild($vlan)
$xml.save("test.xml")
Sign up to request clarification or add additional context in comments.

4 Comments

This is great as it answers my question exactly. It brings up a new problem, where I would need a reliable way to edit vlanID 1/MGMT to a different ID/name, since that would become the "template" to clone.
@user1165300 - Yes, that is what your sample xml showed. Show us what you have tried, or adapt the one above. You should now have some idea. Edit your question with what you have tried and I will update my answer.
@user1165300 - you have to assign the output of SelectSingleNode to a variable and set the properties on that.
I am having an issue with adding param to the code, and it breaking selectsinglenode. Not sure why, see above...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.