首页 > 解决方案 > 添加嵌套的 XML 元素

问题描述

我正在尝试在下面RunSynchronousCommand的代码中添加一个新元素unattend.xml,但由于它嵌套了几个级别而有点卡住。

到目前为止,我已经尝试过这样的方法(在许多其他方法中!),但现在我的头撞在桌子上,因此感谢您的帮助。

$new = $doc.unattend.settings.component.RunSynchronous.RunSynchronousCommand[0].Clone()
$new.action = 'add'
$new.order = 3
$new.Path = "C:\ProgramData\Amazon\EC2-Windows\Launch\Sysprep\my_powershell_file.ps1"
$doc.unattend.InsertAfter($new, $doc.unattend.settings.component.RunSynchronous.RunSynchronousCommand[1])

这给出了错误:

使用“2”参数调用“InsertAfter”的异常:“参考节点不是此节点的子节点。”

这是unattend.xml

<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
    <settings pass="generalize">
        <component name="Microsoft-Windows-PnpSysprep" 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">
            <DoNotCleanUpNonPresentDevices>true</DoNotCleanUpNonPresentDevices>
            <PersistAllDeviceInstalls>true</PersistAllDeviceInstalls>
        </component>
    </settings>
    <settings pass="specialize">
        <component name="Microsoft-Windows-Shell-Setup" 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">
            <ComputerName>*</ComputerName>
            <CopyProfile>true</CopyProfile>
            <RegisteredOrganization>Amazon</RegisteredOrganization>
            <TimeZone>UTC</TimeZone>
        </component>
        <component name="Microsoft-Windows-Deployment" 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">
            <RunSynchronous>
                <RunSynchronousCommand wcm:action="add">
                    <Order>1</Order>
                    <Path>net user Administrator /ACTIVE:NO /LOGONPASSWORDCHG:NO /EXPIRES:NEVER /PASSWORDREQ:NO</Path>
                </RunSynchronousCommand>
                <RunSynchronousCommand wcm:action="add">
                    <Order>2</Order>
                    <Path>"C:\ProgramData\Amazon\EC2-Windows\Launch\Sysprep\SysprepSpecialize.cmd"</Path>
                </RunSynchronousCommand>
            </RunSynchronous>
        </component>
    </settings>
</unattend>

我正在尝试添加这个:

<RunSynchronousCommand wcm:action="add">
  <Order>3</Order>
  <Path>"C:\ProgramData\Amazon\EC2-Windows\Launch\Sysprep\my_powershell_file.ps1"</Path>
</RunSynchronousCommand>

标签: xmlpowershell

解决方案


错误消息可能有点简短,但仍然非常准确地解释了问题:

参考节点不是该节点的子节点。

引用节点( 的第二个参数InsertAfter())不是调用方法的节点的子节点($doc.unattend)。您必须调用InsertAfter()参考节点的父节点。有几种方法可以获取该节点,但在您的场景中,最简单的方法是使用ParentNode参考节点的属性。

$ref = $doc.unattend.settings.component.RunSynchronous.RunSynchronousCommand[1]
$ref.ParentNode.InsertAfter($new, $ref)

推荐阅读