Skip to content

Commit 340825c

Browse files
authored
Fix Update-Module errors when Az modules are installed using Az.Tools.Installer (#22925)
* Fix Update-Module errors when Az modules are installed using Az.Tools.Installer #21262 Update `RepositorySourceLocation` and `Repository` in PSGetModuleInfo.xml * Add Test cases * Address review comments
1 parent 3eeef3e commit 340825c

File tree

8 files changed

+92
-18
lines changed

8 files changed

+92
-18
lines changed

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

Lines changed: 2 additions & 5 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.2'
15+
ModuleVersion = '0.2.3'
1616

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

111111
# ReleaseNotes of this module
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'
112+
ReleaseNotes = '* Fix Update-Module errors when Az modules are installed using Az.Tools.Installer [#21262]'
116113

117114
# Prerelease string of this module
118115
# Prerelease = ''

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,45 @@ function Get-RepositoryUrl {
423423
}
424424
}
425425

426+
function Update-ModuleInstallationRepository {
427+
param (
428+
[Parameter()]
429+
[ValidateNotNullOrEmpty()]
430+
[string]
431+
${ModuleName},
432+
433+
[Parameter()]
434+
[ValidateNotNullOrEmpty()]
435+
[Version]
436+
${InstalledVersion},
437+
438+
[Parameter()]
439+
[ValidateNotNullOrEmpty()]
440+
[string]
441+
${Repository}
442+
)
443+
444+
process {
445+
try {
446+
$moduleBase = (Microsoft.PowerShell.Core\Get-Module -ListAvailable -Name $ModuleName | Where-Object {$_.Version -eq $InstalledVersion}).ModuleBase
447+
$moduleInfoFile = Join-Path -Path $moduleBase -ChildPath 'PSGetModuleInfo.xml'
448+
Write-Debug "Checking file: $moduleInfoFile"
449+
$xmlText = Get-Content $moduleInfoFile
450+
$xmlText = $xmlText -Replace $script:AzTempRepoName, $Repository
451+
$repositoryUrl = Get-RepositoryUrl $Repository
452+
$destString = '"RepositorySourceLocation">' + $repositoryUrl + '<'
453+
$xmlText = $xmlText -Replace
454+
'"RepositorySourceLocation">.*<', $destString
455+
Write-Debug "Write new xml to $moduleInfoFile"
456+
Microsoft.PowerShell.Management\Set-Content -Path $moduleInfoFile -Value $xmlText -Force
457+
}
458+
catch {
459+
Write-Error $PSItem.ToString() -ErrorVariable +errorRecords
460+
throw $PSItem
461+
}
462+
}
463+
}
464+
426465
$exportedFunctions = @( Get-ChildItem -Path $PSScriptRoot/exports/*.ps1 -Recurse -ErrorAction 'SilentlyContinue' )
427466
$internalFunctions = @( Get-ChildItem -Path $PSScriptRoot/internal/*.ps1 -ErrorAction 'SilentlyContinue' )
428467

tools/Az.Tools.Installer/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
## Upcoming Release
2+
## 0.2.3
3+
* Fix Update-Module errors when Az modules are installed using Az.Tools.Installer [#21262]
24

35
## 0.2.2
46
* Updated the docs of parameter `Scope` [#16768]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function Update-AzModule {
140140
}
141141
$installModuleParams = @{
142142
ModuleList = $moduleList
143-
RepositoryUrl = (Get-RepositoryUrl $Repository)
143+
Repository = $Repository
144144
AllowPrerelease = $true
145145
Scope = if ($Scope) {$Scope} else {'CurrentUser'}
146146
RemovePrevious = !$KeepPrevious

tools/Az.Tools.Installer/internal/Install-AzModuleInternal.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function Install-AzModuleInternal {
2323
[Parameter(Mandatory)]
2424
[ValidateNotNullOrEmpty()]
2525
[string]
26-
${RepositoryUrl},
26+
${Repository},
2727

2828
[Parameter()]
2929
[Switch]
@@ -55,6 +55,7 @@ function Install-AzModuleInternal {
5555

5656
try {
5757
Write-Progress -Id $script:FixProgressBarId "Download packages from $Repository."
58+
$RepositoryUrl = Get-RepositoryUrl $Repository
5859

5960
if ($Force -or !$WhatIfPreference) {
6061
[string]$tempRepo = Join-Path ([Path]::GetTempPath()) ((New-Guid).Guid)
@@ -127,6 +128,7 @@ function Install-AzModuleInternal {
127128
PowerShellGet\Uninstall-Module -Name "Az.Accounts" -AllVersion -AllowPrerelease -ErrorAction 'SilentlyContinue'
128129
}
129130
PowerShellGet\Install-Module @installModuleParams -Name "Az.Accounts" -RequiredVersion "$($modules[0].Version)"
131+
Update-ModuleInstallationRepository -ModuleName "Az.Accounts" -InstalledVersion "$($modules[0].Version)" -Repository $Repository
130132
}
131133
$moduleInstalled += [PSCustomObject]@{
132134
Name = "Az.Accounts"
@@ -253,6 +255,7 @@ function Install-AzModuleInternal {
253255
Name = $result.ModuleName
254256
Version = ($result.ModuleVersion | Select-Object -First 1)
255257
}
258+
Update-ModuleInstallationRepository -ModuleName $result.ModuleName -InstalledVersion $result.ModuleVersion[0] -Repository $Repository
256259
}
257260
else {
258261
Write-Error "[$Invoker] Installing $($result.ModuleName) of version $($result.ModuleVersion) is failed. `n$($result.Error)"

tools/Az.Tools.Installer/internal/Install-AzModule_Default.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function Install-AzModule_Default {
111111
}
112112
$installModuleParams = @{
113113
ModuleList = $moduleList
114-
RepositoryUrl = (Get-RepositoryUrl $Repository)
114+
Repository = $Repository
115115
AllowPrerelease = $AllowPrerelease
116116
Scope = if ($Scope) {$Scope} else {'CurrentUser'}
117117
RemovePrevious = $RemovePrevious

tools/Az.Tools.Installer/test/Install-AzModule.Tests.ps1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Describe 'Install-AzModule' {
2727
$modules.Name | Should -Contain 'Az.Network'
2828
$modules.Name | Should -Contain 'Az.Compute'
2929
$modules.Name | Should -Contain 'Az.KeyVault'
30+
31+
$allmoduleInstalled = @('Az.Accounts', 'Az.Storage', 'Az.Network', 'Az.Compute', 'Az.KeyVault')
32+
(Get-InstalledModule -Name $allmoduleInstalled).Repository | Sort-Object -Unique | Should -Be 'PSGallery'
3033
}
3134

3235
It 'InstallByNamePrerelease' {
@@ -40,6 +43,9 @@ Describe 'Install-AzModule' {
4043
$modules.Name | Should -Contain 'Az.Maps'
4144
$modules.Name | Should -Contain 'Az.KeyVault'
4245

46+
$allmoduleInstalled = @('Az.Accounts', 'Az.Storage', 'Az.Network', 'Az.Maps', 'Az.KeyVault')
47+
(Get-InstalledModule -Name $allmoduleInstalled).Repository | Sort-Object -Unique | Should -Be 'PSGallery'
48+
4349
$output = Install-AzModule -Repository PSGallery -Name keyvault,resources,storage -RemovePrevious
4450
$output.Count | Should -Be 4
4551
$modules = Get-AzSubModule
@@ -50,6 +56,9 @@ Describe 'Install-AzModule' {
5056
$modules.Name | Should -Contain 'Az.Maps'
5157
$modules.Name | Should -Contain 'Az.KeyVault'
5258
$modules.Name | Should -Contain 'Az.Resources'
59+
60+
$allmoduleInstalled = @('Az.Accounts', 'Az.Storage', 'Az.Network', 'Az.Maps', 'Az.KeyVault', 'Az.Resources')
61+
(Get-InstalledModule -Name $allmoduleInstalled).Repository | Sort-Object -Unique | Should -Be 'PSGallery'
5362
}
5463

5564
It 'InstallByNameLatest' {
@@ -59,6 +68,9 @@ Describe 'Install-AzModule' {
5968
$modules.Count | Should -Be 2
6069
$modules.Name | Should -Contain 'Az.Accounts'
6170
$modules.Name | Should -Contain 'Az.Storage'
71+
72+
$allmoduleInstalled = @('Az.Accounts', 'Az.Storage')
73+
(Get-InstalledModule -Name $allmoduleInstalled).Repository | Sort-Object -Unique | Should -Be 'PSGallery'
6274
}
6375

6476
It 'InstallAllGA' {
@@ -69,6 +81,8 @@ Describe 'Install-AzModule' {
6981
$modules.Count | Should -Be $azModule.Dependencies.Count
7082
$expectedVersion = [Version] ($azModule.Dependencies | Where-Object {$_.Name -eq 'Az.Accounts'})['MinimumVersion']
7183
($modules | Where-Object {$_.Name -eq 'Az.Accounts'}).Version | Should -Be $expectedVersion
84+
85+
(Get-InstalledModule -Name 'Az.Accounts').Repository | Should -Be 'PSGallery'
7286
}
7387

7488
It 'InstallByUnexistingName' {
@@ -84,6 +98,8 @@ Describe 'Install-AzModule' {
8498
$output.Count | Should -Be 1
8599
(Get-AzSubModule).Name | Should -Be 'Az.Accounts'
86100
Get-InstalledModule -Name Azure* -ErrorAction 'Continue' | Should -Be $null
101+
102+
(Get-InstalledModule -Name 'Az.Accounts').Repository | Should -Be 'PSGallery'
87103
}
88104

89105
It 'InstallByUnexistingVersion' {
@@ -118,6 +134,9 @@ Describe 'Install-AzModule' {
118134
$modules.Name | Should -Contain 'Az.Accounts'
119135
$modules.Name | Should -Contain 'Az.Storage'
120136
$modules.Name | Should -Contain 'Az.Network'
137+
138+
$allmoduleInstalled = @('Az.Accounts', 'Az.Storage', 'Az.Network')
139+
(Get-InstalledModule -Name $allmoduleInstalled).Repository | Sort-Object -Unique | Should -Be 'PSGallery'
121140
}
122141
finally {
123142
foreach ($repo in $repos) {

tools/Az.Tools.Installer/test/Update-AzModule.Tests.ps1

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ Describe 'Update-AzModule' {
3131
$modules.Name | Should -Contain 'Az.Attestation'
3232
$modules.Name | Should -Contain 'Az.Compute'
3333
$modules.Name | Should -Contain 'Az.Resources'
34-
$modules.Name | Should -Contain 'Az.Network'
35-
$modules.Name | Should -Contain 'Az.Storage'
34+
$modules.Name | Should -Contain 'Az.Network'
35+
$modules.Name | Should -Contain 'Az.Storage'
36+
37+
$allmoduleInstalled = @('Az.Accounts', 'Az.Attestation', 'Az.Compute', 'Az.Resources', 'Az.Storage', 'Az.Network')
38+
(Get-InstalledModule -Name $allmoduleInstalled).Repository | Sort-Object -Unique | Should -Be 'PSGallery'
3639
}
3740

3841
It 'UpdateAllKeepPrevious' {
@@ -44,8 +47,11 @@ Describe 'Update-AzModule' {
4447
$modules.Name | Should -Contain 'Az.Attestation'
4548
$modules.Name | Should -Contain 'Az.Compute'
4649
$modules.Name | Should -Contain 'Az.Resources'
47-
$modules.Name | Should -Contain 'Az.Network'
48-
$modules.Name | Should -Contain 'Az.Storage'
50+
$modules.Name | Should -Contain 'Az.Network'
51+
$modules.Name | Should -Contain 'Az.Storage'
52+
53+
$allmoduleInstalled = @('Az.Accounts', 'Az.Attestation', 'Az.Compute', 'Az.Resources', 'Az.Storage', 'Az.Network')
54+
(Get-InstalledModule -Name $allmoduleInstalled).Repository | Sort-Object -Unique | Should -Be 'PSGallery'
4955
}
5056

5157
It 'UpdateAllReplacePrevious' {
@@ -56,12 +62,15 @@ Describe 'Update-AzModule' {
5662
$modules.Name | Should -Contain 'Az.Attestation'
5763
$modules.Name | Should -Contain 'Az.Compute'
5864
$modules.Name | Should -Contain 'Az.Resources'
59-
$modules.Name | Should -Contain 'Az.Network'
60-
$modules.Name | Should -Contain 'Az.Storage'
61-
65+
$modules.Name | Should -Contain 'Az.Network'
66+
$modules.Name | Should -Contain 'Az.Storage'
67+
6268
foreach ($module in $output) {
6369
(Find-Module -Name $module.Name -Repository PSGallery).Version | Should -Be $module.VersionUpdate
6470
}
71+
72+
$allmoduleInstalled = @('Az.Accounts', 'Az.Attestation', 'Az.Compute', 'Az.Resources', 'Az.Storage', 'Az.Network')
73+
(Get-InstalledModule -Name $allmoduleInstalled).Repository | Sort-Object -Unique | Should -Be 'PSGallery'
6574
}
6675

6776
It 'UpdateByUnexistingName' {
@@ -73,15 +82,20 @@ Describe 'Update-AzModule' {
7382
$modules.Name | Should -Contain 'Az.Attestation'
7483
$modules.Name | Should -Contain 'Az.Compute'
7584
$modules.Name | Should -Contain 'Az.Resources'
76-
$modules.Name | Should -Contain 'Az.Network'
85+
$modules.Name | Should -Contain 'Az.Network'
7786
$modules.Name | Should -Contain 'Az.Storage'
87+
88+
$allmoduleInstalled = @('Az.Accounts', 'Az.Attestation', 'Az.Compute', 'Az.Resources', 'Az.Storage', 'Az.Network')
89+
(Get-InstalledModule -Name $allmoduleInstalled).Repository | Sort-Object -Unique | Should -Be 'PSGallery'
7890
}
7991

8092
It 'UpdateWithoutAzAccounts' {
8193
$output = Update-AzModule -Name storage -Repository PSGallery -Scope 'CurrentUser'
8294
$output.Count | Should -Be 2
8395
$output = [Array] (Update-AzModule -Name compute -Repository PSGallery -Scope 'CurrentUser')
8496
$output.Count | Should -Be 1
97+
98+
(Get-InstalledModule -Name Az.Storage).Repository | Should -Be 'PSGallery'
8599
}
86100

87101
It 'UpdateWithoutRepository' {
@@ -92,6 +106,7 @@ Describe 'Update-AzModule' {
92106
try {
93107
$output = Update-AzModule -Name storage -Scope 'CurrentUser'
94108
$output.Count | Should -Be 2
109+
(Get-InstalledModule -Name Az.Storage).Repository | Should -Be 'PSGallery'
95110
}
96111
finally {
97112
foreach ($repo in $repos) {
@@ -108,7 +123,6 @@ Describe 'Update-AzModule' {
108123
}
109124

110125
AfterEach {
111-
Remove-AllAzModule
126+
Remove-AllAzModule
112127
}
113128
}
114-

0 commit comments

Comments
 (0)