-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(GH-739) Get-UninstallRegistryKey / update templates
Add Get-UninstallRegistryKey function to provide a means of getting uninstaller registry keys that does not fail with incorrectly encoded keys and values. Update the code documentation to link update the function with other similar functions and generate updated documentation Add the function to the uninstall template and nuspec with instructions to use the chocolatey-uninstall.extension package when supporting 0.9.9.x and below as the new function will only be available in 0.9.10 and above. The helper includes a trycatch for invalid registry keys. Using the catch is a requirement as blindly enumerating will fail every time if even one invalid key exists.
- Loading branch information
1 parent
9d5cf08
commit d607d65
Showing
14 changed files
with
284 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Get-UninstallRegistryKey | ||
|
||
Retrieve registry key(s) for system-installed applications from an | ||
exact or wildcard search. | ||
|
||
## Syntax | ||
|
||
~~~powershell | ||
Get-UninstallRegistryKey ` | ||
-SoftwareName <String> ` | ||
[-IgnoredArguments <Object[]>] [<CommonParameters>] | ||
~~~ | ||
|
||
## Description | ||
|
||
This function will attempt to retrieve a matching registry key for an | ||
already installed application, usually to be used with a | ||
chocolateyUninstall.ps1 automation script. | ||
|
||
The function also prevents `Get-ItemProperty` from failing when | ||
handling wrongly encoded registry keys. | ||
|
||
## Notes | ||
|
||
Available in 0.9.10+. If you need to maintain compatibility with pre | ||
0.9.10, please add the following to your nuspec: | ||
|
||
~~~xml | ||
<dependencies> | ||
<dependency id="chocolatey-uninstall.extension" /> | ||
</dependencies> | ||
~~~ | ||
|
||
## Aliases | ||
|
||
`Get-InstallRegistryKey` | ||
|
||
|
||
## Examples | ||
|
||
**EXAMPLE 1** | ||
|
||
~~~powershell | ||
# Software name in Programs and Features is "Gpg4Win (2.3.0)" | ||
[array]$key = Get-UninstallRegistryKey -SoftwareName "Gpg4win*" | ||
$key.DisplayName | ||
~~~ | ||
|
||
**EXAMPLE 2** | ||
|
||
~~~powershell | ||
# Software name is "Launchy 2.5" | ||
[array]$key = Get-UninstallRegistryKey -SoftwareName "Launchy*" | ||
$key.UninstallString | ||
~~~ | ||
|
||
**EXAMPLE 3** | ||
|
||
~~~powershell | ||
# Software name is "Mozilla Firefox" | ||
[array]$key = Get-UninstallRegistryKey -SoftwareName "Mozilla Firefox" | ||
$key.UninstallString | ||
~~~ | ||
|
||
## Inputs | ||
|
||
None | ||
|
||
## Outputs | ||
|
||
None | ||
|
||
## Parameters | ||
|
||
### -SoftwareName <String> | ||
Part or all of the Display Name as you see it in Programs and Features. | ||
It should be enough to be unique. | ||
|
||
If the display name contains a version number, such as "Launchy 2.5", | ||
it is recommended you use a fuzzy search "Launchy*" (the wildcard '*') | ||
as if the version is upgraded or autoupgraded, suddenly the uninstall | ||
script will stop working and it may not be clear as to what went wrong | ||
at first. | ||
|
||
Property | Value | ||
---------------------- | -------------- | ||
Aliases | | ||
Required? | true | ||
Position? | 1 | ||
Default Value | | ||
Accept Pipeline Input? | true (ByValue) | ||
|
||
### -IgnoredArguments [<Object[]>] | ||
Property | Value | ||
---------------------- | ----- | ||
Aliases | | ||
Required? | false | ||
Position? | named | ||
Default Value | | ||
Accept Pipeline Input? | false | ||
|
||
### <CommonParameters> | ||
|
||
This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see `about_CommonParameters` http://go.microsoft.com/fwlink/p/?LinkID=113216 . | ||
|
||
|
||
## Links | ||
|
||
* [[Install-ChocolateyPackage|HelpersInstallChocolateyPackage]] | ||
* [[Install-ChocolateyInstallPackage|HelpersInstallChocolateyInstallPackage]] | ||
* [[Uninstall-ChocolateyPackage|HelpersUninstallChocolateyPackage]] | ||
|
||
|
||
[[Function Reference|HelpersReference]] | ||
|
||
***NOTE:*** This documentation has been automatically generated from `Import-Module "$env:ChocolateyInstall\helpers\chocolateyInstaller.psm1" -Force; Get-Help Get-UninstallRegistryKey -Full`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/chocolatey.resources/helpers/functions/Get-UninstallRegistryKey.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# Copyright © 2011 - Present RealDimensions Software, LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
function Get-UninstallRegistryKey { | ||
<# | ||
.SYNOPSIS | ||
Retrieve registry key(s) for system-installed applications from an | ||
exact or wildcard search. | ||
.DESCRIPTION | ||
This function will attempt to retrieve a matching registry key for an | ||
already installed application, usually to be used with a | ||
chocolateyUninstall.ps1 automation script. | ||
The function also prevents `Get-ItemProperty` from failing when | ||
handling wrongly encoded registry keys. | ||
.NOTES | ||
Available in 0.9.10+. If you need to maintain compatibility with pre | ||
0.9.10, please add the following to your nuspec: | ||
~~~xml | ||
<dependencies> | ||
<dependency id="chocolatey-uninstall.extension" /> | ||
</dependencies> | ||
~~~ | ||
.INPUTS | ||
String | ||
.OUTPUTS | ||
This function searches registry objects and returns PSCustomObject of | ||
the matched key's properties. | ||
Retrieve properties with dot notation, for example: | ||
`$key.UninstallString` | ||
.PARAMETER SoftwareName | ||
Part or all of the Display Name as you see it in Programs and Features. | ||
It should be enough to be unique. | ||
If the display name contains a version number, such as "Launchy 2.5", | ||
it is recommended you use a fuzzy search "Launchy*" (the wildcard '*') | ||
as if the version is upgraded or autoupgraded, suddenly the uninstall | ||
script will stop working and it may not be clear as to what went wrong | ||
at first. | ||
.EXAMPLE | ||
> | ||
# Software name in Programs and Features is "Gpg4Win (2.3.0)" | ||
[array]$key = Get-UninstallRegistryKey -SoftwareName "Gpg4win*" | ||
$key.DisplayName | ||
.EXAMPLE | ||
> | ||
# Software name is "Launchy 2.5" | ||
[array]$key = Get-UninstallRegistryKey -SoftwareName "Launchy*" | ||
$key.UninstallString | ||
.EXAMPLE | ||
> | ||
# Software name is "Mozilla Firefox" | ||
[array]$key = Get-UninstallRegistryKey -SoftwareName "Mozilla Firefox" | ||
$key.UninstallString | ||
.LINK | ||
Install-ChocolateyPackage | ||
.LINK | ||
Install-ChocolateyInstallPackage | ||
.LINK | ||
Uninstall-ChocolateyPackage | ||
#> | ||
[CmdletBinding()] | ||
param( | ||
[parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)] | ||
[string] $softwareName, | ||
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments | ||
) | ||
Write-Debug "Running 'Get-UninstallRegistryKey' for `'$env:ChocolateyPackageName`' with SoftwareName:`'$softwareName`'"; | ||
|
||
if ($softwareName -eq $null -or $softwareName -eq '') { | ||
throw "$SoftwareName cannot be empty for Get-UninstallRegistryKey" | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dtgm
Author
Contributor
|
||
|
||
$ErrorActionPreference = 'Stop' | ||
$local_key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' | ||
$machine_key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*' | ||
$machine_key6432 = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' | ||
|
||
Write-Verbose "Retrieving all uninstall registry keys" | ||
[array]$keys = Get-ChildItem -Path @($machine_key6432, $machine_key, $local_key) ` | ||
-ErrorAction SilentlyContinue | ||
|
||
Write-Debug "Error handling check: Get-ItemProperty will fail if a registry key is written incorrectly." | ||
Write-Debug "If such a key is found, loop up to 10 times to try to bypass all badKeys" | ||
[int]$maxAttempts = 10 | ||
for ([int]$attempt = 1; $attempt -le $maxAttempts; $attempt++) { | ||
[bool]$success = $FALSE | ||
|
||
try { | ||
[array]$foundKey = Get-ItemProperty -Path $keys.PsPath ` | ||
-ErrorAction SilentlyContinue ` | ||
| Where-Object { $_.DisplayName -like $softwareName } | ||
$success = $TRUE | ||
} catch { | ||
Write-Debug "Found bad key." | ||
foreach ($key in $keys){ try { Get-ItemProperty $key.PsPath > $null } catch { $badKey = $key.PsPath }} | ||
Write-Verbose "Skipping bad key: $($key.PsPath)" | ||
[array]$keys = Get-ChildItem -Path @($machine_key6432, $machine_key, $local_key) ` | ||
-ErrorAction SilentlyContinue ` | ||
| Where-Object { $badKey -NotContains $_.PsPath } | ||
} | ||
|
||
if ($success) { break; } | ||
} | ||
|
||
return $foundKey | ||
} | ||
|
||
Set-Alias Get-InstallRegistryKey Get-UninstallRegistryKey |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ferventcoder I don't see the throw is giving a better
message than parameter validation would....what is the reason for replacing
[ValidateNotNullOrEmpty()]
with ^^^?