diff --git a/src/Accounts/Accounts/ChangeLog.md b/src/Accounts/Accounts/ChangeLog.md index 85d8909b56b3..e8aa609e4374 100644 --- a/src/Accounts/Accounts/ChangeLog.md +++ b/src/Accounts/Accounts/ChangeLog.md @@ -18,6 +18,7 @@ - Additional information about change #1 --> ## Upcoming Release +* Fix Get-AzTenant/Get-AzDefault/Set-AzDefault throw NullReferenceException when not login ## Version 1.7.0 * Updated Add-AzEnvironment and Set-AzEnvironment to accept parameters AzureAttestationServiceEndpointResourceId and AzureAttestationServiceEndpointSuffix diff --git a/src/Accounts/Accounts/Default/ClearAzureRmDefault.cs b/src/Accounts/Accounts/Default/ClearAzureRmDefault.cs index bfc4401b1b40..9aa608fbad1b 100644 --- a/src/Accounts/Accounts/Default/ClearAzureRmDefault.cs +++ b/src/Accounts/Accounts/Default/ClearAzureRmDefault.cs @@ -46,7 +46,7 @@ public override void ExecuteCmdlet() throw new Exception("Default Resource Group cannot be set on CloudShell"); } - IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext; + IAzureContext context = DefaultContext; // If no parameters are specified, clear all defaults if (!ResourceGroup) { diff --git a/src/Accounts/Accounts/Default/GetAzureRmDefault.cs b/src/Accounts/Accounts/Default/GetAzureRmDefault.cs index 4ee2e9421d35..68ad8885e28d 100644 --- a/src/Accounts/Accounts/Default/GetAzureRmDefault.cs +++ b/src/Accounts/Accounts/Default/GetAzureRmDefault.cs @@ -37,7 +37,7 @@ public class GetAzureRMDefaultCommand : AzureRMCmdlet public override void ExecuteCmdlet() { - IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext; + IAzureContext context = DefaultContext; IResourceManagementClient client = AzureSession.Instance.ClientFactory.CreateCustomArmClient( context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager), AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager), diff --git a/src/Accounts/Accounts/Default/SetAzureRMDefault.cs b/src/Accounts/Accounts/Default/SetAzureRMDefault.cs index f8cdb095bb8a..bd6975baa1e4 100644 --- a/src/Accounts/Accounts/Default/SetAzureRMDefault.cs +++ b/src/Accounts/Accounts/Default/SetAzureRMDefault.cs @@ -50,7 +50,7 @@ public override void ExecuteCmdlet() throw new Exception("Default Resource Group cannot be set on CloudShell"); } - IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext; + IAzureContext context = DefaultContext; IResourceManagementClient resourceManagementclient = AzureSession.Instance.ClientFactory.CreateCustomArmClient( context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager), AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager), diff --git a/src/Accounts/Accounts/Models/RMProfileClient.cs b/src/Accounts/Accounts/Models/RMProfileClient.cs index ef7206b157b5..a41b88919c85 100644 --- a/src/Accounts/Accounts/Models/RMProfileClient.cs +++ b/src/Accounts/Accounts/Models/RMProfileClient.cs @@ -41,6 +41,18 @@ public class RMProfileClient private IAzureTokenCache _cache; public Action WarningLog; + private IAzureContext DefaultContext + { + get + { + if(_profile == null || _profile.DefaultContext == null || _profile.DefaultContext.Account == null) + { + throw new PSInvalidOperationException(ResourceMessages.RunConnectAccount); + } + return _profile.DefaultContext; + } + } + public RMProfileClient(IProfileOperations profile) { _profile = profile; @@ -368,7 +380,7 @@ public List ListTenants(string tenant = "") return new List() { CreateTenant(tenant) }; } - List tenants = ListAccountTenants(_profile.DefaultContext.Account, _profile.DefaultContext.Environment, null, ShowDialog.Never, null); + List tenants = ListAccountTenants(DefaultContext.Account, DefaultContext.Environment, null, ShowDialog.Never, null); return tenants.Where(t => string.IsNullOrEmpty(tenant) || tenant.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase) || tenant.Equals(t.Directory, StringComparison.OrdinalIgnoreCase))