使用PowerShell命令启动进程安装MSI时,出现退出代码1603错误

时间:2020-11-09 15:59:36

标签: powershell windows-installer exit-code

我们希望使用以下脚本在Windows服务器上安装MSI文件,并能够在Windows服务器上安装MSI文件。以下代码对于某些MSI文件运行正常,但对于其他MSI文件则失败。得到退出代码为1603。如果我们进行全新安装,它可以正常工作,但是在尝试重新安装时,我们收到了退出代码:1603错误。所有服务的所有配置设置都相同。

作为mentioned on the Microsoft web site,我们验证了以下条件,但没有适用于我们的情况。

  • Windows Installer尝试安装PC上已安装的应用程序。

  • 您要安装Windows的文件夹 要加密的安装程序包。

  • 包含您要安装Windows Installer软件包的文件夹的驱动器将作为替代驱动器进行访问。

  • SYSTEM帐户对尝试将Windows Installer程序包安装到的文件夹没有完全控制权限。您会注意到错误消息,因为Windows Installer服务使用SYSTEM帐户来安装软件。

代码:

:outer for($i=1; $i -le $attempts; $i++) {
    $timeout = $null
    $proc = Start-Process -filePath $InstallerPath -ArgumentList $InstallCommand -PassThru
    $proc | Wait-Process -Timeout $SecondsToWait -ea 0 -ev timeout
    If (($timeout) -or ($proc.ExitCode -ne 0)) {
        $proc | kill
        $error = "`tFailed To Run $($ProcessTitle)Operations: Exit-Code = $($proc.ExitCode)"
        If(($i+1) -le $attempts) {
            WriteLog -Message($error) -MainLoggingConfigs $MainLoggingConfigs
            Start-Sleep -s $WaitTimePerAttempt
        }
        Else {
            throw $error
        }
    }
    Else {
        break outer
    }

1 个答案:

答案 0 :(得分:2)

如果使用MSI,则需要使用Start-Process msiexec.exe -wait -NoNewWindow而不是Wait-Process。如果您真的担心它会永远运行,请考虑使用PowerShell作业:

Start-Job -Name MyInstall -scriptBlock {
    Start-Process msiexec.exe -NoNewWindow -ArgumentList $MSIArguments
}
Wait-Job -Name MyInstall

然后检查作业Get-Job MyInstall的输出,状态消息,状态,错误,尤其是子作业。

如果您的Start-Process创建尚未结束的子进程,则可能是由于竞争性的安装尝试而导致的错误。尝试使用类似Kevin Marquette's的解决方案来保存详细的MSI日志:

$MSI = 'C:\path\to\msi.msi'
$DateStamp = get-date -Format yyyyMMddTHHmmss
$logFile = "$MSI-$DateStamp.log"
$MSIArguments = @(
    "/i"
    "`"$MSI`""
    "/qn"
    "/norestart"
    "/L*v"
    $logFile
)
Start-Process "msiexec.exe" -ArgumentList $MSIArguments -Wait -NoNewWindow