diff --git a/src/System.Management.Automation/engine/AutomationEngine.cs b/src/System.Management.Automation/engine/AutomationEngine.cs index bc57b454355..743f87271d6 100644 --- a/src/System.Management.Automation/engine/AutomationEngine.cs +++ b/src/System.Management.Automation/engine/AutomationEngine.cs @@ -93,8 +93,6 @@ internal AutomationEngine(PSHost hostInterface, RunspaceConfiguration runspaceCo } InitialSessionState.SetSessionStateDrive(Context, true); - - InitialSessionState.CreateQuestionVariable(Context); } /// diff --git a/src/System.Management.Automation/engine/SessionState.cs b/src/System.Management.Automation/engine/SessionState.cs index 8ac4ae84ee6..9ced8b5e7ec 100644 --- a/src/System.Management.Automation/engine/SessionState.cs +++ b/src/System.Management.Automation/engine/SessionState.cs @@ -351,6 +351,10 @@ internal void InitializeFixedVariables() v = new PSUICultureVariable(); this.GlobalScope.SetVariable(v.Name, v, false, true, this, CommandOrigin.Internal, fastPath: true); + // $? + v = new QuestionMarkVariable(this.ExecutionContext); + this.GlobalScope.SetVariableForce(v, this); + // $ShellId - if there is no runspace config, use the default string string shellId = ExecutionContext.ShellID; @@ -648,4 +652,4 @@ internal ProviderInvocationException NewProviderInvocationException( } #endregion Errors } // SessionStateInternal class -} \ No newline at end of file +} diff --git a/src/System.Management.Automation/engine/SessionStateScope.cs b/src/System.Management.Automation/engine/SessionStateScope.cs index e30e7b6a303..6429ed71a85 100644 --- a/src/System.Management.Automation/engine/SessionStateScope.cs +++ b/src/System.Management.Automation/engine/SessionStateScope.cs @@ -629,6 +629,24 @@ internal PSVariable SetVariable(string name, object value, bool asValue, bool fo return variable; } // SetVariable + /// + /// Sets a variable to scope without any checks. + /// This is intended to be used only for global scope. + /// + /// PSVariable to set + /// SessionState for variable + /// + internal void SetVariableForce(PSVariable variableToSet, SessionStateInternal sessionState) + { + if (Parent != null) + { + throw new NotImplementedException("SetVariableForce"); + } + + variableToSet.SessionState = sessionState; + GetPrivateVariables()[variableToSet.Name] = variableToSet; + } + /// /// Sets a variable to the given value. ///