Skip to content

Commit 3310f6e

Browse files
authored
Merge pull request #4226 from viananth/vishnu_patch
Fix bug for importing modules
2 parents b622eac + 9307f36 commit 3310f6e

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,20 @@ Describe "Use-AzureRmProfile" {
11561156
$useError.exception.message.contains("A different profile version of module") | Should Be $true
11571157
}
11581158
}
1159+
1160+
# User has module2 from profile1 imported; tries to execute Use-AzureRmProfile for profile1 with module1. Should import.
1161+
Context "A different module from same profile was previously imported" {
1162+
Mock Get-AzureRmModule -Verifiable { "1.0" }
1163+
$VersionObj = New-Object -TypeName System.Version -ArgumentList "1.0"
1164+
$moduleObj = New-Object -TypeName PSObject
1165+
$moduleObj | Add-Member NoteProperty -Name "Name" -Value "Module2"
1166+
$moduleObj | Add-Member NoteProperty Version($VersionObj)
1167+
Mock Get-Module -Verifiable { $moduleObj }
1168+
It "Should import module" {
1169+
$result = Use-AzureRmProfile -Profile 'Profile1' -Module 'Module1'
1170+
Assert-MockCalled Import-Module -Times 1
1171+
}
1172+
}
11591173
}
11601174
}
11611175

tools/AzureRM.BootStrapper/AzureRM.Bootstrapper.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,12 +1043,12 @@ function Use-AzureRmProfile
10431043
$importedModules = Get-Module "Azure*"
10441044
foreach ($importedModule in $importedModules)
10451045
{
1046-
$versions = $ProfileMap.$Profile.$($importedModule.Name)
1047-
if ($null -ne $versions)
1046+
$importedVersions = $ProfileMap.$Profile.$($importedModule.Name)
1047+
if ($null -ne $importedVersions)
10481048
{
10491049
# We need the latest version in that profile to be imported. If old version was imported, block user and ask to import in a new session
1050-
$version = Get-LatestModuleVersion -versions $versions
1051-
if ([system.version]$version -ne $importedModule.Version)
1050+
$importedVersion = Get-LatestModuleVersion -versions $importedVersions
1051+
if ([system.version]$importedVersion -ne $importedModule.Version)
10521052
{
10531053
Write-Error "A different profile version of module $importedModule is imported in this session. Start a new PowerShell session and retry the operation." -Category InvalidOperation
10541054
return

0 commit comments

Comments
 (0)