我有一个PowerShell 1.0脚本来打开一堆应用程序。第一个是虚拟机,其他是开发应用程序。我希望虚拟机在其他应用程序打开之前完成引导。
在狂欢中我只能说"cmd1 && cmd2"
这就是我得到的..。
C:\Applications\VirtualBox\vboxmanage startvm superdooper
&"C:\Applications\NetBeans 6.5\bin\netbeans.exe"
发布于 2009-11-16 23:08:05
通常,对于内部命令,PowerShell在启动下一个命令之前会等待。此规则的一个例外是基于EXE的外部Windows子系统。第一个技巧是像这样流水线到Out-Null
:
Notepad.exe | Out-Null
PowerShell将等待Notepad.exe进程退出,然后再继续。这很好,但从阅读代码中可以获得一些微妙的信息。您还可以使用带有-Wait
参数的Start-Process
:
Start-Process <path to exe> -NoNewWindow -Wait
如果您使用的是PowerShell社区扩展版本,则为:
$proc = Start-Process <path to exe> -NoNewWindow -PassThru
$proc.WaitForExit()
PowerShell 2.0中的另一个选项是使用background job
$job = Start-Job { invoke command here }
Wait-Job $job
Receive-Job $job
发布于 2011-09-01 23:11:48
除了使用Start-Process -Wait
之外,通过管道传输可执行文件的输出还会让Powershell等待。根据需要,我通常会通过管道连接到Out-Null
、Out-Default
、Out-String
或Out-String -Stream
。Here是一些其他输出选项的长长列表。
# Saving output as a string to a variable.
$output = ping.exe example.com | Out-String
# Filtering the output.
ping stackoverflow.com | where { $_ -match '^reply' }
# Using Start-Process affords the most control.
Start-Process -Wait SomeExecutable.com
我很怀念您引用的CMD/Bash样式运算符(&,&&,||)。看起来我们必须成为more verbose with Powershell。
发布于 2014-06-27 04:00:15
只需使用"Wait-process“:
"notepad","calc","wmplayer" | ForEach-Object {Start-Process $_} | Wait-Process ;dir
工作完成了
https://stackoverflow.com/questions/1741490
复制相似问题