1

How can i remove the innertext from an xml object via powershell.

cosider:

<XML>
    <AddServer>win-coll3.astest.org</AddServer>
  <SERVERS>
    <Server load="119" url="win-coll.astest.org"/>
    <Server load="119" url="win-coll2.astest.org"/>
  </SERVERS>
</XML>

I Want to clear out the AddServer section

i tried: $FileAsXMLObject.xml.Remove("AddServer") but didnt work

I dont want to delete the section - just cleared out. somesthing like this:

<XML>
    <AddServer> </AddServer>
  <SERVERS>
    <Server load="119" url="win-coll.astest.org"/>
    <Server load="119" url="win-coll2.astest.org"/>
  </SERVERS>
</XML>

1 Answer 1

3

You just need to clear the value of that element. This will work:

$FileAsXMLObject.xml.AddServer = [string]::Empty
Sign up to request clarification or add additional context in comments.

5 Comments

i am able to echo out the innerText of AddServer, but if if try to assign the empty value, i get this error: Property 'AddServer' cannot be found on this object; make sure it exists and is settable. At C:\Users\admin\Desktop\script.ps1:9 char:1 + $servers.xml.AddServer = [string]::Empty + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyAssignmentException
How are you setting $servers? In your question you're using $FileAsXMLObject - I'm assuming this is an XML object with an entire file loaded?
servers is initialized via [xml]$servers = get-content servers.xml. servers.xml's content is the above one. I tried $servers.xml.AddServer = [string]::Empty, and got the errormessage: Property 'AddServer' cannot be found on this object; make sure it exists and is settable. + $servers.xml.AddServer = [string]::Empty + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyAssignmentException
Very strange - works fine for me. What version of powershell are you using? What's the output if you just type $servers.xml?
i think i figured it out. i missed to mention my first line in my servers.xml <?xml version="1.0" encoding="UTF-8"?>. if i remove this line it works. - but why? BTW: PS Powershell on WinServ2012R2 (think it is 4.0)

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.