xml - 添加嵌套的 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>
解决方案
错误消息可能有点简短,但仍然非常准确地解释了问题:
参考节点不是该节点的子节点。
引用节点( 的第二个参数InsertAfter()
)不是调用方法的节点的子节点($doc.unattend
)。您必须调用InsertAfter()
参考节点的父节点。有几种方法可以获取该节点,但在您的场景中,最简单的方法是使用ParentNode
参考节点的属性。
$ref = $doc.unattend.settings.component.RunSynchronous.RunSynchronousCommand[1]
$ref.ParentNode.InsertAfter($new, $ref)
推荐阅读
- python - Pandas - 使用 2 个类创建新列,其中 id 的最后一行是另一个类
- java - Java:使用本地 XSD 和 SchemaFactory 离线验证 XML
- c - strlcpy:源和目标指向同一个对象
- amazon-web-services - kubectl 获取节点无法连接到 AWS EC2 实例上的服务器
- angular - 将自定义 TemplateRef 传递给 NgxDatatable
- javascript - 如何使用 OpenLayers 区分用户和函数对地图移动的变化
- c - 如何在 C 中调用汇编程序函数
- python - 使用 python 'requests' 包按日期排序
- azure - 将应用程序发布到 azure 时,我无法查看/预览应用程序设置
- r - 渲染函数在两个环境中输出不同的文件