Change XML Element value with PowerShell

前端 未结 2 563
醉酒成梦
醉酒成梦 2020-12-03 21:26

I\'m only finding stuff on how to change the attribute values of a XML element here on StackOverflow.

But how do we change the value of the element itself using Pow

2条回答
  •  感动是毒
    2020-12-03 22:01

    InnerText is a property, not a method. It's used like this:

    $element.InnerText = "newtext"
    

    Also, I suspect that your original data (unlike the XML sample you posted) uses namespaces. AFAICS that's the only possible reason why $xml.SelectSingleNode('//Arguments') would return an empty result. XML files exported from the Windows Task Scheduler definitely are namespaced:

    
      
    
    

    Namespaces are not like other node attributes and affect not only the node itself, but also its child nodes. For selecting nodes from an XML with namespaces you need a namespace manager:

    $nsm = New-Object Xml.XmlNamespaceManager($xml.NameTable)
    $nsm.AddNamespace('ns', $xml.DocumentElement.NamespaceURI)
    $element = $xml.SelectSingleNode('//ns:Arguments', $nsm)
    

提交回复
热议问题