protected override void ProcessRecord() { base.ProcessRecord(); if (ShouldProcess("adding environment", Name)) { if (GraphEnvironment.BuiltInEnvironments.Keys.Any((k) => string.Equals(k, Name, StringComparison.CurrentCultureIgnoreCase))) { throw new InvalidOperationException( ErrorConstants.Message.CannotModifyBuiltInEnvironment.FormatCurrentCulture("add", Name)); } GraphSettings settings = this.GetContextSettings(); if (!settings.TryGetEnvironment(Name, out IGraphEnvironment newEnvironment)) { newEnvironment = new GraphEnvironment { Name = Name }; } newEnvironment.AzureADEndpoint = AzureADEndpoint.GetBaseUrl(); newEnvironment.GraphEndpoint = GraphEndpoint.GetBaseUrl(); newEnvironment.Type = GraphEnvironmentConstants.EnvironmentType.UserDefined; bool isSuccess = settings.TrySetEnvironment(newEnvironment, out IGraphEnvironment mergedEnvironment); // Update environment session object. if (isSuccess && GraphSession.Instance.Environment?.Name == mergedEnvironment?.Name) { GraphSession.Instance.Environment = mergedEnvironment; } WriteObject(mergedEnvironment); } }
protected override void ProcessRecord() { base.ProcessRecord(); if (ShouldProcess("updating environment", Name)) { if (GraphEnvironment.BuiltInEnvironments.Keys.Any((k) => string.Equals(k, Name, StringComparison.CurrentCultureIgnoreCase))) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Cannot change built-in environment {0}.", Name)); } GraphSettings settings = this.GetContextSettings(); if (!settings.TryGetEnvironment(Name, out IGraphEnvironment newEnvironment)) { newEnvironment = new GraphEnvironment { Name = Name }; } if (MyInvocation.BoundParameters.ContainsKey(nameof(AzureADEndpoint))) { newEnvironment.AzureADEndpoint = AzureADEndpoint.GetBaseUrl(); } if (MyInvocation.BoundParameters.ContainsKey(nameof(GraphEndpoint))) { newEnvironment.GraphEndpoint = GraphEndpoint.GetBaseUrl(); } ValidateEnvironment(newEnvironment); newEnvironment.Type = GraphEnvironmentConstants.EnvironmentType.UserDefined; bool isSuccess = settings.TrySetEnvironment(newEnvironment, out IGraphEnvironment mergedEnvironment); // Update environment session object. if (isSuccess && GraphSession.Instance.Environment?.Name == mergedEnvironment?.Name) { GraphSession.Instance.Environment = mergedEnvironment; } WriteObject(mergedEnvironment); } }