有没有办法让powershell等待安装完成?

时间:2013-10-16 18:08:07

标签: windows powershell wait

我有一个Windows软件包列表,我使用以下命令通过PowerShell安装:

& mypatch.exe /passive /norestart

mypatch.exe正在从列表中传递,它不会等待先前的安装完成 - 它只是继续。它构建了一个巨大的安装窗口,正在等待安装。此外,我无法使用$LASTEXITCODE来确定安装是成功还是失败。

在开始下一个安装之前是否要等待安装?

3 个答案:

答案 0 :(得分:7)

Start-Process <path to exe> -Wait 

答案 1 :(得分:1)

当然,编写一个运行安装程序的批处理脚本。批处理脚本将在返回之前等待安装程序完成。从PowerShell调用脚本,然后等待批处理脚本完成。

如果您可以访问mypatch的编写方式,则可以在完成后创建一些随机文件,PowerShell可以在while循环中检查其是否存在,只是在文件不存在时休眠。

如果不这样做,您也可以在安装程序完成时让该批处理脚本创建一个虚拟文件。

另一种方式,尽管可能最糟糕的是,只要你打电话给安装人员就硬编码睡眠定时器(启动睡眠)。

编辑刚看到JensG的回答。不知道那一个。尼斯

答案 2 :(得分:1)

JesnG在启动过程中是正确的, 但是,正如问题显示传递参数一样,该行应为:

Start-Process "mypatch.exe" -argumentlist "/passive /norestart" -wait

OP还提到确定安装是成功还是失败。我发现在这种情况下,使用“尝试捕获”来获取错误状态的效果很好

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 $_"
}