添加嵌套的XML元素

时间:2018-04-29 18:42:44

标签: xml powershell

我正在尝试将新的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>

1 个答案:

答案 0 :(得分:1)

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

  

引用节点不是此节点的子节点。

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

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