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 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
                        };
                    }

                    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
                {
                    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.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));
                            WriteObject(new PSAzureEnvironment(profileClient.AddOrSetEnvironment(newEnvironment)));
                        }
                    });
                }
            });
        }