Skip to content

Commit cc0f96f

Browse files
authored
Merge pull request Azure#10303 from VeryEarly/nullreferenceexception-in-AzAccounts#10292
Nullreferenceexception in az accounts#10292
2 parents ec245c0 + 5705fd0 commit cc0f96f

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fix Get-AzTenant/Get-AzDefault/Set-AzDefault throw NullReferenceException when not login
2122

2223
## Version 1.7.3
2324
* Open Azure PowerShell survey page in `Send-Feedback` [#11020]

src/Accounts/Accounts/Default/ClearAzureRmDefault.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public override void ExecuteCmdlet()
4646
throw new Exception("Default Resource Group cannot be set on CloudShell");
4747
}
4848

49-
IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext;
49+
IAzureContext context = DefaultContext;
5050
// If no parameters are specified, clear all defaults
5151
if (!ResourceGroup)
5252
{

src/Accounts/Accounts/Default/GetAzureRmDefault.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class GetAzureRMDefaultCommand : AzureRMCmdlet
3737

3838
public override void ExecuteCmdlet()
3939
{
40-
IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext;
40+
IAzureContext context = DefaultContext;
4141
IResourceManagementClient client = AzureSession.Instance.ClientFactory.CreateCustomArmClient<ResourceManagementClient>(
4242
context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
4343
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager),

src/Accounts/Accounts/Default/SetAzureRMDefault.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public override void ExecuteCmdlet()
5050
throw new Exception("Default Resource Group cannot be set on CloudShell");
5151
}
5252

53-
IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext;
53+
IAzureContext context = DefaultContext;
5454
IResourceManagementClient resourceManagementclient = AzureSession.Instance.ClientFactory.CreateCustomArmClient<ResourceManagementClient>(
5555
context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
5656
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager),

src/Accounts/Accounts/Models/RMProfileClient.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public class RMProfileClient
4141
private IAzureTokenCache _cache;
4242
public Action<string> WarningLog;
4343

44+
private IAzureContext DefaultContext
45+
{
46+
get
47+
{
48+
if(_profile == null || _profile.DefaultContext == null || _profile.DefaultContext.Account == null)
49+
{
50+
throw new PSInvalidOperationException(ResourceMessages.RunConnectAccount);
51+
}
52+
return _profile.DefaultContext;
53+
}
54+
}
55+
4456
public RMProfileClient(IProfileOperations profile)
4557
{
4658
_profile = profile;
@@ -368,7 +380,7 @@ public List<AzureTenant> ListTenants(string tenant = "")
368380
return new List<AzureTenant>() { CreateTenant(tenant) };
369381
}
370382

371-
List<AzureTenant> tenants = ListAccountTenants(_profile.DefaultContext.Account, _profile.DefaultContext.Environment, null, ShowDialog.Never, null);
383+
List<AzureTenant> tenants = ListAccountTenants(DefaultContext.Account, DefaultContext.Environment, null, ShowDialog.Never, null);
372384
return tenants.Where(t => string.IsNullOrEmpty(tenant) ||
373385
tenant.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase) ||
374386
tenant.Equals(t.Directory, StringComparison.OrdinalIgnoreCase))

0 commit comments

Comments
 (0)