Skip to content

Commit a5cb84c

Browse files
Bryant Richardsonknithinc
authored andcommitted
Workaround for AzurePowerShell regression when populating account properties (#469)
* Add workaround for AzurePowerShell profile module regression (failure to populate account properties) * Fix refresh token transformation * Update error message with instructions to clear cross-process auth cache
1 parent 18e52fb commit a5cb84c

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

Identity/AzureStack.Identity.psm1

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,17 @@ function Register-AzsWithMyDirectoryTenant {
437437
Select -Last 1 -ExpandProperty RefreshToken |
438438
ConvertTo-SecureString -AsPlainText -Force
439439

440+
# Workaround due to regression in AzurePowerShell profile module which fails to populate the response object of "Add-AzureRmAccount" cmdlet
441+
if (-not $refreshToken) {
442+
if ($tokens.Count -eq 1) {
443+
Write-Warning "Failed to find target refresh token from Azure PowerShell Cache; attempting to reuse the single cached auth context..."
444+
$refreshToken = $tokens[0].RefreshToken | ConvertTo-SecureString -AsPlainText -Force
445+
}
446+
else {
447+
throw "Unable to find refresh token from Azure PowerShell Cache. Please try the command again in a fresh PowerShell instance after running 'Clear-AzureRmContext -Scope CurrentUser -Force -Verbose'."
448+
}
449+
}
450+
440451
return $refreshToken
441452
}
442453

@@ -802,7 +813,18 @@ function Unregister-AzsWithMyDirectoryTenant {
802813
Sort ExpiresOn |
803814
Select -Last 1 -ExpandProperty RefreshToken |
804815
ConvertTo-SecureString -AsPlainText -Force
805-
816+
817+
# Workaround due to regression in AzurePowerShell profile module which fails to populate the response object of "Add-AzureRmAccount" cmdlet
818+
if (-not $refreshToken) {
819+
if ($tokens.Count -eq 1) {
820+
Write-Warning "Failed to find target refresh token from Azure PowerShell Cache; attempting to reuse the single cached auth context..."
821+
$refreshToken = $tokens[0].RefreshToken | ConvertTo-SecureString -AsPlainText -Force
822+
}
823+
else {
824+
throw "Unable to find refresh token from Azure PowerShell Cache. Please try the command again in a fresh PowerShell instance after running 'Clear-AzureRmContext -Scope CurrentUser -Force -Verbose'."
825+
}
826+
}
827+
806828
return $refreshToken
807829
}
808830

0 commit comments

Comments
 (0)