Skip to content

Commit 1a4af36

Browse files
authored
Fix the Az.Tools.Installer Telemetry SuccessRate Error (#22814)
* Fix the Az.Tools.Installer Telemetry SuccessRate Error * Fix the doc errors and update the version to 0.2.2 * Address the review comments
1 parent 49a512d commit 1a4af36

13 files changed

+274
-201
lines changed

tools/Az.Tools.Installer/Az.Tools.Installer.psd1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'Az.Tools.Installer.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.2.1'
15+
ModuleVersion = '0.2.2'
1616

1717
# Supported PSEditions
1818
CompatiblePSEditions = 'Core', 'Desktop'
@@ -109,9 +109,10 @@
109109
# IconUri = ''
110110

111111
# ReleaseNotes of this module
112-
ReleaseNotes = '* Fixed the issue of installation and updating when Repository is not provided (#20209)
113-
* Updated telemetry assembly to 2.13.1
114-
* Fixed typo in Az.Tools.Installer (#20055)'
112+
ReleaseNotes = '* Updated the docs of parameter `Scope` [#16768]
113+
* Removed the usage of `Get-InstalledModule` from product codes
114+
* Fix the issue of uninstalling the AzureRM modules
115+
* Collected the telemetry data when the cmdlets fail'
115116

116117
# Prerelease string of this module
117118
# Prerelease = ''

tools/Az.Tools.Installer/Az.Tools.Installer.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ class ModuleInfo
399399
function Remove-AzureRM {
400400
process {
401401
try {
402-
$azureModuleNames = (Microsoft.PowerShell.Core\Get-Module -ListAvailable -Name Azure* -ErrorAction Stop).Name | Where-Object {$_ -match "Azure(\.[a-zA-Z0-9]+)?" -or $_ -match "AzureRM(\.[a-zA-Z0-9]+)?"}
402+
$azureModuleNames = (Microsoft.PowerShell.Core\Get-Module -ListAvailable -Name Azure* -ErrorAction Stop).Name | Where-Object {$_ -match "(Azure|AzureRM)(\.[a-zA-Z0-9]+)*$"}
403403
foreach ($moduleName in $azureModuleNames) {
404404
PowerShellGet\Uninstall-Module -Name $moduleName -AllVersion -AllowPrerelease -ErrorAction Continue
405405
}

tools/Az.Tools.Installer/ChangeLog.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## Upcoming Release
2-
* Remove the usage of `Get-InstalledModule` from product codes.
2+
3+
## 0.2.2
4+
* Updated the docs of parameter `Scope` [#16768]
5+
* Removed the usage of `Get-InstalledModule` from product codes
6+
* Fix the issue of uninstalling the AzureRM modules
7+
* Collected the telemetry data when the cmdlets fail
8+
39
## 0.2.1
410
* Fixed the issue of installation and updating when Repository is not provided (#20209)
511
* Updated telemetry assembly to 2.13.1

tools/Az.Tools.Installer/exports/Install-AzModule.ps1

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function Install-AzModule {
5959
[string]
6060
${Path},
6161

62-
[Parameter(HelpMessage = 'Scope to install modules. Accepted values: CurrentUser, AllUser.')]
62+
[Parameter(HelpMessage = 'Scope to install modules. Default value is `CurrentUser` for all the Powershell platforms. Accepted values: CurrentUser, AllUser.')]
6363
[ValidateSet('CurrentUser', 'AllUsers')]
6464
[string]
6565
${Scope},
@@ -82,18 +82,39 @@ function Install-AzModule {
8282
$Invoker = $MyInvocation.MyCommand
8383
$ppsedition = $PSVersionTable.PSEdition
8484
Write-Debug "Powershell $ppsedition Version $($PSVersionTable.PSVersion)"
85-
86-
$Invoker = $MyInvocation.MyCommand
87-
if ($PSCmdlet.ParameterSetName -eq 'Default') {
88-
Install-AzModule_Default @PSBoundParameters -Invoker $Invoker
85+
$IsSuccess = $false
86+
$errorRecords = @()
87+
88+
try {
89+
$Invoker = $MyInvocation.MyCommand
90+
if ($PSCmdlet.ParameterSetName -eq 'Default') {
91+
Install-AzModule_Default @PSBoundParameters -Invoker $Invoker -ErrorVariable +errorRecords
92+
}
93+
else {
94+
Install-AzModule_ByPath @PSBoundParameters -Invoker $Invoker -ErrorVariable +errorRecords
95+
}
96+
$IsSuccess = $true
8997
}
90-
else {
91-
Install-AzModule_ByPath @PSBoundParameters -Invoker $Invoker
98+
catch
99+
{
100+
Write-Error $PSItem.ToString() -ErrorVariable +errorRecords
101+
throw $PSItem
102+
}
103+
finally {
104+
if ($errorRecords.Count -gt 0)
105+
{
106+
Send-PageViewTelemetry -SourcePSCmdlet $PSCmdlet `
107+
-IsSuccess $false `
108+
-StartDateTime $cmdStarted `
109+
-Duration ((Get-Date) - $cmdStarted)
110+
}
111+
else
112+
{
113+
Send-PageViewTelemetry -SourcePSCmdlet $PSCmdlet `
114+
-IsSuccess $IsSuccess `
115+
-StartDateTime $cmdStarted `
116+
-Duration ((Get-Date) - $cmdStarted)
117+
}
92118
}
93-
94-
Send-PageViewTelemetry -SourcePSCmdlet $PSCmdlet `
95-
-IsSuccess $true `
96-
-StartDateTime $cmdStarted `
97-
-Duration ((Get-Date) - $cmdStarted)
98119
}
99120
}

tools/Az.Tools.Installer/exports/Uninstall-AzModule.ps1

Lines changed: 84 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -62,87 +62,108 @@ function Uninstall-AzModule {
6262
$Invoker = $MyInvocation.MyCommand
6363
$ppsedition = $PSVersionTable.PSEdition
6464
Write-Debug "Powershell $ppsedition Version $($PSVersionTable.PSVersion)"
65+
$IsSuccess = $false
66+
$errorRecords = @()
6567

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+
}
7073

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+
}
7477

75-
Write-Progress -Id $script:FixProgressBarId "Check currently installed Az modules."
78+
Write-Progress -Id $script:FixProgressBarId "Check currently installed Az modules."
7679

77-
$allInstalled = @()
78-
$allInstalled += Get-AllAzModule -PrereleaseOnly:$PrereleaseOnly
80+
$allInstalled = @()
81+
$allInstalled += Get-AllAzModule -PrereleaseOnly:$PrereleaseOnly -ErrorVariable +errorRecords
7982

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 $_}
9488
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+
}
96100
}
97101
}
98-
}
99102

100-
Write-Progress -Id $script:FixProgressBarId "Uninstall specified Az modules."
103+
Write-Progress -Id $script:FixProgressBarId "Uninstall specified Az modules."
101104

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+
}
108111

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+
}
123130
}
124-
else {
125-
"$_"
131+
foreach ($versionString in $versionStrings) {
132+
PowerShellGet\Uninstall-Module -Name $moduleName -RequiredVersion $versionString -AllowPrerelease -ErrorAction 'Continue' -ErrorVariable +errorRecords
126133
}
127134
}
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
130137
}
138+
Write-Debug "[$Invoker] Uninstalling $moduleName version $versions is completed."
139+
$index += 1
131140
}
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
137141
}
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)
138166
}
139-
$duration = (Get-Date) - $started
140-
Write-Debug "[$Invoker] All uninstallation tasks are finished; Time Elapsed Total: $($duration.TotalSeconds)s."
141167
}
142-
143-
Send-PageViewTelemetry -SourcePSCmdlet $PSCmdlet `
144-
-IsSuccess $true `
145-
-StartDateTime $cmdStarted `
146-
-Duration ((Get-Date) - $cmdStarted)
147168
}
148169
}

0 commit comments

Comments
 (0)