Add Package Remove Command
You can remove installed packages with the PowerShel cmdlet Uninstall-UscPackage
, but that will only remove or clean up the the installation if the package implements the remove command and that is what we are going to implement in this how-to.
In this how-to we will create removal scripts for both of our packages from prior how-tos example-app and app-dependency.
First, lets implement the remove command for app-dependency package, edit the CreatePackages.ps1 file and add remove command definition to it's manifest:
... $AppDependency = @{ 'Id' = 'app-dependency' 'Name' = 'App Dependency' 'Version' = '1.0' 'Commands' = @{ 'Install' = 'Package.psm1:Install-Package' 'Update' = 'Package.psm1:Update-Package' 'Remove' = 'Package.psm1:Remove-Package' } 'InputPath' = @( Join-Path $PSScriptRoot 'app-dependency\*' # Add all files to package from directory. ) 'OutputDir' = (Join-Path $PSScriptRoot 'Package') } ...
This will tell Update Service to execute the
Remove-Package
function in the Package.psm1 module, if the package is removed.Next, edit the app-dependency\Package.psm1 and add the following function:
function Remove-Package($Context) { AddContent $Context "Removing" }
Instead of removing anything, we will add a new line to our log file at "c:\temp\app-dependency.txt".
Lets do similar for the example-app package, edit the CreatePackages.ps1 and add the remove command to it's manifest:
... $ExampleApp = @{ 'Id' = 'example-app' 'Name' = 'Example App' 'Version' = '2.0' 'Commands' = @{ 'Install' = 'Package.psm1:Install-Package' 'Update' = 'Package.psm1:Install-Package' 'Remove' = 'Package.psm1:Remove-Package' } 'Dependencies' = @( @{ Id = 'app-dependency'; Version = $AppDependency.Version } ) 'InputPath' = @( Join-Path $PSScriptRoot 'example-app\*' # Add all files to package from directory. ) 'OutputDir' = (Join-Path $PSScriptRoot 'Package') } ...
And then add the following functions to example-app\Package.psm1:
... function RemoveByProductCode($ProductCode) { $RegistryPath = Join-Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall' "$ProductCode*" $Item = Get-ItemProperty -Path $RegistryPath if ($Item -eq $null) { $RegistryPath = Join-Path 'HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall' "$ProductCode*" $Item = Get-ItemProperty -Path $RegistryPath } if (($Item -ne $null) -and ($Item.QuietUninstallString -ne $null)) { Invoke-Expression "& $($Item.UninstallString) /VERYSILENT" } } function Remove-Package { RemoveByProductCode '{B17466BF-D913-443A-8AF2-46C9E4AA0224}' } ...
Without going into too much details, the
RemoveByProductCode
function basically checks the Windows registry if Example App.exe is installed on the system, and if it does, runs it's uninstall silently (removing it from Windows). The GUID passed toRemoveByProductCode
inRemove-Package
is the AppId for Example App.exe.Run CreatePackages.ps1 to re-create the packages.
Now we'd like to test and see if it's working. If you didn't notice, we did not bump up the version number for our packages, they are still at v2.0 for example-app and v1.0 for app-dependency and since it's likely you have already installed the packages on your machine, we need to re-install them to include the new changes. Lets start by removing both packages via PowerShell
'app-dependency', 'example-app' | Uninstall-UscPackage
or through the Update Service client (found in the Windows Start Menu):
Since these packages didn't implement the remove command at the time of install, this will only remove the packages from Update Service installed registry.
Then, install the *example-package again with the new changes, either with the installer or through PowerShell:
Install-UscPackage -Id 'example-package'
Before removing the packages the second time, go to Windows Add or remove programs and locate the Example app entry.
Again, like in previous step, remove the two packages.
- This should execute the
Remove-Package
functions we added to the packages on removal, so you can go to Windows Add or remove programs again and see that the Example app is not there any more and check the log file at c:\temp\app-dependency.txt for a new removal entry.
- This should execute the