Example #1
0
        private void CoreClose(bool syncCall)
        {
            bool flag = false;

            lock (this.SyncRoot)
            {
                if ((this.RunspaceState == System.Management.Automation.Runspaces.RunspaceState.Closed) || (this.RunspaceState == System.Management.Automation.Runspaces.RunspaceState.Broken))
                {
                    return;
                }
                if (this.RunspaceState == System.Management.Automation.Runspaces.RunspaceState.BeforeOpen)
                {
                    this.SetRunspaceState(System.Management.Automation.Runspaces.RunspaceState.Closing, null);
                    this.SetRunspaceState(System.Management.Automation.Runspaces.RunspaceState.Closed, null);
                    this.RaiseRunspaceStateEvents();
                    return;
                }
                if (this.RunspaceState == System.Management.Automation.Runspaces.RunspaceState.Opening)
                {
                    Monitor.Exit(this.SyncRoot);
                    try
                    {
                        this.RunspaceOpening.Wait();
                    }
                    finally
                    {
                        Monitor.Enter(this.SyncRoot);
                    }
                }
                if (this._bSessionStateProxyCallInProgress)
                {
                    throw PSTraceSource.NewInvalidOperationException("RunspaceStrings", "RunspaceCloseInvalidWhileSessionStateProxy", new object[0]);
                }
                if (this.RunspaceState == System.Management.Automation.Runspaces.RunspaceState.Closing)
                {
                    flag = true;
                }
                else
                {
                    if (this.RunspaceState != System.Management.Automation.Runspaces.RunspaceState.Opened)
                    {
                        InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.RunspaceNotInOpenedState, this.RunspaceState.ToString()), this.RunspaceState, System.Management.Automation.Runspaces.RunspaceState.Opened);
                        throw exception;
                    }
                    this.SetRunspaceState(System.Management.Automation.Runspaces.RunspaceState.Closing);
                }
            }
            if (!flag)
            {
                this.RaiseRunspaceStateEvents();
                PSSQMAPI.NoteRunspaceEnd(base.InstanceId);
                this.CloseHelper(syncCall);
            }
            else if (syncCall)
            {
                this.WaitForFinishofPipelines();
            }
        }
Example #2
0
        /// <summary>
        /// Converts the current to an InvalidRunspaceStateException.
        /// </summary>
        internal InvalidRunspaceStateException ToInvalidRunspaceStateException()
        {
            InvalidRunspaceStateException exception = new InvalidRunspaceStateException(
                RunspaceStrings.InvalidRunspaceStateGeneral,
                this);

            exception.CurrentState  = RunspacePoolStateToRunspaceState(this.CurrentState);
            exception.ExpectedState = RunspacePoolStateToRunspaceState(this.ExpectedState);
            return(exception);
        }
Example #3
0
 internal void AddToRunningPipelineList(PipelineBase pipeline)
 {
     lock (this._runningPipelines.SyncRoot)
     {
         if (!this._bypassRunspaceStateCheck && (this.RunspaceState != System.Management.Automation.Runspaces.RunspaceState.Opened))
         {
             InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipeline, this.RunspaceState.ToString()), this.RunspaceState, System.Management.Automation.Runspaces.RunspaceState.Opened);
             throw exception;
         }
         this._runningPipelines.Add(pipeline);
         this.currentlyRunningPipeline = pipeline;
     }
 }
Example #4
0
 private void CoreOpen(bool syncCall)
 {
     lock (this.SyncRoot)
     {
         if (this.RunspaceState != System.Management.Automation.Runspaces.RunspaceState.BeforeOpen)
         {
             InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.CannotOpenAgain, new object[] { this.RunspaceState.ToString() }), this.RunspaceState, System.Management.Automation.Runspaces.RunspaceState.BeforeOpen);
             throw exception;
         }
         this.SetRunspaceState(System.Management.Automation.Runspaces.RunspaceState.Opening);
     }
     this.RaiseRunspaceStateEvents();
     PSSQMAPI.NoteRunspaceStart(base.InstanceId);
     this.OpenHelper(syncCall);
 }
Example #5
0
 private void DoConcurrentCheckAndMarkSessionStateProxyCallInProgress()
 {
     lock (this._syncRoot)
     {
         if (this.RunspaceState != System.Management.Automation.Runspaces.RunspaceState.Opened)
         {
             InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.RunspaceNotInOpenedState, this.RunspaceState.ToString()), this.RunspaceState, System.Management.Automation.Runspaces.RunspaceState.Opened);
             throw exception;
         }
         if (this._bSessionStateProxyCallInProgress)
         {
             throw PSTraceSource.NewInvalidOperationException("RunspaceStrings", "AnotherSessionStateProxyInProgress", new object[0]);
         }
         Pipeline currentlyRunningPipeline = this.GetCurrentlyRunningPipeline();
         if (currentlyRunningPipeline != null)
         {
             if ((currentlyRunningPipeline != this.pulsePipeline) && (!currentlyRunningPipeline.IsNested || (this.pulsePipeline == null)))
             {
                 throw PSTraceSource.NewInvalidOperationException("RunspaceStrings", "NoSessionStateProxyWhenPipelineInProgress", new object[0]);
             }
             Monitor.Exit(this._syncRoot);
             try
             {
                 this.WaitForFinishofPipelines();
             }
             finally
             {
                 Monitor.Enter(this._syncRoot);
             }
             this.DoConcurrentCheckAndMarkSessionStateProxyCallInProgress();
         }
         else
         {
             this._bSessionStateProxyCallInProgress = true;
         }
     }
 }
Example #6
0
 private void CoreOpen(bool syncCall)
 {
     lock (this.SyncRoot)
     {
         if (this.RunspaceState != System.Management.Automation.Runspaces.RunspaceState.BeforeOpen)
         {
             InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.CannotOpenAgain, new object[] { this.RunspaceState.ToString() }), this.RunspaceState, System.Management.Automation.Runspaces.RunspaceState.BeforeOpen);
             throw exception;
         }
         this.SetRunspaceState(System.Management.Automation.Runspaces.RunspaceState.Opening);
     }
     this.RaiseRunspaceStateEvents();
     PSSQMAPI.NoteRunspaceStart(base.InstanceId);
     this.OpenHelper(syncCall);
 }
Example #7
0
 private void CoreClose(bool syncCall)
 {
     bool flag = false;
     lock (this.SyncRoot)
     {
         if ((this.RunspaceState == System.Management.Automation.Runspaces.RunspaceState.Closed) || (this.RunspaceState == System.Management.Automation.Runspaces.RunspaceState.Broken))
         {
             return;
         }
         if (this.RunspaceState == System.Management.Automation.Runspaces.RunspaceState.BeforeOpen)
         {
             this.SetRunspaceState(System.Management.Automation.Runspaces.RunspaceState.Closing, null);
             this.SetRunspaceState(System.Management.Automation.Runspaces.RunspaceState.Closed, null);
             this.RaiseRunspaceStateEvents();
             return;
         }
         if (this.RunspaceState == System.Management.Automation.Runspaces.RunspaceState.Opening)
         {
             Monitor.Exit(this.SyncRoot);
             try
             {
                 this.RunspaceOpening.Wait();
             }
             finally
             {
                 Monitor.Enter(this.SyncRoot);
             }
         }
         if (this._bSessionStateProxyCallInProgress)
         {
             throw PSTraceSource.NewInvalidOperationException("RunspaceStrings", "RunspaceCloseInvalidWhileSessionStateProxy", new object[0]);
         }
         if (this.RunspaceState == System.Management.Automation.Runspaces.RunspaceState.Closing)
         {
             flag = true;
         }
         else
         {
             if (this.RunspaceState != System.Management.Automation.Runspaces.RunspaceState.Opened)
             {
                 InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.RunspaceNotInOpenedState, this.RunspaceState.ToString()), this.RunspaceState, System.Management.Automation.Runspaces.RunspaceState.Opened);
                 throw exception;
             }
             this.SetRunspaceState(System.Management.Automation.Runspaces.RunspaceState.Closing);
         }
     }
     if (!flag)
     {
         this.RaiseRunspaceStateEvents();
         PSSQMAPI.NoteRunspaceEnd(base.InstanceId);
         this.CloseHelper(syncCall);
     }
     else if (syncCall)
     {
         this.WaitForFinishofPipelines();
     }
 }
Example #8
0
 internal void AddToRunningPipelineList(PipelineBase pipeline)
 {
     lock (this._runningPipelines.SyncRoot)
     {
         if (!this._bypassRunspaceStateCheck && (this.RunspaceState != System.Management.Automation.Runspaces.RunspaceState.Opened))
         {
             InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipeline, this.RunspaceState.ToString()), this.RunspaceState, System.Management.Automation.Runspaces.RunspaceState.Opened);
             throw exception;
         }
         this._runningPipelines.Add(pipeline);
         this.currentlyRunningPipeline = pipeline;
     }
 }
Example #9
0
        /// <summary>
        /// Add the pipeline to list of pipelines in execution.
        /// </summary>
        /// <param name="pipeline">Pipeline to add to the 
        /// list of pipelines in execution</param>      
        /// 
        /// <exception cref="InvalidRunspaceStateException">
        /// Thrown if the runspace  is not in the Opened state.
        /// <see cref="RunspaceState"/>.
        /// </exception>
        /// 
        /// <exception cref="ArgumentNullException">Thrown if 
        /// <paramref name="pipeline"/> is null.
        /// </exception>
        internal void AddToRunningPipelineList(PipelineBase pipeline)
        {
            Dbg.Assert(pipeline != null, "caller should validate the parameter");

            lock (_pipelineListLock)
            {
                if (ByPassRunspaceStateCheck == false && RunspaceState != RunspaceState.Opened)
                {
                    InvalidRunspaceStateException e =
                        new InvalidRunspaceStateException
                        (
                            StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipeline, RunspaceState.ToString()),
                            RunspaceState,
                            RunspaceState.Opened
                        );
                    throw e;
                }

                //Add the pipeline to list of Executing pipeline.
                //Note:_runningPipelines is always accessed with the lock so
                //there is no need to create a synchronized version of list
                RunningPipelines.Add(pipeline);
                _currentlyRunningPipeline = pipeline;
            }
        }
Example #10
0
        /// <summary>
        /// Close the runspace 
        /// </summary>
        /// <param name="syncCall">If true runspace is closed synchronously
        /// else runspaces is closed asynchronously
        /// </param>
        /// 
        /// <exception cref="InvalidRunspaceStateException">
        /// RunspaceState is BeforeOpen or Opening
        /// </exception>
        /// 
        /// <exception cref="InvalidOperationException">
        /// If SessionStateProxy has some method call in progress
        /// </exception>
        private void CoreClose(bool syncCall)
        {
            bool alreadyClosing = false;

            lock (SyncRoot)
            {
                if (RunspaceState == RunspaceState.Closed ||
                    RunspaceState == RunspaceState.Broken)
                {
                    return;
                }
                else if (RunspaceState == RunspaceState.BeforeOpen)
                {
                    SetRunspaceState(RunspaceState.Closing, null);
                    SetRunspaceState(RunspaceState.Closed, null);

                    RaiseRunspaceStateEvents();

                    return;
                }
                else if (RunspaceState == RunspaceState.Opening)
                {
                    // Wait till the runspace is opened - This is set in DoOpenHelper()
                    // Release the lock before we wait
                    Monitor.Exit(SyncRoot);
                    try
                    {
                        RunspaceOpening.Wait();
                    }
                    finally
                    {
                        // Acquire the lock before we carry on with the rest operations 
                        Monitor.Enter(SyncRoot);
                    }
                }

                if (_bSessionStateProxyCallInProgress)
                {
                    throw PSTraceSource.NewInvalidOperationException(RunspaceStrings.RunspaceCloseInvalidWhileSessionStateProxy);
                }

                if (RunspaceState == RunspaceState.Closing)
                {
                    alreadyClosing = true;
                }
                else
                {
                    if (RunspaceState != RunspaceState.Opened)
                    {
                        InvalidRunspaceStateException e =
                            new InvalidRunspaceStateException
                            (
                                StringUtil.Format(RunspaceStrings.RunspaceNotInOpenedState, RunspaceState.ToString()),
                                RunspaceState,
                                RunspaceState.Opened
                            );
                        throw e;
                    }

                    SetRunspaceState(RunspaceState.Closing);
                }
            }

            if (alreadyClosing)
            {
                //Already closing is set to true if Runspace is already
                //in closing. In this case wait for runspace to close.
                //This can happen in two scenarios:
                //1) User calls Runspace.Close from two threads. 
                //2) In remoting, some error from data structure handler layer can start
                //runspace closure. At the same time, user can call 
                //remove runspace. 
                //

                if (syncCall)
                {
                    WaitForFinishofPipelines();
                }
                return;
            }

            //Raise Event outside the lock
            RaiseRunspaceStateEvents();

            //Call the derived class implementation to do the actual work
            CloseHelper(syncCall);
        }
Example #11
0
 /// <summary>
 /// Asserts if the current state of the runspace is BeforeOpen
 /// </summary>
 private void AssertIfStateIsBeforeOpen()
 {
     lock (_syncRoot)
     {
         //Call fails if RunspaceState is not BeforeOpen. 
         if (_runspaceStateInfo.State != RunspaceState.BeforeOpen)
         {
             InvalidRunspaceStateException e =
                 new InvalidRunspaceStateException
                 (
                     StringUtil.Format(RunspaceStrings.CannotOpenAgain,
                         new object[] { _runspaceStateInfo.State.ToString() }
                     ),
                     _runspaceStateInfo.State,
                     RunspaceState.BeforeOpen
                 );
             throw e;
         }
     }
 }
Example #12
0
 public override Collection<PSObject> Connect()
 {
     Collection<PSObject> collection;
     this.InitPowerShellForConnect(true);
     try
     {
         collection = this._powershell.Connect();
     }
     catch (InvalidRunspacePoolStateException)
     {
         InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipelineConnect, this._runspace.RunspaceStateInfo.State.ToString()), this._runspace.RunspaceStateInfo.State, RunspaceState.Opened);
         throw exception;
     }
     if (((collection.Count == 0) && (this._outputCollection != null)) && (this._outputCollection.Count > 0))
     {
         collection = new Collection<PSObject>(this._outputCollection);
     }
     return collection;
 }
Example #13
0
        /// <summary>
        /// Connects synchronously to a running command on a remote server.
        /// The pipeline object must be in the disconnected state.
        /// </summary>
        /// <returns>A collection of result objects.</returns>
        public override Collection<PSObject> Connect()
        {
            InitPowerShellForConnect(true);

            Collection<PSObject> results;

            try
            {
                results = _powershell.Connect();
            }
            catch (InvalidRunspacePoolStateException)
            {
                InvalidRunspaceStateException e =
                    new InvalidRunspaceStateException
                    (
                        StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipelineConnect, _runspace.RunspaceStateInfo.State.ToString()),
                        _runspace.RunspaceStateInfo.State,
                        RunspaceState.Opened
                    );

                throw e;
            }

            // PowerShell object will return empty results if it was provided an alternative object to 
            // collect output in.  Check to see if the output was collected in a member variable.
            if (results.Count == 0)
            {
                if (_outputCollection != null && _outputCollection.Count > 0)
                {
                    results = new Collection<PSObject>(_outputCollection);
                }
            }

            return results;
        }
Example #14
0
        /// <summary>
        /// Connects asynchronously to a running command on a remote server.
        /// </summary>
        public override void ConnectAsync()
        {
            InitPowerShellForConnect(false);

            try
            {
                _powershell.ConnectAsync();
            }
            catch (InvalidRunspacePoolStateException)
            {
                InvalidRunspaceStateException e =
                    new InvalidRunspaceStateException
                    (
                        StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipelineConnect, _runspace.RunspaceStateInfo.State.ToString()),
                        _runspace.RunspaceStateInfo.State,
                        RunspaceState.Opened
                    );
                throw e;
            }
        }
Example #15
0
        /// <summary>
        /// Invoke the pipeline, synchronously, returning the results as an 
        /// array of objects.
        /// </summary>
        /// <param name="input">an array of input objects to pass to the pipeline.
        /// Array may be empty but may not be null</param>
        /// <returns>An array of zero or more result objects</returns>
        /// <remarks>Caller of synchronous exectute should not close
        /// input objectWriter. Synchronous invoke will always close the input 
        /// objectWriter.
        /// 
        /// On Synchronous Invoke if output is throttled and no one is reading from
        /// output pipe, Execution will block after buffer is full.
        /// </remarks>
        public override Collection<PSObject> Invoke(System.Collections.IEnumerable input)
        {
            if (input == null)
            {
                this.InputStream.Close();
            }
            InitPowerShell(true);

            Collection<PSObject> results;

            try
            {
                results = _powershell.Invoke(input);
            }
            catch (InvalidRunspacePoolStateException)
            {
                InvalidRunspaceStateException e =
                    new InvalidRunspaceStateException
                    (
                        StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipeline, _runspace.RunspaceStateInfo.State.ToString()),
                        _runspace.RunspaceStateInfo.State,
                        RunspaceState.Opened
                    );
                throw e;
            }

            return results;
        }
Example #16
0
 public void ProcessRunspaceException(Runspace runspace, int attempt, InvalidRunspaceStateException e)
 {
     RunspaceStateInfo info = runspace.RunspaceStateInfo;
     if (info != null) {
         LOGGER.TraceEvent(TraceEventType.Warning, CAT_DEFAULT,
             "Runspace is in wrong state. Exception: {0}, State: {1}, Reason: {2}, Attempt number: {3}",
             e, info.State, info.Reason, attempt);
     } else {
         LOGGER.TraceEvent(TraceEventType.Warning, CAT_DEFAULT,
             "Runspace is in wrong state. Exception: {0}, Attempt number: {1}", e, attempt);
     }
     if (attempt == MAX_ATTEMPTS) {
         LOGGER.TraceEvent(TraceEventType.Error, CAT_DEFAULT, "Maximum number of attempts achieved, signalling the exception");
         _runSpacePool.returnFailedRunspace(runspace);
         throw e;
     } else {
         runspace = _runSpacePool.getAnotherRunspace(runspace);
     }
 }
Example #17
0
 /// <summary>
 /// Converts the current to an InvalidRunspaceStateException
 /// </summary>
 internal InvalidRunspaceStateException ToInvalidRunspaceStateException()
 {
     InvalidRunspaceStateException exception = new InvalidRunspaceStateException(
         RunspaceStrings.InvalidRunspaceStateGeneral,
         this);
     exception.CurrentState = RunspacePoolStateToRunspaceState(this.CurrentState);
     exception.ExpectedState = RunspacePoolStateToRunspaceState(this.ExpectedState);
     return exception;
 }
Example #18
0
 private void DoConcurrentCheckAndMarkSessionStateProxyCallInProgress()
 {
     lock (this._syncRoot)
     {
         if (this.RunspaceState != System.Management.Automation.Runspaces.RunspaceState.Opened)
         {
             InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.RunspaceNotInOpenedState, this.RunspaceState.ToString()), this.RunspaceState, System.Management.Automation.Runspaces.RunspaceState.Opened);
             throw exception;
         }
         if (this._bSessionStateProxyCallInProgress)
         {
             throw PSTraceSource.NewInvalidOperationException("RunspaceStrings", "AnotherSessionStateProxyInProgress", new object[0]);
         }
         Pipeline currentlyRunningPipeline = this.GetCurrentlyRunningPipeline();
         if (currentlyRunningPipeline != null)
         {
             if ((currentlyRunningPipeline != this.pulsePipeline) && (!currentlyRunningPipeline.IsNested || (this.pulsePipeline == null)))
             {
                 throw PSTraceSource.NewInvalidOperationException("RunspaceStrings", "NoSessionStateProxyWhenPipelineInProgress", new object[0]);
             }
             Monitor.Exit(this._syncRoot);
             try
             {
                 this.WaitForFinishofPipelines();
             }
             finally
             {
                 Monitor.Enter(this._syncRoot);
             }
             this.DoConcurrentCheckAndMarkSessionStateProxyCallInProgress();
         }
         else
         {
             this._bSessionStateProxyCallInProgress = true;
         }
     }
 }
Example #19
0
 internal void AddToRunningPipelineList(RemotePipeline pipeline)
 {
     lock (this._syncRoot)
     {
         if ((!this._bypassRunspaceStateCheck && (this._runspaceStateInfo.State != RunspaceState.Opened)) && (this._runspaceStateInfo.State != RunspaceState.Disconnected))
         {
             InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipeline, this._runspaceStateInfo.State.ToString()), this._runspaceStateInfo.State, RunspaceState.Opened);
             if (this.ConnectionInfo != null)
             {
                 exception.Source = this.ConnectionInfo.ComputerName;
             }
             throw exception;
         }
         this._runningPipelines.Add(pipeline);
     }
 }
Example #20
0
 public override Collection<PSObject> Invoke(IEnumerable input)
 {
     Collection<PSObject> collection;
     if (input == null)
     {
         this.InputStream.Close();
     }
     this.InitPowerShell(true, false);
     try
     {
         collection = this._powershell.Invoke(input);
     }
     catch (InvalidRunspacePoolStateException)
     {
         InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipeline, this._runspace.RunspaceStateInfo.State.ToString()), this._runspace.RunspaceStateInfo.State, RunspaceState.Opened);
         throw exception;
     }
     return collection;
 }
Example #21
0
        /// <summary>
        /// Opens the runspace.
        /// </summary>
        /// <param name="syncCall">If true runspace is opened synchronously
        /// else runspaces is opened asynchronously
        /// </param>
        /// <exception cref="InvalidRunspaceStateException">
        /// RunspaceState is not BeforeOpen
        /// </exception>
        private void CoreOpen(bool syncCall)
        {
            bool etwEnabled = RunspaceEventSource.Log.IsEnabled();
            if (etwEnabled) RunspaceEventSource.Log.OpenRunspaceStart();
            lock (SyncRoot)
            {
                //Call fails if RunspaceState is not BeforeOpen. 
                if (RunspaceState != RunspaceState.BeforeOpen)
                {
                    InvalidRunspaceStateException e =
                        new InvalidRunspaceStateException
                        (
                            StringUtil.Format(RunspaceStrings.CannotOpenAgain, new object[] { RunspaceState.ToString() }),
                            RunspaceState,
                            RunspaceState.BeforeOpen
                        );
                    throw e;
                }

                SetRunspaceState(RunspaceState.Opening);
            }

            //Raise event outside the lock	
            RaiseRunspaceStateEvents();

            OpenHelper(syncCall);
            if (etwEnabled) RunspaceEventSource.Log.OpenRunspaceStop();

            // We report startup telementry when opening the runspace - because this is the first time
            // we are really using PowerShell. This isn't the cleanest place though, because
            // sometimes there are many runspaces created - the callee ensures telemetry is only
            // reported once. Note that if the host implements IHostProvidesTelemetryData, we rely
            // on the host calling ReportStartupTelemetry.
            if (!(this.Host is IHostProvidesTelemetryData))
            {
                TelemetryAPI.ReportStartupTelemetry(null);
            }
        }
Example #22
0
 private void CoreInvokeAsync()
 {
     try
     {
         _powershell.BeginInvoke();
     }
     catch (InvalidRunspacePoolStateException)
     {
         InvalidRunspaceStateException e =
             new InvalidRunspaceStateException
             (
                 StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipeline, _runspace.RunspaceStateInfo.State.ToString()),
                 _runspace.RunspaceStateInfo.State,
                 RunspaceState.Opened
             );
         throw e;
     }
 }
Example #23
0
        /// <summary>
        /// Add the pipeline to list of pipelines in execution.
        /// </summary>
        /// <param name="pipeline">Pipeline to add to the 
        /// list of pipelines in execution</param>      
        /// 
        /// <exception cref="InvalidRunspaceStateException">
        /// Thrown if the runspace  is not in the Opened state.
        /// <see cref="RunspaceState"/>.
        /// </exception>
        /// 
        /// <exception cref="ArgumentNullException">Thrown if 
        /// <paramref name="pipeline"/> is null.
        /// </exception>
        internal void AddToRunningPipelineList(RemotePipeline pipeline)
        {
            Dbg.Assert(pipeline != null, "caller should validate the parameter");

            lock (_syncRoot)
            {
                if (_bypassRunspaceStateCheck == false &&
                    _runspaceStateInfo.State != RunspaceState.Opened &&
                    _runspaceStateInfo.State != RunspaceState.Disconnected) // Disconnected runspaces can have running pipelines.
                {
                    InvalidRunspaceStateException e =
                        new InvalidRunspaceStateException
                        (
                            StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipeline,
                                _runspaceStateInfo.State.ToString()
                            ),
                            _runspaceStateInfo.State,
                            RunspaceState.Opened
                        );
                    if (this.ConnectionInfo != null)
                    {
                        e.Source = this.ConnectionInfo.ComputerName;
                    }
                    throw e;
                }

                //Add the pipeline to list of Executing pipeline.
                //Note:_runningPipelines is always accessed with the lock so
                //there is no need to create a synchronized version of list
                _runningPipelines.Add(pipeline);
            }
        }
Example #24
0
        /// <summary>
        /// This method ensures that SessionStateProxy call is allowed and if
        /// allowed it sets a variable to disallow further SessionStateProxy or
        /// pipeline calls.
        /// </summary>
        private void DoConcurrentCheckAndMarkSessionStateProxyCallInProgress()
        {
            lock (SyncRoot)
            {
                if (RunspaceState != RunspaceState.Opened)
                {
                    InvalidRunspaceStateException e =
                        new InvalidRunspaceStateException
                        (
                            StringUtil.Format(RunspaceStrings.RunspaceNotInOpenedState, RunspaceState.ToString()),
                            RunspaceState,
                            RunspaceState.Opened
                        );
                    throw e;
                }

                if (_bSessionStateProxyCallInProgress == true)
                {
                    throw PSTraceSource.NewInvalidOperationException(RunspaceStrings.AnotherSessionStateProxyInProgress);
                }

                Pipeline runningPipeline = GetCurrentlyRunningPipeline();
                if (runningPipeline != null)
                {
                    // Detect if we're running an engine pulse, or we're running a nested pipeline
                    // from an engine pulse
                    if (runningPipeline == PulsePipeline ||
                        (runningPipeline.IsNested && PulsePipeline != null))
                    {
                        // If so, wait and try again
                        // Release the lock before we wait for the pulse pipelines
                        Monitor.Exit(SyncRoot);

                        try
                        {
                            WaitForFinishofPipelines();
                        }
                        finally
                        {
                            // Acquire the lock before we carry on with the rest operations
                            Monitor.Enter(SyncRoot);
                        }

                        DoConcurrentCheckAndMarkSessionStateProxyCallInProgress();
                        return;
                    }
                    else
                    {
                        throw PSTraceSource.NewInvalidOperationException(RunspaceStrings.NoSessionStateProxyWhenPipelineInProgress);
                    }
                }

                //Now we can invoke session state proxy
                _bSessionStateProxyCallInProgress = true;
            }
        }
Example #25
0
 private void AssertIfStateIsBeforeOpen()
 {
     lock (this._syncRoot)
     {
         if (this._runspaceStateInfo.State != RunspaceState.BeforeOpen)
         {
             InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.CannotOpenAgain, new object[] { this._runspaceStateInfo.State.ToString() }), this._runspaceStateInfo.State, RunspaceState.BeforeOpen);
             throw exception;
         }
     }
 }