Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
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;
         }
     }
 }
Exemplo n.º 5
0
 private void CoreInvokeAsync()
 {
     try
     {
         this._powershell.BeginInvoke();
     }
     catch (InvalidRunspacePoolStateException)
     {
         InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipeline, this._runspace.RunspaceStateInfo.State.ToString()), this._runspace.RunspaceStateInfo.State, RunspaceState.Opened);
         throw exception;
     }
 }
Exemplo n.º 6
0
 public override void ConnectAsync()
 {
     this.InitPowerShellForConnect(false);
     try
     {
         this._powershell.ConnectAsync();
     }
     catch (InvalidRunspacePoolStateException)
     {
         InvalidRunspaceStateException exception = new InvalidRunspaceStateException(StringUtil.Format(RunspaceStrings.RunspaceNotOpenForPipelineConnect, this._runspace.RunspaceStateInfo.State.ToString()), this._runspace.RunspaceStateInfo.State, RunspaceState.Opened);
         throw exception;
     }
 }
Exemplo n.º 7
0
 public override void InvokeAsync()
 {
     this.InitPowerShell(false);
     try
     {
         this._powershell.BeginInvoke();
     }
     catch (InvalidRunspacePoolStateException ex)
     {
         InvalidRunspaceStateException runspaceStateException = new InvalidRunspaceStateException(ResourceManagerCache.FormatResourceString("Runspace", "RunspaceNotOpenForPipeline", (object)this._runspace.RunspaceStateInfo.State.ToString()), this._runspace.RunspaceStateInfo.State, RunspaceState.Opened);
         RemotePipeline._trace.TraceException((Exception)runspaceStateException);
         throw runspaceStateException;
     }
 }
Exemplo n.º 8
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);
     }
 }
Exemplo n.º 9
0
 public override Collection <PSObject> Invoke(IEnumerable input)
 {
     if (input == null)
     {
         this.InputStream.Close();
     }
     this.InitPowerShell(true);
     try
     {
         return(this._powershell.Invoke(input));
     }
     catch (InvalidRunspacePoolStateException ex)
     {
         InvalidRunspaceStateException runspaceStateException = new InvalidRunspaceStateException(ResourceManagerCache.FormatResourceString("Runspace", "RunspaceNotOpenForPipeline", (object)this._runspace.RunspaceStateInfo.State.ToString()), this._runspace.RunspaceStateInfo.State, RunspaceState.Opened);
         RemotePipeline._trace.TraceException((Exception)runspaceStateException);
         throw runspaceStateException;
     }
 }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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);
        }