【问题标题】:Is there a way to make powershell wait for an install to finish?有没有办法让 powershell 等待安装完成?
【发布时间】:2013-10-24 23:51:32
【问题描述】:
我有一个 Windows 软件包列表,我使用以下命令通过 powershell 安装:
& mypatch.exe /passive /norestart
mypatch.exe 正在从列表中传递,它不会等待先前的安装完成 - 它只是继续。它建立了一个巨大的待安装安装窗口。另外,我不能使用$LASTEXITCODE 来确定安装是成功还是失败。
有没有办法让安装在开始下一个之前等待?
【问题讨论】:
标签:
windows
powershell
wait
【解决方案1】:
当然,编写一个运行安装程序的单行批处理脚本。批处理脚本将等待安装程序完成后再返回。从 PowerShell 调用脚本,然后等待批处理脚本完成。
如果您有权访问 mypatch 的编写方式,则可以在完成时创建一些随机文件,以便 PowerShell 可以在 while 循环中检查其是否存在,并在文件不存在时休眠。
如果您不这样做,您还可以让该批处理脚本在安装程序完成时创建一个虚拟文件。
还有另一种方法,尽管可能所有这些方法中最糟糕的就是在调用安装程序后硬编码一个睡眠计时器(开始-睡眠)。
EDIT 刚刚看到 JensG 的回答。不知道那个。不错
【解决方案2】:
Start-Process <path to exe> -Wait
【解决方案3】:
JesnG 使用 start-process 是正确的,
然而,由于问题显示传递参数,该行应该是:
Start-Process "mypatch.exe" -argumentlist "/passive /norestart" -wait
OP 还提到了确定安装是成功还是失败。我发现在这种情况下,使用“try, catch throw”来检测错误状态效果很好
try {
Start-Process "mypatch.exe" -argumentlist "/passive /norestart" -wait
} catch {
# Catch will pick up any non zero error code returned
# You can do anything you like in this block to deal with the error, examples below:
# $_ returns the error details
# This will just write the error
Write-Host "mypatch.exe returned the following error $_"
# If you want to pass the error upwards as a system error and abort your powershell script or function
Throw "Aborted mypatch.exe returned $_"
}