提问人:Charles Anderson 提问时间:3/3/2010 更新时间:3/3/2010 访问量:1635
如何暂停 Powershell 脚本,直到发生某些外部事件?
How to pause a Powershell script until some external event has occurred?
问:
我正在使用 Powershell 1 脚本启动 Python 程序,然后我想暂停 Powershell,直到 Python 完成其工作。(我拥有 Python 文件的完全所有权。最好的方法是什么?
答:
-1赞
rerun
3/3/2010
#1
您可以使用 get-process commandlet 获取 Process 对象或 ID 来获取 Process 对象,或者您可以像我在下面所示的那样获取它,两者都可以工作。
您可以使用 PID 并调用
$proc = [System.Diagnostics.Process]::GetProcessById($PID)
while(-not $proc.HasExited) {[System.Threading.thread]::sleep(500) }
6赞
Keith Hill
3/3/2010
#2
查看 Wait-Process cmdlet 上的帮助:
man Wait-Process -full
start-process notepad -PassThru | Wait-Process
评论
0赞
Doug Finke
3/3/2010
这是另一种方式:记事本;(PS记事本)。等待退出()
0赞
Charles Anderson
3/4/2010
这很好用,只是 -PassThru 没有被识别。也许是因为我还在使用 v1.0?
0赞
Keith Hill
3/4/2010
在这种情况下,您可能正在使用默认传递的 Start-Process 的 PSCX 版本。虽然,Wait-Process 是 PowerShell 2.0 中的新增功能。所以我的猜测是你正在使用 2.0(如果是这样,$psversiontable将是非空的),并且你正在获得 Start-Process 的 PSCX 版本。
评论