Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 399
user3543477 Avatar asked Aug 12 '14 23:08

user3543477


Video Answer


1 Answers

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
like image 182
Andy Arismendi Avatar answered Sep 24 '22 07:09

Andy Arismendi