0

I'm trying to add a new RunSynchronousCommand element to the unattend.xml code below, but getting a bit stuck as it's nested a couple of levels in.

So far I have tried approaches such as this (amongst many others!), but banging my head against the desk now, so help appreciated.

$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])

This one gives the error:

Exception calling "InsertAfter" with "2" argument(s): "The reference node is not a child of this node."

This is the 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>

I'm trying to add this:

<RunSynchronousCommand wcm:action="add">
  <Order>3</Order>
  <Path>"C:\ProgramData\Amazon\EC2-Windows\Launch\Sysprep\my_powershell_file.ps1"</Path>
</RunSynchronousCommand>
1
  • I made your XML example well-formed. Commented Apr 29, 2018 at 19:06

1 Answer 1

1

The error message might be a bit brief, but still pretty accurately explains the problem:

The reference node is not a child of this node.

The reference node (the second argument to InsertAfter()) is not a child of the node on which the method is called ($doc.unattend). You MUST call InsertAfter() on the parent of the reference node. There are several ways to get that node, but in your scenario the simplest way would be using the ParentNode property of the reference node.

$ref = $doc.unattend.settings.component.RunSynchronous.RunSynchronousCommand[1]
$ref.ParentNode.InsertAfter($new, $ref)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ansgar! Not come across ParentNode!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.