IF (installation was successful) {do A} else {do B}

Hi
I’m rather PS rookie but I’ve managed to setup O365 installtion using PSADT and all looks great.
There is one last thing which I would love to add but just can’t sort it and would appreciate any help.

How and where in Deploy-Application.ps1 I could add a line which says something like:
If (“installaton was sucesfull”) {do A} else {do B}? Is it possible at all?
I was thinking I could check $LASTEXITCODE and based on this do A or B.

Regards.

https://social.technet.microsoft.com/Forums/en-US/c50de74f-4b68-4e90-ad19-0092aa5d61ac/how-to-set-exit-code-in-powershell-which-will-be-used-by-sccm

Thanks but this doesn’t answer my question.
I have tried to insert in Deploy-Application.ps1 this line just for testing:
if ($LASTEXITCODE -eq 0 ) { New-Item C:\Users\Public\Documents\O365\done.txt } else { New-Item C:\Users\Public\Documents\O365\not.txt }
but it does nothing. I have added for testing in Post-Installation section but also I have tried other places and nothing. Hence my question, is that a correct way and if yes where I should add it?

Regards.

I’ve been fooled by Office’s Setup.exe.
The licensing would fail at the start but setup.exe would still return 0 and not an EXE was installed.
I resorted to Test-path "<path>\excel.exe" and that was 3 years ago.

Thanks but with o365 and Click2Run licensing is not an issue as that happens after user run Word and logs in to O365 acc.
What I really would like to know is how I could get exit code from installation and based on that I want to do IF.
I have notticed that if I will do:

Execute-Process -Path "$ExePath" -Parameters "/configure Office365.xml" -WindowStyle Hidden -PassThru

straight after this line if I will use $LASTEXITCODE I get 0 or 1, depnds if installation was sucesfull or not. So seems to be OK.
but If strait after this I will add just for testing:

if ($LASTEXITCODE -eq 0 ) { New-Item C:\Users\Public\Documents\O365\done.txt } else { New-Item C:\Users\Public\Documents\O365\not.txt }

if $LASTEXITCODE is 0 then file done.txt is being created
if &LASTEXITCODE is 1 then file not.txt is not being created.

I’m not really sure if that is a way to do this or there is another proper way of doing this.

Then you want exitcode of Office’s setup.exe.

This is a simplified version of what I did.


$results = Execute-Process -Path "setup.exe" -Parameters "/configure configuration.xml" -ContinueOnError $true -ExitOnProcessFailure $false -PassThru
#CAVEAT: PSADT introduced -ExitOnProcessFailure arround 3.8.3. 
#By default ($ExitOnProcessFailure = $true) will call Exit-Script and anything after this line is ignored
#If you want the goodies that -PassThru gives you, you need  -ExitOnProcessFailure $false

#I collect the log files here but most people don't care. Code removed

#Did it work?
If (3010,1641,0 -contains $($results.ExitCode) ) {
	Write-Log "Exitcode [$($results.ExitCode)] means success!" 
} Else {
	$mainExitCode = $results.ExitCode
	[string]$Message = "${$results.STDErr |Out-String} ${$results.STDOut |Out-String}" #Out-String cleans up the output to make sure Write-log can handle it
	Write-Log "Exitcode [$($results.ExitCode)] means Setup.exe failed. [$Message]" -Severity 3
	Start-Sleep -Seconds 5 #delay to let you read error in the console (if shown)
	Exit-Script -ExitCode $mainExitCode
}
1 Like

Sorry if im out of topic, i will create a new post if you want.

I read somewhere that you could use exit/error code to catch a pending reboot lets say before i start a new required install. Can i do that and what error code can i use?

if ($LASTEXITCODE -eq 0)
{ … }
else
{ … }

To share with you all, i used your discussion and created this to one of my apps :slight_smile:

,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,.-‘¨’-.,

if ((Test-Path -Path ‘C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe’ -PathType Leaf) -or (Test-Path -Path ‘C:\Program Files\Microsoft\Edge\Application\msedge.exe’ -PathType Leaf))
{
$AppInfo = Get-WmiObject Win32_Product -Filter “Name Like ‘Microsoft Edge’”
ForEach ($findBUCKETversions in $AppInfo)
{
$AppInfoName = ($findBUCKETversions).Name
$Ver = ($findBUCKETversions).Version
Write-Host “… den-track … Microsoft Edge install detected, displayname: $AppInfoName” -ForegroundColor Cyan -BackgroundColor DarkGray
#Write-Log -Message “… den-track … Microsoft Edge install detected, displayname: $AppInfoName” -LogType ‘CMTrace’
Write-Host “… den-track … Microsoft Edge install detected, version: $Ver” -ForegroundColor Cyan -BackgroundColor DarkGray
#Write-Log -Message “… den-track … Microsoft Edge install detected, version: $Ver” -LogType ‘CMTrace’
if ($Ver -ge 100.0.1185.29)
{
Write-Host “… den-track … Found new or newer install, aborting script with exit code 0” -ForegroundColor Cyan -BackgroundColor DarkGray
Exit-Script -ExitCode 0 # 767676
}
else
{
Write-Host “… den-track … Exit was not used, continue with install” -ForegroundColor Cyan -BackgroundColor DarkGray
}
}
$AppInfo = $null
$Ver = $null
}
else
{
Write-Host “… den-track … No Microsoft Edge install detected” -ForegroundColor Cyan -BackgroundColor DarkGray
Write-Log -Message “… den-track … No Microsoft Edge install detected” -LogType ‘CMTrace’
}