12

I have a list of Windows packages that I'm installing via powershell using the following command:

& mypatch.exe /passive /norestart

mypatch.exe is being passed from a list and it doesn't wait for the prior install to finish - it just keeps going. It builds up a huge window of installs that are pending installation. Also, I can't use $LASTEXITCODE to determine if the install succeeded or failed.

Is there anyway to make the installs wait before starting the next?

4

3 回答 3

12
Start-Process <path to exe> -Wait 
于 2013-10-16T18:50:39.987 回答
7

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 $_"
}
于 2018-11-25T11:24:51.763 回答
1

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

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

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

还有另一种方式,尽管可能所有这些中最糟糕的方式是在调用安装程序后硬编码一个睡眠计时器(开始睡眠)。

编辑刚刚看到 JensG 的回答。不知道那个。好的

于 2013-10-16T18:36:44.120 回答