Problem during installation

Hi guys,

I tried to install the software on the script and successfully completed the installation. But if during the installation the software turns out to be open, he first asks me to close it uninstalls it and then goes to error because during the installation phase he tries the uninstall again. I have tried a thousand ways but I can’t find a solution. I should add that if I remove from the installation phase, the uninstallation of the old version, and the software is closed he does not uninstall it.

#region INSTALLATION
	if ($deploymentType -ine 'Uninstall' -and $deploymentType -ine 'Repair') {

	#region PRE-INSTALLATION
		[string]$installPhase = 'Pre-Installation'
		#*====================================PRE-INSTALLATION BEGIN==================================================================

		# check for pending reboot and stop script execution on true with exit code 1641, if not running in task sequence.
		if ($MYBusiness_CheckForReboot){
			Set-Reboot -ForceExitScript -OnlyOnPendingReboot -MandatoryDeviceRestart
		}
		
		if (get-process smartgit) {
	    $AppRunning = $true
		

		if ($MYBusiness_UseDialogs){
			if ($MYBusiness_AllowDefer){
				Show-InstallationWelcome -AllowDefer -DeferTimes 4 -PersistPrompt -TopMost $False -MinimizeWindows $false
			}
			if ($MYBusiness_ProcToClose) {
				Show-InstallationWelcome -CheckDiskSpace -RequiredDiskSpace "$MYBusiness_FreeSpace" -CloseApps "$MYBusiness_ProcToClose" -AllowDeferCloseApps -DeferTimes 4 -PersistPrompt -TopMost $False -MinimizeWindows $false
			}
			if ($MYBusiness_ProcToCloseNonUI) {
				Show-InstallationWelcome -CheckDiskSpace -RequiredDiskSpace "$MYBusiness_FreeSpace" -CloseApps "$MYBusiness_ProcToCloseNonUI" -Silent
			}
			if ($MYBusiness_ProcToBlock) {
				Show-InstallationWelcome -CloseApps "$MYBusiness_ProcToBlock" -AllowDeferCloseApps -DeferTimes 4 -BlockExecution
			}
		}
		
		## Show Progress Message (With a Message to Indicate the Application is Being Uninstalled)
        Show-InstallationProgress -StatusMessage "Rimozione della versione precedente di SmartGIT in corso. Attendere prego..." -WindowLocation 'BottomRight'
		
	    ## Remove Any Existing Versions of SmartGIT
		Execute-Process -Path "C:\Program Files (x86)\SmartGit\unins000.exe" -PARAMETERS "/VERYSILENT" -Wait
				
		#*====================================PRE-INSTALLATION END==================================================================
#endregion PRE-INSTALLATION		

#region MAIN-INSTALLATION
		[string]$installPhase = 'Installation'
		#*====================================MAIN-INSTALLATION BEGIN==================================================================
        [String]$installPhase = 'Installation'

		} else {
		$AppRunning = $false
		}
		If(Test-Path -Path "C:\Program Files (x86)\SmartGit\unins000.exe")	{ 
            
		## Show Progress Message (With a Message to Indicate the Application is Being Uninstalled)
		Show-InstallationProgress -StatusMessage "Rimozione della versione precedente di SmartGIT in corso. Attendere prego..." -WindowLocation 'BottomRight'
		Execute-Process -Path "C:\Program Files (x86)\SmartGit\unins000.exe" -PARAMETERS "/VERYSILENT" -Wait	
		
		}
		if ($MYBusiness_UseDialogs){
			## Only use for longer installations (Installation duration approx. >3 minutes)
			Show-InstallationProgress -StatusMessage "Installazione di SmartGIT in corso. Attendere prego..."-WindowLocation 'BottomRight'
		}

        Write-Log -Message 'Start Installation SmartGIT' -Severity 2 -Source $deployAppScriptFriendlyName 
	   
	   $ReturnCode = Execute-Process -Path 'smartgit-23_1_4_2-setup.exe' -Parameters '/VERYSILENT /NORESTART /ALLUSERS' -WindowStyle 'Hidden' -passthru -IgnoreExitCodes '*'

		       switch ($ReturnCode.Exitcode)
       {		
	    "5"	{
		        Write-Log -Message "Execution completed with exit code [$($returnCode.ExitCode)]." -Severity 2 -Source $deployAppScriptFriendlyName
                Set-Reboot -MandatoryDeviceRestart
                $ProceedPostInstall = $false
            }

        "0" {
			    Write-Log -Message "Installation of SmartGIT is successfully with exit code [$($returnCode.ExitCode)]." -Severity 2 -Source $deployAppScriptFriendlyName
                $ProceedPostInstall = $True
            }

        "3010"  {
				    Write-Log -Message "Installation of SmartGIT is successfully with exit code [$($returnCode.ExitCode)]." -Severity 2 -Source $deployAppScriptFriendlyName
                    $ProceedPostInstall = $True
                }
            
        default {
		            Write-Log -Message "Execution failed with exit code [$($returnCode.ExitCode)]. Please check the log files in folder." -Severity 3 -Source $deployAppScriptFriendlyName
					Exit-Script -ExitCode 69001
                }
        }
		
		#*====================================MAIN-INSTALLATION END==================================================================
	#endregion MAIN-INSTALLATION
	
	#region POST-INSTALLATION
		[string]$installPhase = 'Post-Installation'
		#*====================================POST-INSTALLATION BEGIN==================================================================

        If($ProceedPostInstall)
        {  

		##Copy license file
		Copy-Item -path "$dirSupportfiles\license.lic" -Destination "C:\Program Files\SmartGit"
		
		##Merge certificate
		# Percorso del primo certificato .cer
		$cert1Path = "$dirSupportfiles\certificati\my.cer"

		# Percorso del secondo certificato .cer
		$cert2Path = "C:\Program Files\Git\mingw64\etc\ssl\certs\ca-bundle.crt"

		# Carica il contenuto dei certificati
		$cert1 = Get-Content $cert1Path
		$cert2 = Get-Content $cert2Path

		# Aggiungi il contenuto del primo certificato al secondo certificato
		$cert2 += $cert1

		# Salva il nuovo certificato
		$cert2 | Set-Content -Path $cert2Path -Encoding UTF8
		
		## Branding Install
		Set-Branding
	}

Maybe the uninstall is not complete when the installation starts.
I always add a small 5 second delay between uninstall and re-install, JIC.

Maybe add a test before the installation starts to check if the applications that must be closed ARE TRULY not running.