public override void ExecuteCmdlet()
        {
            ConfirmAction("adding environment", Name,
                          () =>
            {
                if (AzureEnvironment.PublicEnvironments.Keys.Any((k) => string.Equals(k, Name, StringComparison.CurrentCultureIgnoreCase)))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                      "Cannot add built-in or discovered environment {0}.", Name));
                }

                if (this.ParameterSetName.Equals(MetadataParameterSet, StringComparison.Ordinal))
                {
                    // Simply use built-in environments if the ARM endpoint matches the ARM endpoint for a built-in environment
                    var publicEnvironment = AzureEnvironment.PublicEnvironments.FirstOrDefault(
                        env => !string.IsNullOrWhiteSpace(ARMEndpoint) &&
                        string.Equals(
                            env.Value?.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager)?.ToLowerInvariant(),
                            GeneralUtilities.EnsureTrailingSlash(ARMEndpoint)?.ToLowerInvariant(), StringComparison.CurrentCultureIgnoreCase));

                    var defProfile = GetDefaultProfile();
                    IAzureEnvironment newEnvironment;
                    if (!defProfile.TryGetEnvironment(this.Name, out newEnvironment))
                    {
                        newEnvironment = new AzureEnvironment {
                            Name = this.Name,
                            Type = AzureEnvironment.TypeUserDefined
                        };
                    }

                    if (publicEnvironment.Key == null)
                    {
                        SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.ResourceManager, ARMEndpoint);
                        try
                        {
                            EnvHelper = (EnvHelper == null ? new EnvironmentHelper() : EnvHelper);
                            MetadataResponse metadataEndpoints = EnvHelper.RetrieveMetaDataEndpoints(newEnvironment.ResourceManagerUrl).Result;
                            string domain = EnvHelper.RetrieveDomain(ARMEndpoint);

                            SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.ActiveDirectory,
                                                  metadataEndpoints.authentication.LoginEndpoint.TrimEnd('/') + '/');
                            SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId,
                                                  metadataEndpoints.authentication.Audiences[0]);
                            SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.Gallery, metadataEndpoints.GalleryEndpoint);
                            SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.Graph, metadataEndpoints.GraphEndpoint);
                            SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.GraphEndpointResourceId,
                                                  metadataEndpoints.GraphEndpoint);
                            SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix,
                                                  AzureKeyVaultDnsSuffix ?? string.Format("vault.{0}", domain).ToLowerInvariant());
                            SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId,
                                                  AzureKeyVaultServiceEndpointResourceId ?? string.Format("https://vault.{0}", domain).ToLowerInvariant());
                            SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.StorageEndpointSuffix, StorageEndpoint ?? domain);
                            newEnvironment.OnPremise = metadataEndpoints.authentication.LoginEndpoint.TrimEnd('/').EndsWith("/adfs", System.StringComparison.OrdinalIgnoreCase);
                        }
                        catch (AggregateException ae)
                        {
                            if (ae.Flatten().InnerExceptions.Count > 1)
                            {
                                throw;
                            }

                            if (ae.InnerException != null)
                            {
                                throw ae.InnerException;
                            }
                        }
                    }
                    else
                    {
                        newEnvironment      = new AzureEnvironment(publicEnvironment.Value);
                        newEnvironment.Name = Name;
                        SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix,
                                              AzureKeyVaultDnsSuffix);
                        SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId,
                                              AzureKeyVaultServiceEndpointResourceId);
                    }

                    ModifyContext((profile, client) =>
                    {
                        WriteObject(new PSAzureEnvironment(client.AddOrSetEnvironment(newEnvironment)));
                    });
                }
                else if (this.ParameterSetName.Equals(EnvironmentPropertiesParameterSet, StringComparison.Ordinal))
                {
                    ModifyContext((profile, profileClient) =>
                    {
                        IAzureEnvironment newEnvironment = new AzureEnvironment {
                            Name = Name
                        };
                        if (profile.EnvironmentTable.ContainsKey(Name))
                        {
                            newEnvironment = profile.EnvironmentTable[Name];
                        }

                        if (MyInvocation != null && MyInvocation.BoundParameters != null)
                        {
                            if (MyInvocation.BoundParameters.ContainsKey(nameof(EnableAdfsAuthentication)))
                            {
                                newEnvironment.OnPremise = EnableAdfsAuthentication;
                            }

                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.PublishSettingsFileUrl,
                                               nameof(PublishSettingsFileUrl));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.ServiceManagement, nameof(ServiceEndpoint));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.ResourceManager,
                                               nameof(ResourceManagerEndpoint));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.ManagementPortalUrl,
                                               nameof(ManagementPortalUrl));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.StorageEndpointSuffix,
                                               nameof(StorageEndpoint));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.ActiveDirectory,
                                               nameof(ActiveDirectoryEndpoint), true);
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.ContainerRegistryEndpointSuffix,
                                               nameof(ContainerRegistryEndpointSuffix));
                            SetEndpointIfBound(newEnvironment,
                                               AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId,
                                               nameof(ActiveDirectoryServiceEndpointResourceId));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.Gallery, nameof(GalleryEndpoint));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.Graph, nameof(GraphEndpoint));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix,
                                               nameof(AzureKeyVaultDnsSuffix));
                            SetEndpointIfBound(newEnvironment,
                                               AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId,
                                               nameof(AzureKeyVaultServiceEndpointResourceId));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.TrafficManagerDnsSuffix,
                                               nameof(TrafficManagerDnsSuffix));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix,
                                               nameof(SqlDatabaseDnsSuffix));
                            SetEndpointIfBound(newEnvironment,
                                               AzureEnvironment.Endpoint.AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix,
                                               nameof(AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix));
                            SetEndpointIfBound(newEnvironment,
                                               AzureEnvironment.Endpoint.AzureDataLakeStoreFileSystemEndpointSuffix,
                                               nameof(AzureDataLakeStoreFileSystemEndpointSuffix));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.AdTenant, nameof(AdTenant));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.GraphEndpointResourceId,
                                               nameof(GraphAudience));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.DataLakeEndpointResourceId,
                                               nameof(DataLakeAudience));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.Endpoint.BatchEndpointResourceId,
                                               nameof(BatchEndpointResourceId));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpointResourceId,
                                               nameof(AzureOperationalInsightsEndpointResourceId));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpoint,
                                               nameof(AzureOperationalInsightsEndpoint));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.ExtendedEndpoint.AnalysisServicesEndpointSuffix,
                                               nameof(AzureAnalysisServicesEndpointSuffix));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.ExtendedEndpoint.AnalysisServicesEndpointResourceId,
                                               nameof(AzureAnalysisServicesEndpointResourceId));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.ExtendedEndpoint.AzureAttestationServiceEndpointSuffix,
                                               nameof(AzureAttestationServiceEndpointSuffix));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.ExtendedEndpoint.AzureAttestationServiceEndpointResourceId,
                                               nameof(AzureAttestationServiceEndpointResourceId));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.ExtendedEndpoint.AzureSynapseAnalyticsEndpointSuffix,
                                               nameof(AzureSynapseAnalyticsEndpointSuffix));
                            SetEndpointIfBound(newEnvironment, AzureEnvironment.ExtendedEndpoint.AzureSynapseAnalyticsEndpointResourceId,
                                               nameof(AzureSynapseAnalyticsEndpointResourceId));
                            WriteObject(new PSAzureEnvironment(profileClient.AddOrSetEnvironment(newEnvironment)));
                        }
                    });
                }
                else
                {
                    AzureEnvironment.DiscoverEnvironments(Uri?.ToString(), this.WriteDebug, this.WriteWarning);
                    ModifyContext((profile, profileClient) =>
                    {
                        foreach (var env in profile.EnvironmentTable.Where(i => (i.Value is AzureEnvironment environment) && AzureEnvironment.TypeDiscovered.Equals(environment.Type)).ToList())
                        {
                            profile.EnvironmentTable.Remove(env.Key);
                        }
                        foreach (var env in AzureEnvironment.PublicEnvironments.Values)
                        {
                            profile.EnvironmentTable[env.Name] = env;
                            WriteObject(new PSAzureEnvironment(env));
                        }
                    });