@@ -201,26 +201,33 @@ function Export-AzTokenFromCache {
201201 # Resolve target account
202202 #
203203
204- $targetAccount = $accounts | Where Username -EQ $AccountId
204+ $targetAccountIdentifier = $accounts | Where Username -EQ $AccountId | ForEach { $_ .HomeAccountId.Identifier } | Select - Unique
205205
206- if (-not $targetAccount -or $targetAccount.Count -gt 1 ) {
206+ if (-not $targetAccountIdentifier -and $data.Account )
207+ {
208+ # Fallback to resolve account identifier from data
209+ $targetAccountIdentifier = ($data.Account | Get-Member - MemberType NoteProperty).Name | ForEach { $data.Account ." $_ " } | Where Username -EQ $AccountId | Select - ExpandProperty home_account_id - Unique
210+ }
211+
212+ if (-not $targetAccountIdentifier -or $targetAccountIdentifier.Count -gt 1 )
213+ {
207214 Write-Error " Unable to resolve acccount for identity '$AccountId '; available accounts: $ ( ConvertTo-Json $accounts.Username - Compress) "
208215 return
209216 }
210217
211- Write-Verbose " Target account resolved to: $ ( ConvertTo-Json $targetAccount - Compress ) "
218+ Write-Verbose " Target account resolved to: $targetAccountIdentifier "
212219
213220 #
214221 # Resolve target token(s)
215222 #
216223
217224 $resolvedRefreshToken = $data.RefreshToken ." $ ( Get-Member - InputObject $data.RefreshToken - MemberType NoteProperty |
218- Where { " $ ( $_.Name ) " .StartsWith($targetAccount .HomeAccountId.Identifier , [System.StringComparison ]::OrdinalIgnoreCase) } |
225+ Where { " $ ( $_.Name ) " .StartsWith($targetAccountIdentifier , [System.StringComparison ]::OrdinalIgnoreCase) } |
219226 Select - ExpandProperty Name) " .secret
220227
221228 $resolvedAccessToken = Get-Member - InputObject $data.AccessToken - MemberType NoteProperty |
222229 ForEach { $data.AccessToken ." $ ( $_.Name ) " } |
223- Where home_account_id -EQ $targetAccount .HomeAccountId.Identifier |
230+ Where home_account_id -EQ $targetAccountIdentifier |
224231 Where { (-not $_.realm ) -or ($_.realm -eq $TenantId ) } |
225232 Where target -Like " *$Resource *" |
226233 Sort expires_on - Descending |
0 commit comments