英文:
Powershell .iso install with variable - unable to unmount properly
问题
我正在尝试使用Powershell自动化一些流程,浏览了一些内容后,我创建了一个小的.ps1文件,它允许我挂载一个ISO映像到下一个可用的驱动器,并将所选驱动器存储为一个变量,然后使用它来获取ISO上的设置文件(还需要一个.xml文件才能正确安装)。
$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
脚本在尝试使用最后一个字符串卸载映像时工作正常,但每当我按原样运行代码时,安装程序失败,因为在进程完成之前磁盘被卸载。
是否有办法使所有这些工作正常?(比如,可能使用等待功能?)
尝试在运行以下命令后使用Wait-Process
命令:
& "${DriveLetter}:\the_setup_file_onmy_iso.exe" /config "C:\the_xml_config_file.xml"
英文:
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"
答案1
得分: 0
当你使用&
运算符调用GUI应用程序时,调用是 异步 的(与运行命令行应用程序相反,后者是 同步 运行的)。这意味着PowerShell在不等待启动的应用程序完成的情况下继续执行下一行。
要同步运行GUI应用程序,你可以使用 Start-Process
并使用参数 -Wait
:
Start-Process -Wait -Path "${DriveLetter}:\the_setup_file_onmy_iso.exe" -ArgumentList '/config "C:\the_xml_config_file.xml"'
注意: 在某些情况下,当设置过程启动一个独立的子进程时,你仍然会得到异步行为,因为 Start-Process
不等待子进程。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论