如何暂停PowerShell直到一个外部进程执行结束后返回?

在PowerShell 里启动Win32的外部进程后,控制会立即返回PowerShell并执行下一条指令。如何让PowerShell停止执行下一条指令,等待前面的进程运行结束呢?

可以使用 [diagnostics.process] 来启动Windows批命令或者其他应用程序,并通过WaitForExit等待进程结束
$batfile = [diagnostics.process]::Start(“D:\Demo\My_Script.bat”)
$batfile.WaitForExit()

然后PowerShell才继续执行余下的指令。

One thought on “如何暂停PowerShell直到一个外部进程执行结束后返回?

  • 星期四 九月 19th, 2013 at 09:29
    Permalink

    How can I have powershell run the silent un-install first wait till it is finished then run the install? After researching I did the below which works, but the blank Notepad window pops up and I had to close it. I don’t want the user to get any notepad window when I deploy this to them, simply uninstall and then install thank you.

    Get-WmiObject -Class Win32_Product | Where-Object {$.Name -eq “On-Screen Takeoff”} | foreach- object -process {$.Uninstall()}

    Notepad.exe | Out-Null

    $arguments=”/quiet” Start-Process “\davisconstruction.com\ROOT\Installs\OnCenter\OST\Testverion3906\ost3906.msi” $arguments

    Reply

Sagun进行回复 取消回复

电子邮件地址不会被公开。 必填项已用*标注