ExitCode 1603 when silently installing a .msi - powershell

I'm trying to write a script that installs a .msi silently. When I run the command from the Powershell command line as a ./thing.msi with the argument /qn, it works just fine.
However, now that it is in a script it is returning a 1603 error ("A fatal error occurred during install"). If I try to switch it up and go to /qb with or without /quite, it runs, but it's not silent. Using -WindowStyle Hidden is doing nothing of note either. Any thoughts?
$InsightInstall = Start-Process -FilePath $PSScriptRoot\support.msi -
ArgumentList "/quiet /qb" -Wait -Passthru -WindowStyle Hidden
if($InsightInstall.ExitCode -eq 0)
{
Write-Host "Installation complete."
}
else
{
Write-Host "Failed with ExitCode" $InsightInstall.ExitCode
pause
}

You don't need to try that hard (I don't think Start-Process is needed). Just run msiexec and specify the package, followed by parameters.
msiexec /i d:\path\package.msi /quiet

Related

Powershell - Start Process MSI Not Installing - Was working before

I have that line of code who was working before, but not anymore:
Start-Process msiexec.exe -ArgumentList '/qn /i "C:\temp\connecteur\Druide\Antidote-Connectix11.msi"' -Wait -Verbose | Out-null
I try to catch error, but it's just do... nothing. I have a short waiting time, and end the script with a Completed status. Any idea what can be the error here ?
Thanks

During Installation process of a software using Start-Process "msiexec.exe" ... -wait, how to fix error and continue the installation process?

For the installation of the "Self-Hosted Integration runtime software, using PowerShell command
Start-Process "msiexec.exe" "/i $path /quiet /passive" -Wait, error comes up for "DIAHostService" not having "LocalSystem" access.
I have the code to change the login to the LocalSystem ("sc.exe config "ServiceName" obj="LocalSystem") But How can I do this during the Installation Process, how do I catch the error and do the required change and continue for the installation automatically?
Code:-
param([string]$path, [string]$authKey)
function Install-Gateway([string] $gwPath)
{
# uninstall any existing gateway
UnInstall-Gateway
Write-Host "Start Gateway installation"
Start-Process "msiexec.exe" "/i $path /quiet /passive" -Wait
Start-Sleep -Seconds 30
Write-Host "Succeed to install gateway"
}
Since I am in no position to replicate what you are doing, take this generic approach as an example.
param([string]$path, [string]$authKey)
function Install-Gateway([string] $gwPath)
{
Write-Warning -Message 'Starting Gateway uninstallation'
UnInstall-Gateway
Try
{
'Starting Gateway installation'
Start-Process -FilePath 'msiexec.exe' -ArgumentList "/i $path /quiet /passive" -Wait -ErrorAction Stop
Start-Sleep -Seconds 30
Write-Verbose -Message 'Gateway installation successful.' -Verbose
}
Catch
{
Write-Warning -Message 'AN error occurred'
$PSItem.Exception.Message
}
}
If you need to run this with a different user, then you need to add the RunAs as part of the Start-Process error logic. Yet using RunAs means starting a new Powershell instance. It will not run in the same process.
References
• PowerShell: Running Executables
Direct - Using the environment path or local folder
Invoke-Expression (IEX)
Invoke-Command (ICM)
Invoke-Item (II)
The Call Operator &
cmd /c - Using the old cmd shell
Start-Process (start/saps)
[Diagnostics.Process] Start()
WMI Win32_Process Create() Method
Stop-Parsing Symbol --%
see also:
'powershell start-process msiexec'
Powershell: Installing MSI files
'PowerShell start-process msiexec' try/catch
about_Try_Catch_Finally - PowerShell | Microsoft Docs
'PowerShell error preference'
Handling Errors the PowerShell Way | Scripting Blog
-ErrorAction and -ErrorVariable
You could also look at using PowerShell jobs for this use case.
start-process 'about PowerShell jobs'

Get return of cmd.exe exit

I develop a PowerShell application. I use for example
Start-Process "cmd.exe" "/c some_command"
#rest of the source code
But when I run this, it is not waiting for cmd.exe to close before executing the rest of the source code.
How can I get cmd.exe "exit event" return to continue to run my PowerShell script?
Or other way to sleep PowerShell script while cmd.exe is running?
I found the answer is to use -wait
Start-Process "cmd.exe" "/c some_command" -wait
You can invoke CMD directly from PowerShell:
cmd.exe /c some_command
In case the executable is provided as a string rather than a bare word you must prefix the statement with the call operator (otherwise using the operator is optional):
& 'cmd.exe' /c some_command
This kind of invocation automatically runs synchronously.
Start-Process invocation is asynchronous by default. You can change the behaviour to synchronous by adding the parameter -Wait.
In addition to using -Wait, you could also check the exit code to verify command success:
[int]$ExitCode = (Start-Process "cmd.exe" "/c some_command" -Wait).ExitCode
# Check exit code
if ($ExitCode -ne 0) {
throw "Something went wrong.
}
This is useful for debugging and checking cmd success.

Find exit code for executing a cmd command through PowerShell

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?
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
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
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

Retrieving MSIEXEC exit code in PowerShell

I need to run an MSIEXEC command line from a PowerShell and check whether the installation was successful or not.
If I do:
msiexec.exe /qn /l*v e:/tmp/surfaceruntime.log /i '\\nas\lui\tools\surfaceruntime2.msi'
(where the specified MSI doesn't exist – that's for testing purposes)
I get a $LASTEXITCODE of 1
OTOH, if I do:
$parms=#("/qn", "/l*v", "e:/tmp/surfaceruntime.log";"/i";"\\nas\lui\tools\surfaceruntime2.msi")
$run=[System.Diagnostics.Process]::Start("msiexec",$parms)
$run.WaitForExit()
$run.ExitCode
I get 1619 (same as %ERRORLEVEL% if I run the command line from CMD).
How come $LASTEXITCODE is incorrect?
Try this:
(Start-Process -FilePath msiexec.exe -ArgumentList $parms -Wait -Passthru).ExitCode