如何告诉 PowerShell 在开始下一个命令之前等待每个命令结束? [英] How to tell PowerShell to wait for each command to end before starting the next?

查看:61
本文介绍了如何告诉 PowerShell 在开始下一个命令之前等待每个命令结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PowerShell 1.0 脚本来打开一堆应用程序.第一个是虚拟机,其他是开发应用程序.我希望虚拟机在其他应用程序打开之前完成启动.

I have a PowerShell 1.0 script to just open a bunch of applications. The first is a virtual machine and the others are development applications. I want the virtual machine to finish booting before the rest of the applications are opened.

在 bash 中,我只能说 "cmd1 && cmd2"

In bash I could just say "cmd1 && cmd2"

这就是我所拥有的...

This is what I've got...

C:ApplicationsVirtualBoxvboxmanage startvm superdooper
    &"C:ApplicationsNetBeans 6.5in
etbeans.exe"

推荐答案

通常,对于内部命令,PowerShell 在开始下一个命令之前会等待.此规则的一个例外是基于外部 Windows 子系统的 EXE.第一个技巧是管道到 Out-Null 像这样:

Normally, for internal commands PowerShell does wait before starting the next command. One exception to this rule is external Windows subsystem based EXE. The first trick is to pipeline to Out-Null like so:

Notepad.exe | Out-Null

PowerShell 将等到 Notepad.exe 进程退出后再继续.从阅读代码中可以看出这很漂亮但有点微妙.您还可以使用 Start-Process-Wait 参数:

PowerShell will wait until the Notepad.exe process has been exited before continuing. That is nifty but kind of subtle to pick up from reading the code. You can also use Start-Process with the -Wait parameter:

Start-Process <path to exe> -NoNewWindow -Wait

如果您使用的是 PowerShell 社区扩展版本,则为:

If you are using the PowerShell Community Extensions version it is:

$proc = Start-Process <path to exe> -NoNewWindow -PassThru
$proc.WaitForExit()

PowerShell 2.0 中的另一个选项是使用 背景工作:

Another option in PowerShell 2.0 is to use a background job:

$job = Start-Job { invoke command here }
Wait-Job $job
Receive-Job $job

这篇关于如何告诉 PowerShell 在开始下一个命令之前等待每个命令结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆