有没有一种方法可以使PowerShell等待安装完成?

12

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

& mypatch.exe /passive /norestart

mypatch.exe是从列表中传递的,它不会等待先前的安装完成——它只会继续进行。这会积累大量待安装的窗口。此外,我无法使用$LASTEXITCODE来确定安装是否成功或失败。

有没有办法在开始下一个安装之前让安装程序等待?

3个回答

13
Start-Process <path to exe> -Wait 

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

当我在AWS云上从我的用户数据脚本运行此命令时,它对我来说毫无用处(意思是完全没有效果,也没有调用脚本)。如果有人能够提供一个在我的情况下有效的更新答案,我将不胜感激。 - gk_2000

1
当然,编写一行批处理脚本来运行安装程序。批处理脚本将等待安装程序完成后返回。从PowerShell调用该脚本,PowerShell将等待批处理脚本完成。
如果您可以访问mypatch的编写方式,您可以在完成时创建一些随机文件,PowerShell可以在while循环中检查其存在性,而在文件不存在时只需休眠即可。
如果没有,您还可以让批处理脚本在安装程序完成时创建一个虚拟文件。
另一种方法,尽管可能是所有这些方法中最差的一种,就是在调用安装程序后硬编码睡眠计时器(start-sleep)。
编辑刚看到JensG的答案。不错。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接