How do I run a Windows installer and get a succeed/fail value in PowerShell?

后端 未结 4 1652
-上瘾入骨i
-上瘾入骨i 2021-02-19 19:21

I would like to install a set of applications: .NET 4, IIS 7 PowerShell snap-ins, ASP.NET MVC 3, etc. How do I get the applications to install and return a value that determines

4条回答
  •  眼角桃花
    2021-02-19 19:35

    These answers all seem either overly complicated or not complete enough. Running an installer in the PowerShell console has a few problems. An MSI is run in the Windows subsystem, so you can't just invoke them (Invoke-Expression or &). Some people claim to get those commands to work by piping to Out-Null or Out-Host, but I have not observed that to work.

    The method that works for me is Start-Process with the silent installation parameters to msiexec.

    $list = 
    @(
        "/I `"$msi`"",                     # Install this MSI
        "/QN",                             # Quietly, without a UI
        "/L*V `"$ENV:TEMP\$name.log`""     # Verbose output to this log
    )
    
    Start-Process -FilePath "msiexec" -ArgumentList $list -Wait
    

    You can get the exit code from the Start-Process command and inspect it for pass/fail values. (and here is the exit code reference)

    $p = Start-Process -FilePath "msiexec" -ArgumentList $list -Wait -PassThru
    
    if($p.ExitCode -ne 0)
    {
        throw "Installation process returned error code: $($p.ExitCode)"
    }
    

提交回复
热议问题