How to prevent MSI error: Another program is being installed?

How to prevent MSI error: Another program is being installed?

我写了一个小的 PowerShell 脚本来调用不同的 MSI 文件来安装软件。有时我从 Windows 安装程序收到以下错误:

Another program is being installed. Please wait until that installation is complete, and then try installing this software again.

在调用 MSI 文件之前如何防止此错误?我试图将其与 TrustedInstaller 进程相关联。我认为当 PC 正在安装阻止我安装的东西时,该过程会出现。但不幸的是,有时我的安装工作正常,尽管过程是 运行。是否有可以用 PS 捕获的明确指示?

您想查看 _MSIExecute Mutex 是否已设置。

简单地检查 运行 msiexec.exe 不会告诉你,因为 a) msiexec 可能正在处理一个不会阻止你的 UI 序列,或者 b) msiexec 可能有完成安装并等待 10 分钟以停止其服务。

try
{
    $Mutex = [System.Threading.Mutex]::OpenExisting("Global\_MSIExecute");
    $Mutex.Dispose();
    Write-Host "An installer is currently running."
}
catch
{
    Write-Host "An installer is not currently runnning."
}

与此同时,我发现了以下方法:每当当前基于 MSI 的安装 运行 时,都会设置一个特定的注册码。这比信任此时处于活动状态的任何进程更可靠。注册码仅在安装 运行 时设置。

Test-Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Installer\InProgress"