diff --git a/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs index d211b0c2aa..9090fd957e 100644 --- a/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs +++ b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs @@ -999,8 +999,11 @@ private string QualifyFilePath(string path) /// private void ResetGraphSessionEnvironment() { - _originalEnvironment = GraphSession.Instance.Environment; - GraphSession.Instance.Environment = _originalEnvironment; + var currentEnvironment = GraphSession.Instance.Environment; + if(currentEnvironment != null && !currentEnvironment.Equals(_originalEnvironment)) + { + GraphSession.Instance.Environment = _originalEnvironment; + } } #region CmdLet LifeCycle @@ -1039,6 +1042,8 @@ private async Task ProcessRecordAsync() if (ShouldCheckHttpStatus && !isSuccess) { var httpErrorRecord = await GenerateHttpErrorRecordAsync(httpResponseMessageFormatter, httpRequestMessage); + // A reset of the GraphSession Environment is required to avoid side effects + ResetGraphSessionEnvironment(); ThrowTerminatingError(httpErrorRecord); } await ProcessResponseAsync(httpResponseMessage);