Ejemplo n.º 1
0
        /// <summary>
        /// Initializes the underlying PowerShell object after verifying that it is
        /// in a state where it can connect to the remote command.
        /// </summary>
        /// <param name="syncCall"></param>
        private void InitPowerShellForConnect(bool syncCall)
        {
            if (_pipelineStateInfo.State != PipelineState.Disconnected)
            {
                throw new InvalidPipelineStateException(StringUtil.Format(PipelineStrings.PipelineNotDisconnected),
                                                        _pipelineStateInfo.State,
                                                        PipelineState.Disconnected);
            }

            // The connect may be from the same Pipeline that disconnected and in this case
            // the Pipeline state already exists.  Or this could be a new Pipeline object
            // (connect reconstruction case) and new state is created.

            // Check to see if this pipeline already exists in the runspace.
            RemotePipeline currentPipeline = (RemotePipeline)((RemoteRunspace)_runspace).GetCurrentlyRunningPipeline();

            if (currentPipeline == null ||
                currentPipeline != null && !ReferenceEquals(currentPipeline, this))
            {
                ((RemoteRunspace)_runspace).DoConcurrentCheckAndAddToRunningPipelines(this, syncCall);
            }

            // Initialize the PowerShell object if it hasn't been initialized before.
            if ((_powershell.RemotePowerShell) == null || !_powershell.RemotePowerShell.Initialized)
            {
                PSInvocationSettings settings = new PSInvocationSettings();
                settings.AddToHistory = _addToHistory;

                _powershell.InitForRemotePipelineConnect(_inputStream, _outputStream, _errorStream, settings, RedirectShellErrorOutputPipe);

                _powershell.RemotePowerShell.HostCallReceived +=
                    new EventHandler <RemoteDataEventArgs <RemoteHostCall> >(HandleHostCallReceived);
            }
        }