简体   繁体   English

PowerShell 中是否有等待参数允许脚本等待命令完成

[英]is there a wait parameter in PowerShell to allow the script to wait until a command is completed

I trying to install a software using Start-Process in PowerShell, I would like for the script to wait until a command is completed before proceeding to the next want.我尝试在 PowerShell 中使用 Start-Process 安装软件,我希望脚本等到命令完成后再继续执行下一个命令。 I'm not experienced I tired the script below but it did not work.我没有经验我厌倦了下面的脚本,但它没有用。

Start-Process -Wait -FilePath "C:\Temp\Latitude_5X10_Precision_3550_1.15.0.exe" -ArgumentList "/S" -PassThru

Your Start-Process call is correct, but -Wait invariably only tracks the lifetime of the directly launched process ( C:\Temp\Latitude_5X10_Precision_3550_1.15.0.exe in your case).您的Start-Process调用是正确的,但-Wait始终只跟踪直接启动的进程的生命周期( C:\Temp\Latitude_5X10_Precision_3550_1.15.0.exe在您的情况下)。

That is, you're out of luck if the target process itself spawns yet another process in order to perform its task and then returns before that child process has terminated .也就是说,如果目标进程本身生成另一个进程以执行其任务,然后在该子进程终止之前返回,那么您将不走运。

Additional work is then needed, if even feasible:如果可行,则需要进行额外的工作:

  • If you know the name of the child process, you can try to find and track it via Get-Process .如果您知道子进程的名称,您可以尝试通过Get-Process查找和跟踪它。

  • Alternatively, if you know of an indirect sign that the task has completed, such as the existence of a directory or a registry entry, look for that.或者,如果您知道任务已完成的间接迹象,例如存在目录或注册表项,请查找。


As an aside: console (-subsystem) applications can be invoked directly for synchronous (blocking) execution (eg, foo.exe bar baz or & $fooExePath bar baz ), which is the preferred method, because it connects the application's output streams to PowerShell's streams.顺便说一句:可以直接调用控制台(-subsystem)应用程序以进行同步(阻塞)执行(例如, foo.exe bar baz& $fooExePath bar baz ),这是首选方法,因为它将应用程序的 output 流连接到PowerShell 的流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM