示例#1
0
 private HistoryInfo GetHistoryEntryToInvoke(History history)
 {
     HistoryInfo entry = null;
     if (this._id == null)
     {
         HistoryInfo[] infoArray = history.GetEntries((long) 0L, 1L, true);
         if (infoArray.Length == 1)
         {
             return infoArray[0];
         }
         Exception exception = new InvalidOperationException(StringUtil.Format(HistoryStrings.NoLastHistoryEntryFound, new object[0]));
         base.ThrowTerminatingError(new ErrorRecord(exception, "InvokeHistoryNoLastHistoryEntryFound", ErrorCategory.InvalidOperation, null));
         return entry;
     }
     this.PopulateIdAndCommandLine();
     if (this._commandLine == null)
     {
         if (this._historyId <= 0L)
         {
             Exception exception3 = new ArgumentOutOfRangeException("Id", StringUtil.Format(HistoryStrings.InvalidIdGetHistory, this._historyId));
             base.ThrowTerminatingError(new ErrorRecord(exception3, "InvokeHistoryInvalidIdGetHistory", ErrorCategory.InvalidArgument, this._historyId));
             return entry;
         }
         entry = history.GetEntry(this._historyId);
         if ((entry == null) || (entry.Id != this._historyId))
         {
             Exception exception4 = new ArgumentException(StringUtil.Format(HistoryStrings.NoHistoryForId, this._historyId));
             base.ThrowTerminatingError(new ErrorRecord(exception4, "InvokeHistoryNoHistoryForId", ErrorCategory.ObjectNotFound, this._historyId));
         }
         return entry;
     }
     HistoryInfo[] infoArray2 = history.GetEntries((long) 0L, -1L, false);
     for (int i = infoArray2.Length - 1; i >= 0; i--)
     {
         if (infoArray2[i].CommandLine.StartsWith(this._commandLine, StringComparison.CurrentCulture))
         {
             entry = infoArray2[i];
             break;
         }
     }
     if (entry == null)
     {
         Exception exception2 = new ArgumentException(StringUtil.Format(HistoryStrings.NoHistoryForCommandline, this._commandLine));
         base.ThrowTerminatingError(new ErrorRecord(exception2, "InvokeHistoryNoHistoryForCommandline", ErrorCategory.ObjectNotFound, this._commandLine));
     }
     return entry;
 }
示例#2
0
 protected override void Dispose(bool disposing)
 {
     try
     {
         if (!this._disposed)
         {
             lock (base.SyncRoot)
             {
                 if (this._disposed)
                 {
                     return;
                 }
                 this._disposed = true;
             }
             if (disposing)
             {
                 this.Close();
                 this._engine             = null;
                 this._history            = null;
                 this._jobManager         = null;
                 this._jobRepository      = null;
                 this._runspaceRepository = null;
                 if (base.RunspaceOpening != null)
                 {
                     base.RunspaceOpening.Dispose();
                     base.RunspaceOpening = null;
                 }
                 if ((base.ExecutionContext != null) && (base.ExecutionContext.Events != null))
                 {
                     try
                     {
                         base.ExecutionContext.Events.Dispose();
                     }
                     catch (ObjectDisposedException)
                     {
                     }
                 }
             }
         }
     }
     finally
     {
         base.Dispose(disposing);
     }
 }
示例#3
0
        public override void ResetRunspaceState()
        {
            PSInvalidOperationException exception = null;

            if (this.InitialSessionState == null)
            {
                exception = PSTraceSource.NewInvalidOperationException();
            }
            else if (base.RunspaceState != RunspaceState.Opened)
            {
                exception = PSTraceSource.NewInvalidOperationException("RunspaceStrings", "RunspaceNotInOpenedState", new object[] { base.RunspaceState });
            }
            else if (this.RunspaceAvailability != RunspaceAvailability.Available)
            {
                exception = PSTraceSource.NewInvalidOperationException("RunspaceStrings", "ConcurrentInvokeNotAllowed", new object[0]);
            }
            if (exception != null)
            {
                exception.Source = "ResetRunspaceState";
                throw exception;
            }
            this.InitialSessionState.ResetRunspaceState(base.ExecutionContext);
            this._history = new Microsoft.PowerShell.Commands.History(base.ExecutionContext);
        }
示例#4
0
 protected override void BeginProcessing()
 {
     this.history = ((LocalRunspace) base.Context.CurrentRunspace).History;
 }
示例#5
0
        /// <summary>
        /// Helper function used for opening a runspace
        /// </summary>
        private void DoOpenHelper()
        {
            // NTRAID#Windows Out Of Band Releases-915851-2005/09/13
            if (_disposed)
            {
                throw PSTraceSource.NewObjectDisposedException("runspace");
            }

            bool startLifeCycleEventWritten = false;
            s_runspaceInitTracer.WriteLine("begin open runspace");
            try
            {
                _transcriptionData = new TranscriptionData();

                if (InitialSessionState != null)
                {
                    // All ISS-based configuration of the engine itself is done by AutomationEngine,
                    // which calls InitialSessionState.Bind(). Anything that doesn't
                    // require an active and open runspace should be done in ISS.Bind()
                    _engine = new AutomationEngine(Host, null, InitialSessionState);
                }
                else
                {
                    _engine = new AutomationEngine(Host, RunspaceConfiguration, null);
                }
                _engine.Context.CurrentRunspace = this;

                //Log engine for start of engine life
                MshLog.LogEngineLifecycleEvent(_engine.Context, EngineState.Available);
                startLifeCycleEventWritten = true;

                _commandFactory = new CommandFactory(_engine.Context);
                _history = new History(_engine.Context);
                _jobRepository = new JobRepository();
                _jobManager = new JobManager();
                _runspaceRepository = new RunspaceRepository();

                s_runspaceInitTracer.WriteLine("initializing built-in aliases and variable information");
                InitializeDefaults();
            }
            catch (Exception exception)
            {
                CommandProcessorBase.CheckForSevereException(exception);
                s_runspaceInitTracer.WriteLine("Runspace open failed");

                //Log engine health event
                LogEngineHealthEvent(exception);

                //Log engine for end of engine life
                if (startLifeCycleEventWritten)
                {
                    Dbg.Assert(_engine.Context != null, "if startLifeCycleEventWritten is true, ExecutionContext must be present");
                    MshLog.LogEngineLifecycleEvent(_engine.Context, EngineState.Stopped);
                }

                //Open failed. Set the RunspaceState to Broken.
                SetRunspaceState(RunspaceState.Broken, exception);

                //Raise the event
                RaiseRunspaceStateEvents();

                //Rethrow the exception. For asynchronous execution, 
                //OpenThreadProc will catch it. For synchronous execution
                //caller of open will catch it.
                throw;
            }

            SetRunspaceState(RunspaceState.Opened);
            RunspaceOpening.Set();

            //Raise the event
            RaiseRunspaceStateEvents();
            s_runspaceInitTracer.WriteLine("runspace opened successfully");

            // Now do initial state configuration that requires an active runspace
            if (InitialSessionState != null)
            {
                Exception initError = InitialSessionState.BindRunspace(this, s_runspaceInitTracer);
                if (initError != null)
                {
                    // Log engine health event
                    LogEngineHealthEvent(initError);

                    // Log engine for end of engine life
                    Debug.Assert(_engine.Context != null,
                                "if startLifeCycleEventWritten is true, ExecutionContext must be present");
                    MshLog.LogEngineLifecycleEvent(_engine.Context, EngineState.Stopped);

                    // Open failed. Set the RunspaceState to Broken.
                    SetRunspaceState(RunspaceState.Broken, initError);

                    // Raise the event
                    RaiseRunspaceStateEvents();

                    // Throw the exception. For asynchronous execution, 
                    // OpenThreadProc will catch it. For synchronous execution
                    // caller of open will catch it.
                    throw initError;
                }
            }

            TelemetryAPI.ReportLocalSessionCreated(InitialSessionState, TranscriptionData);
        }
示例#6
0
        /// <summary>
        /// Resets the runspace state to allow for fast reuse. Not all of the runspace
        /// elements are reset. The goal is to minimize the chance of the user taking
        /// accidental dependencies on prior runspace state.
        /// </summary>
        public override void ResetRunspaceState()
        {
            PSInvalidOperationException invalidOperation = null;

            if (this.InitialSessionState == null)
            {
                invalidOperation = PSTraceSource.NewInvalidOperationException();
            }
            else if (this.RunspaceState != Runspaces.RunspaceState.Opened)
            {
                invalidOperation = PSTraceSource.NewInvalidOperationException(
                        RunspaceStrings.RunspaceNotInOpenedState, this.RunspaceState);
            }
            else if (this.RunspaceAvailability != Runspaces.RunspaceAvailability.Available)
            {
                invalidOperation = PSTraceSource.NewInvalidOperationException(
                        RunspaceStrings.ConcurrentInvokeNotAllowed);
            }

            if (invalidOperation != null)
            {
                invalidOperation.Source = "ResetRunspaceState";
                throw invalidOperation;
            }

            this.InitialSessionState.ResetRunspaceState(this.ExecutionContext);

            // Finally, reset history for this runspace. This needs to be done
            // last to so that changes to the default MaximumHistorySize will be picked up.
            _history = new History(this.ExecutionContext);
        }
示例#7
0
 public override void ResetRunspaceState()
 {
     PSInvalidOperationException exception = null;
     if (this.InitialSessionState == null)
     {
         exception = PSTraceSource.NewInvalidOperationException();
     }
     else if (base.RunspaceState != RunspaceState.Opened)
     {
         exception = PSTraceSource.NewInvalidOperationException("RunspaceStrings", "RunspaceNotInOpenedState", new object[] { base.RunspaceState });
     }
     else if (this.RunspaceAvailability != RunspaceAvailability.Available)
     {
         exception = PSTraceSource.NewInvalidOperationException("RunspaceStrings", "ConcurrentInvokeNotAllowed", new object[0]);
     }
     if (exception != null)
     {
         exception.Source = "ResetRunspaceState";
         throw exception;
     }
     this.InitialSessionState.ResetRunspaceState(base.ExecutionContext);
     this._history = new Microsoft.PowerShell.Commands.History(base.ExecutionContext);
 }
示例#8
0
 private void DoOpenHelper()
 {
     if (this._disposed)
     {
         throw PSTraceSource.NewObjectDisposedException("runspace");
     }
     bool startLifeCycleEventWritten = false;
     runspaceInitTracer.WriteLine("begin open runspace", new object[0]);
     try
     {
         if (this.InitialSessionState != null)
         {
             this._engine = new AutomationEngine(base.Host, null, this.InitialSessionState);
         }
         else
         {
             this._engine = new AutomationEngine(base.Host, this.RunspaceConfiguration, null);
         }
         this._engine.Context.CurrentRunspace = this;
         MshLog.LogEngineLifecycleEvent(this._engine.Context, EngineState.Available);
         startLifeCycleEventWritten = true;
         this._commandFactory = new System.Management.Automation.CommandFactory(this._engine.Context);
         this._history = new Microsoft.PowerShell.Commands.History(this._engine.Context);
         this._jobRepository = new System.Management.Automation.JobRepository();
         this._jobManager = new System.Management.Automation.JobManager();
         this._runspaceRepository = new System.Management.Automation.RunspaceRepository();
         runspaceInitTracer.WriteLine("initializing built-in aliases and variable information", new object[0]);
         this.InitializeDefaults();
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         runspaceInitTracer.WriteLine("Runspace open failed", new object[0]);
         this.LogEngineHealthEvent(exception);
         if (startLifeCycleEventWritten)
         {
             MshLog.LogEngineLifecycleEvent(this._engine.Context, EngineState.Stopped);
         }
         base.SetRunspaceState(RunspaceState.Broken, exception);
         base.RaiseRunspaceStateEvents();
         throw;
     }
     base.SetRunspaceState(RunspaceState.Opened);
     base.RunspaceOpening.Set();
     base.RaiseRunspaceStateEvents();
     runspaceInitTracer.WriteLine("runspace opened successfully", new object[0]);
     string environmentVariable = Environment.GetEnvironmentVariable("PSMODULEPATH");
     if (this.InitialSessionState != null)
     {
         try
         {
             Environment.SetEnvironmentVariable("PSMODULEPATH", ModuleIntrinsics.GetSystemwideModulePath());
             this.ProcessImportModule(this.InitialSessionState.CoreModulesToImport, startLifeCycleEventWritten);
             this._engine.Context.EngineSessionState.Module = null;
         }
         finally
         {
             Environment.SetEnvironmentVariable("PSMODULEPATH", environmentVariable);
         }
         this.ProcessImportModule(this.InitialSessionState.ModuleSpecificationsToImport, startLifeCycleEventWritten);
         InitialSessionState.SetSessionStateDrive(this._engine.Context, true);
         if (this.InitialSessionState.WarmUpTabCompletionOnIdle)
         {
             ScriptBlock action = ScriptBlock.Create("$null = [System.Management.Automation.CommandCompletion]::CompleteInput('Set-Location', 12, $null)");
             this._engine.Context.Events.SubscribeEvent(null, null, "PowerShell.OnIdle", null, action, true, false, 1);
         }
     }
 }
示例#9
0
 protected override void Dispose(bool disposing)
 {
     try
     {
         if (!this._disposed)
         {
             lock (base.SyncRoot)
             {
                 if (this._disposed)
                 {
                     return;
                 }
                 this._disposed = true;
             }
             if (disposing)
             {
                 this.Close();
                 this._engine = null;
                 this._history = null;
                 this._jobManager = null;
                 this._jobRepository = null;
                 this._runspaceRepository = null;
                 if (base.RunspaceOpening != null)
                 {
                     base.RunspaceOpening.Dispose();
                     base.RunspaceOpening = null;
                 }
                 if ((base.ExecutionContext != null) && (base.ExecutionContext.Events != null))
                 {
                     try
                     {
                         base.ExecutionContext.Events.Dispose();
                     }
                     catch (ObjectDisposedException)
                     {
                     }
                 }
             }
         }
     }
     finally
     {
         base.Dispose(disposing);
     }
 }
示例#10
0
        private HistoryInfo GetHistoryEntryToInvoke(History history)
        {
            HistoryInfo entry = null;

            //User didn't specify any input parameter. Invoke the last 
            //entry
            if (_id == null)
            {
                HistoryInfo[] entries = history.GetEntries(0, 1, true);

                if (entries.Length == 1)
                {
                    entry = entries[0];
                }
                else
                {
                    Exception ex =
                        new InvalidOperationException
                        (
                            StringUtil.Format(HistoryStrings.NoLastHistoryEntryFound)
                        );

                    ThrowTerminatingError
                    (
                        new ErrorRecord
                        (
                            ex,
                            "InvokeHistoryNoLastHistoryEntryFound",
                            ErrorCategory.InvalidOperation,
                            null
                        )
                    );
                }
            }
            else
            {
                //Parse input
                PopulateIdAndCommandLine();
                //User specified a commandline. Get list of all history entries
                //and find latest match
                if (_commandLine != null)
                {
                    HistoryInfo[] entries = history.GetEntries(0, -1, false);

                    // and search backwards through the entries
                    for (int i = entries.Length - 1; i >= 0; i--)
                    {
                        if (entries[i].CommandLine.StartsWith(_commandLine, StringComparison.CurrentCulture))
                        {
                            entry = entries[i];
                            break;
                        }
                    }
                    if (entry == null)
                    {
                        Exception ex =
                            new ArgumentException
                            (
                                StringUtil.Format(HistoryStrings.NoHistoryForCommandline, _commandLine)
                            );

                        ThrowTerminatingError
                        (
                            new ErrorRecord
                            (
                                ex,
                                "InvokeHistoryNoHistoryForCommandline",
                                ErrorCategory.ObjectNotFound,
                                _commandLine
                            )
                        );
                    }
                }
                else
                {
                    if (_historyId <= 0)
                    {
                        Exception ex =
                            new ArgumentOutOfRangeException
                            (
                                "Id",
                                StringUtil.Format(HistoryStrings.InvalidIdGetHistory, _historyId)
                            );

                        ThrowTerminatingError
                        (
                            new ErrorRecord
                            (
                                ex,
                                "InvokeHistoryInvalidIdGetHistory",
                                ErrorCategory.InvalidArgument,
                                _historyId
                            )
                        );
                    }
                    else
                    {
                        //Retrieve the command at the index we've specified
                        entry = history.GetEntry(_historyId);
                        if (entry == null || entry.Id != _historyId)
                        {
                            Exception ex =
                                new ArgumentException
                                (
                                    StringUtil.Format(HistoryStrings.NoHistoryForId, _historyId)
                                );

                            ThrowTerminatingError
                            (
                                new ErrorRecord
                                (
                                    ex,
                                    "InvokeHistoryNoHistoryForId",
                                    ErrorCategory.ObjectNotFound,
                                    _historyId
                                )
                            );
                        }
                    }
                }
            }
            return entry;
        }
示例#11
0
        private void DoOpenHelper()
        {
            if (this._disposed)
            {
                throw PSTraceSource.NewObjectDisposedException("runspace");
            }
            bool startLifeCycleEventWritten = false;

            runspaceInitTracer.WriteLine("begin open runspace", new object[0]);
            try
            {
                if (this.InitialSessionState != null)
                {
                    this._engine = new AutomationEngine(base.Host, null, this.InitialSessionState);
                }
                else
                {
                    this._engine = new AutomationEngine(base.Host, this.RunspaceConfiguration, null);
                }
                this._engine.Context.CurrentRunspace = this;
                MshLog.LogEngineLifecycleEvent(this._engine.Context, EngineState.Available);
                startLifeCycleEventWritten = true;
                this._commandFactory       = new System.Management.Automation.CommandFactory(this._engine.Context);
                this._history            = new Microsoft.PowerShell.Commands.History(this._engine.Context);
                this._jobRepository      = new System.Management.Automation.JobRepository();
                this._jobManager         = new System.Management.Automation.JobManager();
                this._runspaceRepository = new System.Management.Automation.RunspaceRepository();
                runspaceInitTracer.WriteLine("initializing built-in aliases and variable information", new object[0]);
                this.InitializeDefaults();
            }
            catch (Exception exception)
            {
                CommandProcessorBase.CheckForSevereException(exception);
                runspaceInitTracer.WriteLine("Runspace open failed", new object[0]);
                this.LogEngineHealthEvent(exception);
                if (startLifeCycleEventWritten)
                {
                    MshLog.LogEngineLifecycleEvent(this._engine.Context, EngineState.Stopped);
                }
                base.SetRunspaceState(RunspaceState.Broken, exception);
                base.RaiseRunspaceStateEvents();
                throw;
            }
            base.SetRunspaceState(RunspaceState.Opened);
            base.RunspaceOpening.Set();
            base.RaiseRunspaceStateEvents();
            runspaceInitTracer.WriteLine("runspace opened successfully", new object[0]);
            string environmentVariable = Environment.GetEnvironmentVariable("PSMODULEPATH");

            if (this.InitialSessionState != null)
            {
                try
                {
                    Environment.SetEnvironmentVariable("PSMODULEPATH", ModuleIntrinsics.GetSystemwideModulePath());
                    this.ProcessImportModule(this.InitialSessionState.CoreModulesToImport, startLifeCycleEventWritten);
                    this._engine.Context.EngineSessionState.Module = null;
                }
                finally
                {
                    Environment.SetEnvironmentVariable("PSMODULEPATH", environmentVariable);
                }
                this.ProcessImportModule(this.InitialSessionState.ModuleSpecificationsToImport, startLifeCycleEventWritten);
                InitialSessionState.SetSessionStateDrive(this._engine.Context, true);
                if (this.InitialSessionState.WarmUpTabCompletionOnIdle)
                {
                    ScriptBlock action = ScriptBlock.Create("$null = [System.Management.Automation.CommandCompletion]::CompleteInput('Set-Location', 12, $null)");
                    this._engine.Context.Events.SubscribeEvent(null, null, "PowerShell.OnIdle", null, action, true, false, 1);
                }
            }
        }