@@ -62,87 +62,108 @@ function Uninstall-AzModule {
62
62
$Invoker = $MyInvocation.MyCommand
63
63
$ppsedition = $PSVersionTable.PSEdition
64
64
Write-Debug " Powershell $ppsedition Version $ ( $PSVersionTable.PSVersion ) "
65
+ $IsSuccess = $false
66
+ $errorRecords = @ ()
65
67
66
- if ($RemoveAzureRm -and ($Force -or $PSCmdlet.ShouldProcess (' Remove AzureRm modules' , ' AzureRm modules' , ' Remove' ))) {
67
- Write-Progress - Id $script :FixProgressBarId " Uninstall Azure and AzureRM."
68
- Remove-AzureRM
69
- }
68
+ try {
69
+ if ($RemoveAzureRm -and ($Force -or $PSCmdlet.ShouldProcess (' Remove AzureRm modules' , ' AzureRm modules' , ' Remove' ))) {
70
+ Write-Progress - Id $script :FixProgressBarId " Uninstall Azure and AzureRM."
71
+ Remove-AzureRM - ErrorVariable + errorRecords
72
+ }
70
73
71
- if ($Force -or $PSCmdlet.ShouldProcess (' Remove Az if installed' , ' Az' , ' Remove' )) {
72
- Uninstall-Module - Name ' Az' - AllVersion - ErrorAction SilentlyContinue
73
- }
74
+ if ($Force -or $PSCmdlet.ShouldProcess (' Remove Az if installed' , ' Az' , ' Remove' )) {
75
+ Uninstall-Module - Name ' Az' - AllVersion - ErrorAction SilentlyContinue - ErrorVariable + errorRecords
76
+ }
74
77
75
- Write-Progress - Id $script :FixProgressBarId " Check currently installed Az modules."
78
+ Write-Progress - Id $script :FixProgressBarId " Check currently installed Az modules."
76
79
77
- $allInstalled = @ ()
78
- $allInstalled += Get-AllAzModule - PrereleaseOnly:$PrereleaseOnly
80
+ $allInstalled = @ ()
81
+ $allInstalled += Get-AllAzModule - PrereleaseOnly:$PrereleaseOnly - ErrorVariable + errorRecords
79
82
80
- $moduleToUninstall = $allInstalled | Foreach-Object {[PSCustomObject ]@ {Name = $_.Name ; Version = $_.Version }}
81
- if ($Name ) {
82
- $Name = Normalize- ModuleName $Name
83
- $moduleToUninstall = $moduleToUninstall | Where-Object {$Name -Contains $_.Name }
84
- $modulesNotInstalled = $Name | Where-Object {! $allInstalled -or $allInstalled.Name -NotContains $_ }
85
- if ($modulesNotInstalled ) {
86
- Write-Warning " [$Invoker ] $modulesNotInstalled are not installed."
87
- }
88
- }
89
- else {
90
- if ($ExcludeModule ) {
91
- $ExcludeModule = Normalize- ModuleName $ExcludeModule
92
- $moduleToUninstall = $moduleToUninstall | Where-Object {$ExcludeModule -NotContains $_.Name }
93
- $modulesNotInstalled = $ExcludeModule | Where-Object {! $allInstalled -or $allInstalled.Name -NotContains $_ }
83
+ $moduleToUninstall = $allInstalled | Foreach-Object {[PSCustomObject ]@ {Name = $_.Name ; Version = $_.Version }}
84
+ if ($Name ) {
85
+ $Name = Normalize- ModuleName $Name
86
+ $moduleToUninstall = $moduleToUninstall | Where-Object {$Name -Contains $_.Name }
87
+ $modulesNotInstalled = $Name | Where-Object {! $allInstalled -or $allInstalled.Name -NotContains $_ }
94
88
if ($modulesNotInstalled ) {
95
- Throw " [$Invoker ] $modulesNotInstalled are not installed."
89
+ Write-Warning " [$Invoker ] $modulesNotInstalled are not installed."
90
+ }
91
+ }
92
+ else {
93
+ if ($ExcludeModule ) {
94
+ $ExcludeModule = Normalize- ModuleName $ExcludeModule - ErrorVariable + errorRecords
95
+ $moduleToUninstall = $moduleToUninstall | Where-Object {$ExcludeModule -NotContains $_.Name }
96
+ $modulesNotInstalled = $ExcludeModule | Where-Object {! $allInstalled -or $allInstalled.Name -NotContains $_ }
97
+ if ($modulesNotInstalled ) {
98
+ Throw " [$Invoker ] $modulesNotInstalled are not installed."
99
+ }
96
100
}
97
101
}
98
- }
99
102
100
- Write-Progress - Id $script :FixProgressBarId " Uninstall specified Az modules."
103
+ Write-Progress - Id $script :FixProgressBarId " Uninstall specified Az modules."
101
104
102
- if ($moduleToUninstall ) {
103
- $moduleNotToUninstall = $allInstalled.Name | Where-Object {$moduleToUninstall.Name -NotContains $_ }
104
- if ($null -ne $moduleNotToUninstall -and $null -ne $Name -and $Name -Contains ' Az.Accounts' ) {
105
- $moduleToUninstall = $moduleToUninstall | Where-Object { $_.Name -ne ' Az.Accounts' }
106
- Write-Warning " [$Invoker ] 'Az.Accounts cannot be uninstalled now for other modules are still dependent of it."
107
- }
105
+ if ($moduleToUninstall ) {
106
+ $moduleNotToUninstall = $allInstalled.Name | Where-Object {$moduleToUninstall.Name -NotContains $_ }
107
+ if ($null -ne $moduleNotToUninstall -and $null -ne $Name -and $Name -Contains ' Az.Accounts' ) {
108
+ $moduleToUninstall = $moduleToUninstall | Where-Object { $_.Name -ne ' Az.Accounts' }
109
+ Write-Warning " [$Invoker ] 'Az.Accounts cannot be uninstalled now for other modules are still dependent of it."
110
+ }
108
111
109
- $groupSet = @ {}
110
- $moduleToUninstall | Group-Object - Property Name | Foreach-Object {$groupSet [$_.Name ] = ($_.Group.Version | Sort-Object - Descending) }
111
-
112
- $started = Get-Date
113
- $moduleName = $null
114
- $index = 1
115
- foreach ($moduleName in $groupSet.Keys ) {
116
- $versions = $groupSet [$moduleName ]
117
- if ($Force -or $PSCmdlet.ShouldProcess (" Uninstalling module $moduleName version $versions " , " $moduleName version $versions " , " Uninstall" )) {
118
- Write-Progress - ParentId $script :FixProgressBarId - Activity " Uninstall Module" - Status " $moduleName version $versions " - PercentComplete ($index / $groupSet.Count * 100 )
119
- if ($PrereleaseOnly ) {
120
- $versionStrings = $versions | ForEach-Object {
121
- if ($_ -ge [Version ] " 1.0" ) {
122
- " $ ( $_ ) -preview"
112
+ $groupSet = @ {}
113
+ $moduleToUninstall | Group-Object - Property Name | Foreach-Object {$groupSet [$_.Name ] = ($_.Group.Version | Sort-Object - Descending) }
114
+
115
+ $started = Get-Date
116
+ $moduleName = $null
117
+ $index = 1
118
+ foreach ($moduleName in $groupSet.Keys ) {
119
+ $versions = $groupSet [$moduleName ]
120
+ if ($Force -or $PSCmdlet.ShouldProcess (" Uninstalling module $moduleName version $versions " , " $moduleName version $versions " , " Uninstall" )) {
121
+ Write-Progress - ParentId $script :FixProgressBarId - Activity " Uninstall Module" - Status " $moduleName version $versions " - PercentComplete ($index / $groupSet.Count * 100 )
122
+ if ($PrereleaseOnly ) {
123
+ $versionStrings = $versions | ForEach-Object {
124
+ if ($_ -ge [Version ] " 1.0" ) {
125
+ " $ ( $_ ) -preview"
126
+ }
127
+ else {
128
+ " $_ "
129
+ }
123
130
}
124
- else {
125
- " $_ "
131
+ foreach ( $versionString in $versionStrings ) {
132
+ PowerShellGet\Uninstall-Module - Name $moduleName - RequiredVersion $versionString - AllowPrerelease - ErrorAction ' Continue ' - ErrorVariable + errorRecords
126
133
}
127
134
}
128
- foreach ( $versionString in $versionStrings ) {
129
- PowerShellGet\Uninstall-Module - Name $moduleName - RequiredVersion $versionString - AllowPrerelease - ErrorAction ' Continue'
135
+ else {
136
+ PowerShellGet\Uninstall-Module - Name $moduleName - AllVersion - AllowPrerelease - ErrorAction ' Continue' - ErrorVariable + errorRecords
130
137
}
138
+ Write-Debug " [$Invoker ] Uninstalling $moduleName version $versions is completed."
139
+ $index += 1
131
140
}
132
- else {
133
- PowerShellGet\Uninstall-Module - Name $moduleName - AllVersion - AllowPrerelease - ErrorAction ' Continue'
134
- }
135
- Write-Debug " [$Invoker ] Uninstalling $moduleName version $versions is completed."
136
- $index += 1
137
141
}
142
+ $duration = (Get-Date ) - $started
143
+ Write-Debug " [$Invoker ] All uninstallation tasks are finished; Time Elapsed Total: $ ( $duration.TotalSeconds ) s."
144
+ }
145
+ $IsSuccess = $true
146
+ }
147
+ catch
148
+ {
149
+ Write-Error $PSItem.ToString () - ErrorVariable + errorRecords
150
+ throw $PSItem
151
+ }
152
+ finally {
153
+ if ($errorRecords.Count -gt 0 )
154
+ {
155
+ Send-PageViewTelemetry - SourcePSCmdlet $PSCmdlet `
156
+ - IsSuccess $false `
157
+ - StartDateTime $cmdStarted `
158
+ - Duration ((Get-Date ) - $cmdStarted )
159
+ }
160
+ else
161
+ {
162
+ Send-PageViewTelemetry - SourcePSCmdlet $PSCmdlet `
163
+ - IsSuccess $IsSuccess `
164
+ - StartDateTime $cmdStarted `
165
+ - Duration ((Get-Date ) - $cmdStarted )
138
166
}
139
- $duration = (Get-Date ) - $started
140
- Write-Debug " [$Invoker ] All uninstallation tasks are finished; Time Elapsed Total: $ ( $duration.TotalSeconds ) s."
141
167
}
142
-
143
- Send-PageViewTelemetry - SourcePSCmdlet $PSCmdlet `
144
- - IsSuccess $true `
145
- - StartDateTime $cmdStarted `
146
- - Duration ((Get-Date ) - $cmdStarted )
147
168
}
148
169
}
0 commit comments