Removing the roots of a subtree in an Xml file using powershell


hi all

i remove subtree in xml file. found how remove subtree, not root of subtree
here want remove second <disk> ... </disk> data.

removeall() removes <disk></disk> tags.

how remove theses ?

thanks lot !

ml

$xml = [xml] @"
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
    <settings pass="specialize">
        <component name="microsoft-windows-international-core" processorarchitecture="amd64" publickeytoken="31bf3856ad364e35" language="neutral" versionscope="nonsxs" xmlns:wcm="http://schemas.microsoft.com/wmiconfig/2002/state" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">
                <diskconfiguration>
                <disk wcm:action="add">
                    <createpartitions>
                    </createpartitions>
                    <modifypartitions>
                    </modifypartitions>
                    <diskid>0</diskid>
                    <willwipedisk>true</willwipedisk>
                </disk>
                <willshowui>onerror</willshowui>
                <disk wcm:action="add">
                    <createpartitions>
                    </createpartitions>
                    <modifypartitions>
                    </modifypartitions>
                    <diskid>1</diskid>
                    <willwipedisk>true</willwipedisk>
                </disk>

            </diskconfiguration>
</component>
</settings>
            </unattend>

"@
$xml.unattend.settings.component.diskconfiguration

$xml.unattend.settings.component.diskconfiguration.disk[1].removeall()
$xml.save(".\foo.xml")

ps c:\> type .\foo.xml
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
  <settings pass="specialize">
    <component name="microsoft-windows-international-core" processorarchitecture="amd64" publickeytoken="31bf3856ad364e35" language="neutral" versionscope="nonsxs" xmlns:wcm="http://schemas
.microsoft.com/wmiconfig/2002/state" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">
      <diskconfiguration>
        <disk wcm:action="add">
          <createpartitions>
          </createpartitions>
          <modifypartitions>
          </modifypartitions>
          <diskid>0</diskid>
          <willwipedisk>true</willwipedisk>
        </disk>
        <willshowui>onerror</willshowui>
        <disk>
        </disk>

     </diskconfiguration>
    </component>
  </settings>
</unattend>


ml


try following , keep informed it works:

$xml = [xml](get-content "<yourxmlfile>.xml")  [system.xml.xmlnamespacemanager]$nsmgr =$xml.nametable $nsmgr.addnamespace('urn', "urn:schemas-microsoft-com:unattend")  $tmp = $xml.selectsinglenode("//urn:disk[urn:diskid=1]", $nsmgr)  ($xml.selectsinglenode("//urn:diskconfiguration", $nsmgr)).removechild($tmp)  format-outerxml $xml

hope helps,

wizend



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

WIMMount (HSM) causing cluster storage to go redirected (2012r2 DC)

Failed to delete the test record dcdiag-test-record in zone test.com

Azure MFA with Azure AD and RDS