I'm trying to automate some processes with Powershell and browsing here and there a bit, I've put together a little .ps1 file that allows me to mount an Iso image on the next available drive, store the chosen drive as a variable, and use it to retrieve the setup file on the ISO (which also needs an .xml to be installed properly).
$mountResult = Mount-DiskImage C:\My_iso_path\my_iso_file.iso -PassThru
$driveLetter = ($mountResult | Get-Volume).DriveLetter
& "${DriveLetter}:\the_setup_file_onmy_iso.exe" /config "C:\the_xml_config_file.xml"
Dismount-DiskImage -ImagePath C:\My_iso_path\my_iso_file.iso
The script works fine until I try to Dismount the image with the last string, but whenever I run the code as is, the setup fails, as the disk gets dismounted before the process can finish.
Is there any way to make all this work? (like, idk, maybe a wait function?)
Tried using the Wait-Process
command after running:
& "${DriveLetter}:\the_setup_file_onmy_iso.exe" /config "C:\the_xml_config_file.xml"
When you run a GUI application using the call &
operator, the call is asynchronous (as opposed to running a command-line application, which runs synchronously). This means that PowerShell continues with the next line without waiting for the launched application to finish.
To run a GUI application synchronously, you can use Start-Process
with parameter -Wait
:
Start-Process -Wait -Path "${DriveLetter}:\the_setup_file_onmy_iso.exe" -ArgumentList '/config "C:\the_xml_config_file.xml"'
Note: In some cases, when the setup process launches a detached child process, you will still get asynchronous behavior, because Start-Process
doesn't wait for child processes.