@@ -68,7 +68,7 @@ public AzureRMProfile Login(
68
68
var token = AcquireAccessToken ( account , environment , tenantId , password , promptBehavior ) ;
69
69
if ( TryGetTenantSubscription ( token , account , environment , tenantId , subscriptionId , subscriptionName , out newSubscription , out newTenant ) )
70
70
{
71
- account . SetProperty ( AzureAccount . Property . Tenants , new [ ] { newTenant . Id . ToString ( ) } ) ;
71
+ account . SetOrAppendProperty ( AzureAccount . Property . Tenants , new [ ] { newTenant . Id . ToString ( ) } ) ;
72
72
}
73
73
}
74
74
// (tenant is not provided and subscription is present) OR
@@ -240,7 +240,7 @@ private void SwitchSubscription(AzureSubscription subscription)
240
240
241
241
public List < AzureTenant > ListTenants ( string tenant )
242
242
{
243
- return ListAccountTenants ( _profile . Context . Account , _profile . Context . Environment , null , ShowDialog . Auto )
243
+ return ListAccountTenants ( _profile . Context . Account , _profile . Context . Environment , null , ShowDialog . Never )
244
244
. Where ( t => tenant == null ||
245
245
tenant . Equals ( t . Id . ToString ( ) , StringComparison . OrdinalIgnoreCase ) ||
246
246
tenant . Equals ( t . Domain , StringComparison . OrdinalIgnoreCase ) )
@@ -380,7 +380,9 @@ public IEnumerable<AzureSubscription> ListSubscriptions()
380
380
{
381
381
try
382
382
{
383
- subscriptions . AddRange ( ListSubscriptions ( tenant . Id . ToString ( ) ) ) ;
383
+ subscriptions . AddRange (
384
+ ListSubscriptions (
385
+ ( tenant . Id == Guid . Empty ) ? tenant . Domain : tenant . Id . ToString ( ) ) ) ;
384
386
}
385
387
catch ( AadAuthenticationException )
386
388
{
@@ -481,19 +483,24 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
481
483
}
482
484
else
483
485
{
484
- var subscriptions = subscriptionClient . Subscriptions . List ( ) . Subscriptions ;
485
- if ( subscriptions != null && subscriptions . Any ( ) )
486
+ var subscriptions = ( subscriptionClient . Subscriptions . List ( ) . Subscriptions ??
487
+ new List < Microsoft . Azure . Subscriptions . Models . Subscription > ( ) )
488
+ . Where ( s => "enabled" . Equals ( s . State , StringComparison . OrdinalIgnoreCase ) ||
489
+ "warned" . Equals ( s . State , StringComparison . OrdinalIgnoreCase ) ) ;
490
+
491
+ if ( subscriptions . Any ( ) )
486
492
{
487
493
if ( subscriptionName != null )
488
494
{
489
- subscriptionFromServer = subscriptions . FirstOrDefault ( s => s . DisplayName . Equals ( subscriptionName , StringComparison . OrdinalIgnoreCase ) ) ;
495
+ subscriptionFromServer = subscriptions . FirstOrDefault (
496
+ s => s . DisplayName . Equals ( subscriptionName , StringComparison . OrdinalIgnoreCase ) ) ;
490
497
}
491
498
else
492
499
{
493
- if ( subscriptions . Count > 1 )
500
+ if ( subscriptions . Count ( ) > 1 )
494
501
{
495
502
WriteWarningMessage ( string . Format (
496
- "TenantId '{0}' contains more than one subscription. First one will be selected for further use. " +
503
+ "TenantId '{0}' contains more than one active subscription. First one will be selected for further use. " +
497
504
"To select another subscription, use Set-AzureRmContext." ,
498
505
tenantId ) ) ;
499
506
}
@@ -569,10 +576,21 @@ private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironm
569
576
{
570
577
result =
571
578
account . GetPropertyAsArray ( AzureAccount . Property . Tenants )
572
- . Select ( ti => new AzureTenant ( )
573
- {
574
- Id = new Guid ( ti ) ,
575
- Domain = AccessTokenExtensions . GetDomain ( account . Id )
579
+ . Select ( ti => {
580
+ var tenant = new AzureTenant ( ) ;
581
+
582
+ Guid guid ;
583
+ if ( Guid . TryParse ( ti , out guid ) )
584
+ {
585
+ tenant . Id = guid ;
586
+ tenant . Domain = AccessTokenExtensions . GetDomain ( account . Id ) ;
587
+ }
588
+ else
589
+ {
590
+ tenant . Domain = ti ;
591
+ }
592
+
593
+ return tenant ;
576
594
} ) . ToList ( ) ;
577
595
}
578
596
@@ -608,7 +626,7 @@ private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount a
608
626
subscriptions . Subscriptions . Select (
609
627
( s ) =>
610
628
s . ToAzureSubscription ( new AzureContext ( _profile . Context . Subscription , account ,
611
- environment , CreateTenantFromString ( tenantId ) ) ) ) ;
629
+ environment , CreateTenantFromString ( tenantId , accessToken . TenantId ) ) ) ) ;
612
630
}
613
631
614
632
return new List < AzureSubscription > ( ) ;
@@ -623,7 +641,7 @@ private void WriteWarningMessage(string message)
623
641
}
624
642
}
625
643
626
- private static AzureTenant CreateTenantFromString ( string tenantOrDomain )
644
+ private static AzureTenant CreateTenantFromString ( string tenantOrDomain , string accessTokenTenantId )
627
645
{
628
646
AzureTenant result = new AzureTenant ( ) ;
629
647
Guid id ;
@@ -633,6 +651,7 @@ private static AzureTenant CreateTenantFromString(string tenantOrDomain)
633
651
}
634
652
else
635
653
{
654
+ result . Id = Guid . Parse ( accessTokenTenantId ) ;
636
655
result . Domain = tenantOrDomain ;
637
656
}
638
657
0 commit comments