Skip to content

Commit f261146

Browse files
author
Stephen Tramer
committed
Update uninstall script to be more correct
1 parent 5c73c59 commit f261146

File tree

4 files changed

+65
-18
lines changed

4 files changed

+65
-18
lines changed

docs-conceptual/azps-1.8.0/uninstall-az-ps.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,20 @@ function Uninstall-AllModules {
6161
'Creating list of dependencies...'
6262
$target = Find-Module $TargetModule -RequiredVersion $version
6363
$target.Dependencies | ForEach-Object {
64-
if ($_.requiredVersion) {
64+
if ($_.PSObject.Properties.Name -contains 'requiredVersion') {
6565
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$_.requiredVersion}
6666
}
6767
else { # Assume minimum version
6868
# Minimum version actually reports the installed dependency
6969
# which is used, not the actual "minimum dependency." Check to
7070
# see if the requested version was installed as a dependency earlier.
71-
$candidate = Get-InstalledModule $_.name -RequiredVersion $version
71+
$candidate = Get-InstalledModule $_.name -RequiredVersion $version -ErrorAction Ignore
7272
if ($candidate) {
7373
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$version}
7474
}
7575
else {
76-
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall" -f $_.name,$version)
76+
$availableModules = Get-InstalledModule $_.name -AllVersions
77+
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall. Available versions are: {2}" -f $_.name,$version,($availableModules.Version -join ', '))
7778
}
7879
}
7980
}
@@ -109,12 +110,17 @@ Uninstalling Az.AnalysisServices version 0.7.0
109110
...
110111
```
111112

113+
> [!NOTE]
114+
> If this script can't match an exact dependent module version to uninstall, it won't uninstall _any_ version of that module. This is because there may be other versions
115+
> of `Az` on your system which rely on these modules. In this case, the versions of the module that couldn't be found will be listed, if any were installed. You can
116+
> then remove any old versions manually with `Uninstall-Module`.
117+
112118
Run this command for every version of Azure PowerShell that you want to uninstall. For convenience, the following
113119
script will uninstall all versions of Az __except__ for the latest.
114120

115121
```powershell-interactive
116122
$versions = (Get-InstalledModule Az -AllVersions | Select-Object Version)
117-
$versions[1..($versions.Length-1)] | foreach { Uninstall-AllModules -TargetModule Az -Version ($_.Version) -Force }
123+
$versions[0..($versions.Length-2)] | foreach { Uninstall-AllModules -TargetModule Az -Version ($_.Version) -Force }
118124
```
119125

120126
## Uninstall the AzureRM module

docs-conceptual/azps-2.2.0/uninstall-az-ps.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Uninstall Azure PowerShell
33
description: How to perform a complete uninstall of Azure PowerShell
4-
ms.date: 05/10/2019
4+
ms.date: 06/10/2019
55
author: sptramer
66
ms.author: sttramer
77
ms.manager: carmonm
@@ -61,19 +61,20 @@ function Uninstall-AllModules {
6161
'Creating list of dependencies...'
6262
$target = Find-Module $TargetModule -RequiredVersion $version
6363
$target.Dependencies | ForEach-Object {
64-
if ($_.requiredVersion) {
64+
if ($_.PSObject.Properties.Name -contains 'requiredVersion') {
6565
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$_.requiredVersion}
6666
}
6767
else { # Assume minimum version
6868
# Minimum version actually reports the installed dependency
6969
# which is used, not the actual "minimum dependency." Check to
7070
# see if the requested version was installed as a dependency earlier.
71-
$candidate = Get-InstalledModule $_.name -RequiredVersion $version
71+
$candidate = Get-InstalledModule $_.name -RequiredVersion $version -ErrorAction Ignore
7272
if ($candidate) {
7373
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$version}
7474
}
7575
else {
76-
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall" -f $_.name,$version)
76+
$availableModules = Get-InstalledModule $_.name -AllVersions
77+
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall. Available versions are: {2}" -f $_.name,$version,($availableModules.Version -join ', '))
7778
}
7879
}
7980
}
@@ -109,12 +110,17 @@ Uninstalling Az.AnalysisServices version 0.7.0
109110
...
110111
```
111112

113+
> [!NOTE]
114+
> If this script can't match an exact dependency with the same version to uninstall, it won't uninstall _any_ version of that dependecy. This is because there may be
115+
> other versions of the target module on your system which rely on these dependencies. In this case, the available versions of the dependency are listed.
116+
> You can then remove any old versions manually with `Uninstall-Module`.
117+
112118
Run this command for every version of Azure PowerShell that you want to uninstall. For convenience, the following
113119
script will uninstall all versions of Az __except__ for the latest.
114120

115121
```powershell-interactive
116122
$versions = (Get-InstalledModule Az -AllVersions | Select-Object Version)
117-
$versions[1..($versions.Length-1)] | foreach { Uninstall-AllModules -TargetModule Az -Version ($_.Version) -Force }
123+
$versions[0..($versions.Length-2)] | foreach { Uninstall-AllModules -TargetModule Az -Version ($_.Version) -Force }
118124
```
119125

120126
## Uninstall the AzureRM module

docs-conceptual/azurermps-5.7.0/uninstall-azurerm-ps.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Uninstall Azure PowerShell
33
description: How to perform a complete uninstall of Azure PowerShell
4-
ms.date: 06/20/2018
4+
ms.date: 06/10/2019
55
author: sptramer
66
ms.author: sttramer
77
ms.manager: carmonm
@@ -50,22 +50,38 @@ function Uninstall-AllModules {
5050
[Parameter(Mandatory=$true)]
5151
[string]$Version,
5252
53-
[switch]$Force
53+
[switch]$Force,
54+
55+
[switch]$WhatIf
5456
)
5557
5658
$AllModules = @()
5759
5860
'Creating list of dependencies...'
5961
$target = Find-Module $TargetModule -RequiredVersion $version
6062
$target.Dependencies | ForEach-Object {
61-
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$_.requiredversion}
63+
if ($_.requiredVersion) {
64+
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$_.requiredVersion}
65+
}
66+
else { # Assume minimum version
67+
# Minimum version actually reports the installed dependency
68+
# which is used, not the actual "minimum dependency." Check to
69+
# see if the requested version was installed as a dependency earlier.
70+
$candidate = Get-InstalledModule $_.name -RequiredVersion $version
71+
if ($candidate) {
72+
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$version}
73+
}
74+
else {
75+
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall" -f $_.name,$version)
76+
}
77+
}
6278
}
6379
$AllModules += New-Object -TypeName psobject -Property @{name=$TargetModule; version=$Version}
6480
6581
foreach ($module in $AllModules) {
66-
Write-Host ('Uninstalling {0} version {1}' -f $module.name,$module.version)
82+
Write-Host ('Uninstalling {0} version {1}...' -f $module.name,$module.version)
6783
try {
68-
Uninstall-Module -Name $module.name -RequiredVersion $module.version -Force:$Force -ErrorAction Stop
84+
Uninstall-Module -Name $module.name -RequiredVersion $module.version -Force:$Force -ErrorAction Stop -WhatIf:$WhatIf
6985
} catch {
7086
Write-Host ("`t" + $_.Exception.Message)
7187
}
@@ -81,7 +97,8 @@ Uninstall-AllModules -TargetModule AzureRM -Version 4.4.1 -Force
8197
```
8298

8399
As the script runs, it will display the name and version of each submodule that is being
84-
uninstalled.
100+
uninstalled. To run the script to only see what would be deleted, without removing it,
101+
use the `-WhatIf` option.
85102

86103
```output
87104
Creating list of dependencies...
@@ -92,4 +109,16 @@ Uninstalling Azure.AnalysisServices version 0.4.7
92109
...
93110
```
94111

95-
Run this command for every version of Azure PowerShell that you want to uninstall.
112+
> [!NOTE]
113+
> If this script can't match an exact dependency with the same version to uninstall, it won't uninstall _any_ version of that dependecy. This is because there may be
114+
> other versions of the target module on your system which rely on these dependencies. In this case, the available versions of the dependency are listed.
115+
> You can then remove any old versions manually with `Uninstall-Module`.
116+
117+
118+
Run this command for every version of Azure PowerShell that you want to uninstall. For convenience, the following
119+
script will uninstall all versions of AzureRM __except__ for the latest.
120+
121+
```powershell-interactive
122+
$versions = (get-installedmodule AzureRM -AllVersions | Select-Object Version)
123+
$versions[0..($versions.Length-2)] | foreach { Uninstall-AllModules -TargetModule AzureRM -Version ($_.Version) -Force }
124+
```

docs-conceptual/azurermps-6.13.0/uninstall-azurerm-ps.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Uninstall Azure PowerShell
33
description: How to perform a complete uninstall of Azure PowerShell
4-
ms.date: 11/30/2018
4+
ms.date: 06/10/2019
55
author: sptramer
66
ms.author: sttramer
77
ms.manager: carmonm
@@ -123,10 +123,16 @@ Uninstalling Azure.AnalysisServices version 0.4.7
123123
...
124124
```
125125

126+
> [!NOTE]
127+
> If this script can't match an exact dependency with the same version to uninstall, it won't uninstall _any_ version of that dependecy. This is because there may be
128+
> other versions of the target module on your system which rely on these dependencies. In this case, the available versions of the dependency are listed.
129+
> You can then remove any old versions manually with `Uninstall-Module`.
130+
131+
126132
Run this command for every version of Azure PowerShell that you want to uninstall. For convenience, the following
127133
script will uninstall all versions of AzureRM __except__ for the latest.
128134

129135
```powershell-interactive
130136
$versions = (get-installedmodule AzureRM -AllVersions | Select-Object Version)
131-
$versions[1..($versions.Length-1)] | foreach { Uninstall-AllModules -TargetModule AzureRM -Version ($_.Version) -Force }
137+
$versions[0..($versions.Length-2)] | foreach { Uninstall-AllModules -TargetModule AzureRM -Version ($_.Version) -Force }
132138
```

0 commit comments

Comments
 (0)