private void InitializeCommon(AutomationEngine engine, PSHost hostInterface) { this._engine = engine; if (!_assemblyEventHandlerSet) { lock (lockObject) { if (!_assemblyEventHandlerSet) { AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ExecutionContext.PowerShellAssemblyResolveHandler); _assemblyEventHandlerSet = true; } } } this._debugger = new System.Management.Automation.Debugger(this); this.eventManager = new PSLocalEventManager(this); this.transactionManager = new PSTransactionManager(); this.myHostInterface = hostInterface as System.Management.Automation.Internal.Host.InternalHost; if (this.myHostInterface == null) { this.myHostInterface = new System.Management.Automation.Internal.Host.InternalHost(hostInterface, this); } this._assemblyCache = new Dictionary <string, Assembly>(); this._topLevelSessionState = this._engineSessionState = new SessionStateInternal(this); if (this._authorizationManager == null) { this._authorizationManager = new System.Management.Automation.AuthorizationManager(null); } this._modules = new ModuleIntrinsics(this); }
internal ExecutionContext(AutomationEngine engine, PSHost hostInterface, System.Management.Automation.Runspaces.RunspaceConfiguration runspaceConfiguration) { this.ignoreScriptDebug = true; this.logContextCache = new System.Management.Automation.LogContextCache(); this._questionMarkVariableValue = true; this._runspaceConfiguration = runspaceConfiguration; this._authorizationManager = runspaceConfiguration.AuthorizationManager; this.InitializeCommon(engine, hostInterface); }
private ExecutionContext GetExecutionContext() { CultureInfo currentCulture = CultureInfo.CurrentCulture; PSHost hostInterface = new DefaultHost(currentCulture,currentCulture); RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); InitialSessionState iss = InitialSessionState.CreateDefault2(); AutomationEngine engine = new AutomationEngine(hostInterface, runspaceConfiguration, iss); ExecutionContext executionContext = new ExecutionContext(engine, hostInterface, iss); return executionContext; }
public void TestDrives() { CultureInfo currentCulture = CultureInfo.CurrentCulture; PSHost hostInterface = new DefaultHost(currentCulture,currentCulture); RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); InitialSessionState iss = InitialSessionState.CreateDefault2(); AutomationEngine engine = new AutomationEngine(hostInterface, runspaceConfiguration, iss); ExecutionContext executionContext = new ExecutionContext(engine, hostInterface, iss); SessionStateInternal sessionState = new SessionStateInternal(executionContext); Collection<PSDriveInfo> drives = sessionState.Drives(null); Assert.NotNull(drives); }
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); } } }
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(); }
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); } }