Uninstall Applications With Inline PS Script Custom Action - Advanced Installer Community
Collins
Posts: 142
Joined: Wed Oct 12, 2016 2:57 pm

Uninstall Applications With Inline PS Script Custom Action

I would like to use the inline powershell custom action to remove applications that are not part of the installer. I have an exe that installs using feature based prerequisites and after the FinishExecution stage I run an inline script to uninstall some older applications using this:

# Block for declaring the script parameters.
Param($desktopPrereq, $ver, $uninst, $_)



#Uninstall My_Application
$desktopPrereq = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
Get-ItemProperty |
Where-Object {$_.DisplayName -match "My_Application" } |
Select-Object -Property DisplayName, UninstallString

ForEach ($ver in $desktopPrereq) {

If ($ver.UninstallString) {

$uninst = $ver.UninstallString
Start-Process cmd -ArgumentList "/c $uninst /quiet /norestart" -NoNewWindow -Wait
}

}

or this

# Block for declaring the script parameters.
Param($uninstall_base_version_1, $uninstall_base_version_2, $uninstall_base_version_3, $uninstall_base_version_4, $old_desktop_prereq, $old_scanner_prereq)

$uninstall_base_version_1="6.1"
$uninstall_base_version_2="6.2"
$uninstall_base_version_3="6.3"
$uninstall_base_version_4="6.4"

# Uninstall My_Application
$old_desktop_prereq=Get-WmiObject -Class Win32_Product | sort-object Name | where { $_.Name -match "My_Application" -and ($_.Version -like "$($uninstall_base_version_1)*" -or $_.Version -like "$($uninstall_base_version_2)*" -or $_.Version -like "$($uninstall_base_version_3)*" -or $_.Version -like "$($uninstall_base_version_4)*")}
if ($old_desktop_prereq) {
$old_desktop_prereq.Uninstall()
}

# Uninstall My_Application
$old_scanner_prereq=Get-WmiObject -Class Win32_Product | sort-object Name | where { $_.Name -match "My_Application" -and ($_.Version -like "$($uninstall_base_version_1)*" -or $_.Version -like "$($uninstall_base_version_2)*" -or $_.Version -like "$($uninstall_base_version_3)*" -or $_.Version -like "$($uninstall_base_version_4)*")}
if ($old_scanner_prereq) {
$old_scanner_prereq.Uninstall()
}

When I run it from PS, it works, but when I run it as an inline script custom action, it doesn't work. I have tried it at different execution times and execution stages with no success.
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Uninstall Applications With Inline PS Script Custom Action

Hello,

Can you try to uncheck the "Wait for custom action to finish before proceeding" and "Wait for return code at the end of the sequence" execution options of your custom action and see if this helps.

If the setup packages of the applications you try to remove through your custom action are also Windows Installer setups, then you won't be able to uninstall them from the "Install Execution Stage" of your current setup. This is because Windows Installer service cannot handle two installation/uninstall processes at the same time. The only workaround in this case will be to add your PowerShell custom action as the last action in list after "Finish Execution" action group and configure it to run asynchronously by unchecking the "Wait for custom action to finish before proceeding" and "Wait for return code at the end of the sequence" execution options.

If this still doesn't help you should try to troubleshoot your custom action code when running during the installation.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Collins
Posts: 142
Joined: Wed Oct 12, 2016 2:57 pm

Re: Uninstall Applications With Inline PS Script Custom Action

Thank you Daniel, but that didn't work.

I moved away from the PS and created some extended searches to look for Registry key exists to find the Product GUID. Then I used the built in Launch EXE With Working Directory custom action to run the msiexec.exe, conditioned them to the searches and on the command line used the instructions from the user guide:
"set the "Command line" field to /x GUID /qb, where GUID is the Product Code of the package you want to uninstall", but I was getting "this installation package could not be opened. verify that the package exists and that you can access it" when the custom action would run.

I discovered that the Command line field (/x GUID /qb) should be (/x {GUID} /qb).

Now it gets passed that but, as I have three running consecutively after each other, they intermittently collide because the "Wait for custom action to finish before proceeding" is unchecked. Is there a way to get around this or create a wait or delay?
Daniel
Posts: 8283
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Uninstall Applications With Inline PS Script Custom Action

Hello,

I'm afraid there is no way to add such a delay between the custom actions execution. The only workaround will be to develop a single custom action and run from its code the uninstall commands subsequently. Maybe you can try to write your custom action in another programming language (e.g. C#).

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”