Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 if the installation was successful or not?

like image 253
burnt1ce Avatar asked Jan 20 '11 15:01

burnt1ce


1 Answers

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)"
}
like image 116
Anthony Mastrean Avatar answered Oct 23 '22 01:10

Anthony Mastrean