Reputation: 4080
In this code, the order of actions is clearly defined, save the xml, then "before", then sleep 3, then "after". Most of this code is not relevant but here to demonstrate the way that PowerShell consistently executes the commands in a completely different order to how they are written in the code.
Write-Host "New SynchronousCommand element injected with Order $newOrder"
Write-Host $newCommandLine.InnerText
# Save the modified XML back to the file
$xmlDocument.Save($autoUnattendModifiedXml)
# Update the ComputerName entry in the autounattend.xml to be $computerName
$xml = [xml](Get-Content $autoUnattendXml)
# Find the element with the old computer name and replace it with the new one
$oldComputerNameElement = $xml.unattend | Select-Xml -Namespace @{ns='urn:schemas-microsoft-com:unattend'} -XPath "//ns:ComputerName"
$oldComputerNameElement.Node.InnerText = $ComputerName
# Get also the comment at the start of the xml to replace the entry there
$commentContent = $xml.SelectSingleNode("//comment()[contains(.,'https://schneegans.de/windows/unattend-generator/')]")
$commentContent.InnerText = $commentContent.InnerText -replace '&ComputerName=[^&]+&', "&ComputerName=$ComputerName&"
# Save the modified XML back to the file
$xml.Save($xmlPath)
"before"
sleep 3
"after"
pause
Instead of following this order, "before" happens after the sleep 3 and errors for the xml part come 30-60 seconds later (after robocopy
and oscdimg.exe
actions, I get errors for the xml save operation as if that is happening a good 30-60 seconds later, which I can't understand at all).
I have known in the past that PowerShell will randomly rearrange, but I've never understood it, or been able to control it. I read that the $xml.Save
could be queued, but, I don't want this behaviour, I would really like my commands to operate in the exact sequence that I write them, but it seems impossible, with PowerShell reordering the commands and output in no way that seems rational. Is there a way to get PowerShell to process my commands sequentially?
In particular, I would like the $xml.Save($xmlPath)
to happen before the pause
command, as it is written, but this seems impossible, which is very frustrating as we should be able to get commands to execute in the order that we write them.
New SynchronousCommand element injected with Order 3
cmd /c copy "C:\Windows\Scripts\Install-MySandbox.ps1" "`$env:UserProfile\Install-MySandbox.ps1"
New SynchronousCommand element injected with Order 4
cmd /c powershell.exe -File "`$env:UserProfile\Install-MySandbox.ps1"
#text
-----
3
cmd /c copy "C:\Windows\Scripts\Install-MySandbox.ps1" "`$env:UserProfile\Install-MySandbox.ps1"
4
cmd /c powershell.exe -File "`$env:UserProfile\Install-MySandbox.ps1"
Multiple ambiguous overloads found for "Save" and the argument count: "1".
At C:\Users\roysu\OneDrive\MySandbox-Create-VMs\Create-HyperV-Windows.ps1:463 char:5
+ $xml.Save($xmlPath)
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
*** !!!! THE 3 SECOND SLEEP HAPPENS HERE !!! ****
before
after
Press Enter to continue...:
Upvotes: 0
Views: 58