9

I am using a silent installation command to install software. I am running this command from PowerShell 3.0.

$silentInstall = C:\Users\Admin\Documents\Setup-2.0.exe exe /s /v"EULAACCEPTED=\"Yes\" /l*v c:\install.log /qn"

Invoke-Expression $silentInstall

This runs the command which installs the software, but it does not wait for it to complete and goes ahead with the next lines of code. I want to have control over the installation so that I would know if it's completed or not.

How do I get an error code for the Invoke-Expression cmdlet so that I can get to know if the cmd executed successfully or not?

1

3 Answers 3

25

It depends on how the EXE file runs - sometimes it will kick off a separate process and return immediately, and in such cases this usually works -

$p = Start-Process -FilePath <path> -ArgumentList <args> -Wait -NoNewWindow -PassThru
$p.ExitCode

Otherwise this usually works -

& <path> <args>
$LASTEXITCODE

Or sometimes this -

& cmd.exe /c <path> <args>
$LASTEXITCODE
Sign up to request clarification or add additional context in comments.

2 Comments

$LASTEXITCODE is what I needed when not going with the Start-Process approach.
+1 for the cmd.exe /c trick - this is the only thing that worked to get the exit code when running VC++ installer.
3

It looks like you're running an MSI installer. When running from the console, control is immediately returned while MSI forks a new process to run the installer. There is no way to change this behavior.

What you'll probably need to do is use Get-Process to find a process named msiexec, and wait for it to finish. There is always an msiexec process running, which handles starting new installers, so you'll need to find the msiexec process that started after your install began.

$msiexecd = Get-Process -Name 'msiexec'
C:\Users\Admin\Documents\Setup-2.0.exe exe `
                                       /s `
                                       /v"EULAACCEPTED=\"Yes\" /l*v c:\install.log /qn"
$myMsi = Get-Process -Name 'msiexec' | 
             Where-Object { $_.Id -ne $msiexecd.Id }
$myMsi.WaitForExit()
Write-Verbose $myMsi.ExitCode

1 Comment

Thank you so much, this really helps. This is what I was looking for. I could find the msiexec process running but I could not figure out how to get the exit code (to find if the installation is successful or not.)
3

You shouldn't need to use Invoke-Expression:

& C:\Users\Admin\Documents\Setup-2.0.exe /s /vEULAACCEPTED=Yes /l*v C:\install.log /qn

2 Comments

This should be the accepted answer. Works out of the box, including retrieving the error code by $?.
Note that some setup.exe files (e.g. the ones generated by Inno Setup) return immediately when run with &. I had to use Start-Process -Wait -PassThru for some reason...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.