Uninstalling program via powershell custom action - Advanced Installer Community
nickcho
Posts: 1
Joined: Fri Sep 20, 2019 4:40 pm

Uninstalling program via powershell custom action

I am trying to run a powershell inline script as a custom action, but it does not execute during runtime. There are no errors in the log file. I've ensured that the script runs fine on its own. I've also tried putting the script as a ps1 file, adding it to the project and use the 'Run PowerShell script file' custom action.

Some of the troubleshooting that has been done:
-Toggling between Immediately and When the system is being modified (deferred) Execution Time
-Unchecked and checked Wait for custom action to finish before proceeding and Wait for return code at the end of the sequence
-Placing the custom action in various locations in Installation Sequence (Beginning of Install execution stage, after Remove Resources, after Finish Execution

The script uninstalls a windows program:

Code: Select all

$software = "program name"

$installed = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -like $software }) -ne $null

If($installed) {
	$app = Get-WmiObject -Class Win32_Product | Where-Object { 
        $_.Name -like $software
    }
  $app.Uninstall()
}
A snippet of the log file where the custom action executes:

Code: Select all

MSI (s) (18:44) [13:25:20:963]: Doing action: AI_DATA_SETTER_1
Action 13:25:20: AI_DATA_SETTER_1. 
Action start 13:25:20: AI_DATA_SETTER_1.
MSI (s) (18:44) [13:25:20:963]: PROPERTY CHANGE: Modifying CustomActionData property. Its current value is '***'. Its new value: 'Is64BitParamsScript#Uninstalls Java 8 if installed
$software = "Java 8 Update 201 (64-bit)"

$installed = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -like $software }) -ne $null

If($installed) {
	$app = Get-WmiObject -Class Win32_Product | Where-Object { 
        $_.Name -like $software
    }
  $app.Uninstall()
}
'.
Action ended 13:25:20: AI_DATA_SETTER_1. Return value 1.
MSI (s) (18:44) [13:25:20:963]: Doing action: RemoveJava
Action 13:25:20: RemoveJava. 
Action start 13:25:20: RemoveJava.
Action ended 13:25:20: RemoveJava. Return value 1.
Environment details:
Win 10
Advanced Installer 13
Powershell 5.1

Regards,
nic
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Uninstalling program via powershell custom action

Hello Nic and welcome to Advanced Installer forums,

First of all, please accept my apologies for the delayed reply.

One thing to keep in mind is that Windows Installer does not allow two installation processes to take place at the same time.

This is exactly why your script is not executing.

Basically, the installation process (your main process) will try and wait for the custom action to finish before proceeding. By doing this, the second process will not even be launched because the first one did not end yet (because the "Wait for custom action to finish before proceeding" option is checked in the "Custom Actions" page --> your custom action --> "Execution Options" section)

If you want to check this first hand, you can modify your script as it follows:

- after "Param()", add the following line:

Code: Select all

Add-Type -AssemblyName PresentationFramework
(this will allow us to use the MessageBox class)

beside that, after calling the Uninstall() method ("$app.Uninstall()"), add the following lines:

Code: Select all

$var = $app.Uninstall.returnvalue
[System.Windows.MessageBox]::Show($var)
By doing so, you will notice that the returned code will be "1618". By doing a little research, we can find out that this code means:

ERROR_INSTALL_ALREADY_RUNNING
Another installation is already in progress. Complete that installation before proceeding with this install.For information about the mutex, see _MSIExecute Mutex.


As discussed above, in order for this to work, we have to force the main process (the main installation process) to not wait for the CA. This can be done by disabling the following "Execution Options":

- Wait for custom action to finish before proceeding

and

- Fail installation if custom action returns an error

However, by placing the PowerShell Custom Action after the "Finish Execution" action group, you will notice that the "Wait for custom action to finish before proceeding" is disabled (the checkbox state cannot be altered).

The reason behind this can be found in the following Microsoft article:

Custom Action Return Processing Options

As can be seen in the above article:
An asynchronous execution that waits for exit code at the end of the sequence.
This option cannot be used with Concurrent Installations, Rollback Custom Actions, or Script Custom Actions.
"This option cannot be used with bla,bla,bla or Script Custom Actions"

This means that what you want to achieve is not possible with a script-based custom action.

However, this can still be achievable.

Basically, what we will need here is a .BAT file that will launch PowerShell and then run our script.

Important: One very important thing here is the fact that we need to launch the 64-bit PowerShell, otherwise the condition that is based on the registries will fail (due to registry redirection).

Here is how the .BAT file can look like:

Code: Select all

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -File "C:\Users\Catalin\Desktop\sample.ps1"
where sample.ps1 is the script that does the uninstall. Its content (for my tests) is:

Code: Select all

$software = "Java 8 Update 191 (64-bit)"

$installed = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -like $software }) -ne $null

if($installed){
$app = Get-WmiObject -Class Win32_Product | Where-Object {
$_.Name -like $software
}
$app.Uninstall()
}
Basically, here, you will just need to replace the "$software" variable to match your needs.

Now, in order to integrate this in Advanced Installer, you can proceed as it follows:

- add the .BAT file to your project (in the "Files and Folders" page)

- go to "Custom Actions" page

- add a "LaunchFile" custom action with sequence (by pressing the "Add custom action with sequence" button which is placed to the right side of the CA name)

- schedule it after the "Finish Execution" action group ("Install Execution Stage").

- the CA should look like this:
Annotation 2019-09-25 183035.png
Annotation 2019-09-25 183035.png (159.02 KiB) Viewed 4222 times
Simply replace the .BAT file name with the name you wish.

This way, your software should be correctly uninstalled.

Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”