Find exit code to execute cmd command via PowerShell

Thus, I use the unattended installation command to install the software. I run this command from PowerShell 3.0

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

The command that installs the software is executed, but without waiting for the completion and continuation of the following lines of code. I want to have control over the installation so that I know if it is complete or not.

How to get an error code for the Invoke-expression cmdlet so that I can find out if cmd was successful or not?

+6
source share
3 answers

It looks like you are running the MSI installer. When launched from the console, control returns immediately, and MSI creates a new process to launch the installer. Cannot change this behavior.

You may need to use Get-Process to find the process named msiexec and wait for it to complete. The msiexec process is always executed, which handles the launch of new installers, so you need to find the msiexec process that started after the installation began.

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

Depends on how exe works - sometimes it starts a separate process and returns immediately, in such cases it usually works -

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

Otherwise, this usually works -

 & <path> <args> $LASTEXITCODE 

Or sometimes it is -

 & cmd.exe /c <path> <args> $LASTEXITCODE 
+16
source

You do not need to use invoke-expression .

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

Source: https://habr.com/ru/post/973735/


All Articles