Beispiel #1
0
 private void ValidateAndThrowRunspaceOpenModuleLoadException(PowerShell pse, List <ErrorRecord> errors, bool startLifeCycleEventWritten, string moduleName, RunspaceOpenModuleLoadException exception)
 {
     if (this.InitialSessionState.ThrowOnRunspaceOpenError)
     {
         RunspaceOpenModuleLoadException exception2 = null;
         if (exception != null)
         {
             exception2 = exception;
         }
         else if ((pse.Streams.Error.Count > 0) || (errors.Count > 0))
         {
             ErrorRecord record;
             Exception   exception3;
             PSDataCollection <ErrorRecord> datas = new PSDataCollection <ErrorRecord>();
             if (errors.Count > 0)
             {
                 record     = errors[0];
                 exception3 = record.Exception;
                 foreach (ErrorRecord record2 in errors)
                 {
                     datas.Add(record2);
                 }
             }
             else
             {
                 record     = pse.Streams.Error[0];
                 exception3 = record.Exception;
                 foreach (ErrorRecord record3 in pse.Streams.Error)
                 {
                     datas.Add(record3);
                 }
             }
             runspaceInitTracer.WriteLine("Runspace open failed while loading module '{0}': First error {1}", new object[] { moduleName, exception3 });
             exception2 = new RunspaceOpenModuleLoadException(moduleName, datas);
         }
         if (exception2 != null)
         {
             this.LogEngineHealthEvent(exception2);
             if (startLifeCycleEventWritten)
             {
                 MshLog.LogEngineLifecycleEvent(this._engine.Context, EngineState.Stopped);
             }
             base.SetRunspaceState(RunspaceState.Broken, exception2);
             base.RaiseRunspaceStateEvents();
             throw exception2;
         }
     }
 }
Beispiel #2
0
 private void DoCloseHelper()
 {
     base.StopPipelines();
     this.StopOrDisconnectAllJobs();
     this.CloseOrDisconnectAllRemoteRunspaces(delegate {
         List <RemoteRunspace> list = new List <RemoteRunspace>();
         foreach (PSSession session in this.RunspaceRepository.Runspaces)
         {
             list.Add(session.Runspace as RemoteRunspace);
         }
         return(list);
     });
     this._engine.Context.RunspaceClosingNotification();
     MshLog.LogEngineLifecycleEvent(this._engine.Context, EngineState.Stopped);
     this._engine         = null;
     this._commandFactory = null;
     base.SetRunspaceState(RunspaceState.Closed);
     base.RaiseRunspaceStateEvents();
 }
Beispiel #3
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);
                }
            }
        }