public static Runspace CreateOutOfProcessRunspace(TypeTable typeTable, PowerShellProcessInstance processInstance) { NewProcessConnectionInfo connectionInfo = new NewProcessConnectionInfo(null) { Process = processInstance }; return CreateRunspace(connectionInfo, null, typeTable); }
/// <summary> /// </summary> /// <param name="typeTable"></param> /// <param name="processInstance"></param> /// <returns></returns> public static Runspace CreateOutOfProcessRunspace(TypeTable typeTable, PowerShellProcessInstance processInstance) { NewProcessConnectionInfo connectionInfo = new NewProcessConnectionInfo(null) { Process = processInstance }; return(CreateRunspace(connectionInfo, null, typeTable)); }
internal ActivityHostProcess(int activityHostTimeoutSec, PSLanguageMode? languageMode) { _languageMode = languageMode; _useJobIPCProcess = true; _tracer.WriteMessage("BEGIN Creating new PowerShell process instance"); _processInstance = new PowerShellProcessInstance(); _tracer.WriteMessage("END Creating new PowerShell process instance "); _runspace = CreateRunspace(); _tracer.WriteMessage("New runspace created ", _runspace.InstanceId.ToString()); _timer = new Timer {AutoReset = false, Interval = TimeOut}; _timer.Elapsed += TimerElapsed; _timer.Interval = activityHostTimeoutSec > 0 ? activityHostTimeoutSec*1000 : TimeOut; _perfCountersMgr.UpdateCounterByValue( PSWorkflowPerformanceCounterSetInfo.CounterSetId, PSWorkflowPerformanceCounterIds.ActivityHostMgrCreatedProcessesCount); }
/// <summary> /// Launch a new Process (PowerShell.exe -s) to perform remoting. This is used by *-Job cmdlets /// to support background jobs without depending on WinRM (WinRM has complex requirements like /// elevation to support local machine remoting) /// </summary> /// <exception cref="System.InvalidOperationException"> /// </exception> /// <exception cref="System.ComponentModel.Win32Exception"> /// 1. There was an error in opening the associated file. /// </exception> internal override void CreateAsync() { if (null != _connectionInfo) { _processInstance = _connectionInfo.Process ?? new PowerShellProcessInstance(_connectionInfo.PSVersion, _connectionInfo.Credential, _connectionInfo.InitializationScript, _connectionInfo.RunAs32); if (_connectionInfo.Process != null) { _processCreated = false; } // _processInstance.Start(); } PSEtwLog.LogAnalyticInformational(PSEventId.WSManCreateShell, PSOpcode.Connect, PSTask.CreateRunspace, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, RunspacePoolInstanceId.ToString()); try { lock (syncObject) { if (isClosed) { return; } // Attach handlers and start the process _serverProcess = _processInstance.Process; if (_processInstance.RunspacePool != null) { _processInstance.RunspacePool.Close(); _processInstance.RunspacePool.Dispose(); } stdInWriter = _processInstance.StdInWriter; //if (stdInWriter == null) { _serverProcess.OutputDataReceived += new DataReceivedEventHandler(OnOutputDataReceived); _serverProcess.ErrorDataReceived += new DataReceivedEventHandler(OnErrorDataReceived); } _serverProcess.Exited += new EventHandler(OnExited); //serverProcess.Start(); _processInstance.Start(); if (stdInWriter != null) { _serverProcess.CancelErrorRead(); _serverProcess.CancelOutputRead(); } // Start asynchronous reading of output/errors _serverProcess.BeginOutputReadLine(); _serverProcess.BeginErrorReadLine(); stdInWriter = new OutOfProcessTextWriter(_serverProcess.StandardInput); _processInstance.StdInWriter = stdInWriter; } } catch (System.ComponentModel.Win32Exception w32e) { PSRemotingTransportException psrte = new PSRemotingTransportException(w32e, RemotingErrorIdStrings.IPCExceptionLaunchingProcess, w32e.Message); psrte.ErrorCode = w32e.HResult; TransportErrorOccuredEventArgs eventargs = new TransportErrorOccuredEventArgs(psrte, TransportMethodEnum.CreateShellEx); RaiseErrorHandler(eventargs); return; } catch (Exception e) { CommandProcessorBase.CheckForSevereException(e); PSRemotingTransportException psrte = new PSRemotingTransportException(PSRemotingErrorId.IPCExceptionLaunchingProcess, RemotingErrorIdStrings.IPCExceptionLaunchingProcess, e.Message); TransportErrorOccuredEventArgs eventargs = new TransportErrorOccuredEventArgs(psrte, TransportMethodEnum.CreateShellEx); RaiseErrorHandler(eventargs); return; } // Send one fragment SendOneItem(); }
internal ActivityHostProcess(int activityHostTimeoutSec, PSLanguageMode? languageMode) { object obj; this._syncObject = new object(); this._tracer = PowerShellTraceSourceFactory.GetTraceSource(); this._languageMode = languageMode; this._useJobIPCProcess = true; this._tracer.WriteMessage("BEGIN Creating new PowerShell process instance"); this._processInstance = new PowerShellProcessInstance(); this._tracer.WriteMessage("END Creating new PowerShell process instance "); this._runspace = this.CreateRunspace(); Guid instanceId = this._runspace.InstanceId; this._tracer.WriteMessage("New runspace created ", instanceId.ToString()); Timer timer = new Timer(); timer.AutoReset = false; timer.Interval = 300000; this._timer = timer; this._timer.Elapsed += new ElapsedEventHandler(this.TimerElapsed); Timer timer1 = this._timer; if (activityHostTimeoutSec > 0) { obj = activityHostTimeoutSec * 0x3e8; } else { obj = 0x493e0; } timer1.Interval = (double)obj; ActivityHostProcess._perfCountersMgr.UpdateCounterByValue(PSWorkflowPerformanceCounterSetInfo.CounterSetId, 21, (long)1, true); }
internal override void CreateAsync() { if (this.connectionInfo != null) { this._processInstance = this.connectionInfo.Process ?? new PowerShellProcessInstance(this.connectionInfo.PSVersion, this.connectionInfo.Credential, this.connectionInfo.InitializationScript, this.connectionInfo.RunAs32); if (this.connectionInfo.Process != null) { this._processCreated = false; } } PSEtwLog.LogAnalyticInformational(PSEventId.WSManCreateShell, PSOpcode.Connect, PSTask.CreateRunspace, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic, new object[] { base.RunspacePoolInstanceId.ToString() }); try { lock (base.syncObject) { if (base.isClosed) { return; } this.serverProcess = this._processInstance.Process; if (this._processInstance.RunspacePool != null) { this._processInstance.RunspacePool.Close(); this._processInstance.RunspacePool.Dispose(); } this.stdInWriter = this._processInstance.StdInWriter; this.serverProcess.OutputDataReceived += new DataReceivedEventHandler(this.OnOutputDataReceived); this.serverProcess.ErrorDataReceived += new DataReceivedEventHandler(this.OnErrorDataReceived); this.serverProcess.Exited += new EventHandler(this.OnExited); this._processInstance.Start(); if (this.stdInWriter != null) { this.serverProcess.CancelErrorRead(); this.serverProcess.CancelOutputRead(); } this.serverProcess.BeginOutputReadLine(); this.serverProcess.BeginErrorReadLine(); this.stdInWriter = new OutOfProcessTextWriter(this.serverProcess.StandardInput); this._processInstance.StdInWriter = this.stdInWriter; } } catch (Win32Exception exception) { PSRemotingTransportException e = new PSRemotingTransportException(exception, RemotingErrorIdStrings.IPCExceptionLaunchingProcess, new object[] { exception.Message }) { ErrorCode = exception.ErrorCode }; TransportErrorOccuredEventArgs eventArgs = new TransportErrorOccuredEventArgs(e, TransportMethodEnum.CreateShellEx); this.RaiseErrorHandler(eventArgs); return; } catch (Exception exception3) { CommandProcessorBase.CheckForSevereException(exception3); PSRemotingTransportException exception4 = new PSRemotingTransportException(PSRemotingErrorId.IPCExceptionLaunchingProcess, RemotingErrorIdStrings.IPCExceptionLaunchingProcess, new object[] { exception3.Message }); TransportErrorOccuredEventArgs args2 = new TransportErrorOccuredEventArgs(exception4, TransportMethodEnum.CreateShellEx); this.RaiseErrorHandler(args2); return; } this.SendOneItem(); }