Esempio n. 1
0
 private void DisposeCommands()
 {
     this.stopping = true;
     this.FlushLog();
     if (this._commands != null)
     {
         for (int i = 0; i < this._commands.Count; i++)
         {
             CommandProcessorBase base2 = this._commands[i];
             if (base2 != null)
             {
                 try
                 {
                     base2.CommandRuntime.RemoveVariableListsInPipe();
                     base2.Dispose();
                 }
                 catch (Exception exception)
                 {
                     CommandProcessorBase.CheckForSevereException(exception);
                     InvocationInfo myInvocation = null;
                     if (base2.Command != null)
                     {
                         myInvocation = base2.Command.MyInvocation;
                     }
                     ProviderInvocationException innerException = exception as ProviderInvocationException;
                     if (innerException != null)
                     {
                         exception = new CmdletProviderInvocationException(innerException, myInvocation);
                     }
                     else
                     {
                         exception = new CmdletInvocationException(exception, myInvocation);
                         MshLog.LogCommandHealthEvent(base2.Command.Context, exception, Severity.Warning);
                     }
                     this.RecordFailure(exception, base2.Command);
                 }
             }
         }
     }
     this._commands = null;
     if (this._redirectionPipes != null)
     {
         foreach (PipelineProcessor processor in this._redirectionPipes)
         {
             try
             {
                 if (processor != null)
                 {
                     processor.Dispose();
                 }
             }
             catch (Exception exception3)
             {
                 CommandProcessorBase.CheckForSevereException(exception3);
             }
         }
     }
     this._redirectionPipes = null;
 }
Esempio n. 2
0
        /// <summary>
        /// Verifies that actual exception is is not null,
        /// same type as original provided,
        /// has same errorMessage as original provided
        /// </summary>
        public Exception VerifyExceptionType(CmdletInvocationException cmdletExp)
        {
            /// Verify type of returned exception is same expected.
            var actualException = cmdletExp.ErrorRecord.Exception;

            Assert.IsNotNull(actualException);
            Assert.IsInstanceOfType(actualException, this.exceptionType);
            return(actualException);
        }
Esempio n. 3
0
        /// <summary>
        /// Verifies that actual exception is is not null,
        /// same type as original provided,
        /// has same errorMessage as original provided
        /// </summary>
        public void VerifyException(CmdletInvocationException cmdletExp)
        {
            /// Verify type of returned exception is same expected.
            var actualException = this.VerifyExceptionType(cmdletExp);

            /// Verify that returned ErrorCategory is the same as expected.
            Verify.AreEqual(
                this.errorCategory,
                cmdletExp.ErrorRecord.CategoryInfo.Category);

            /// Verify that error message in actualException the same as the original one.
            Verify.AreEqual(this.errorMessage, actualException.Message);
        }
Esempio n. 4
0
        internal IEnumerator <DSResource> InvokeCmdletAsync(System.Management.Automation.PowerShell powerShell, Expression expression, EventHandler <DataAddedEventArgs> dataAddedEventHandler, AsyncCallback executionCompletionCallback, bool noStreamingResponse)
        {
            Tracer tracer = new Tracer();

            this.dataStore = new AsyncDataStore <DSResource>(expression, noStreamingResponse);
            this.output    = new PSDataCollection <PSObject>();
            tracer.CommandInvocationStart(this.cmdletInfo.CmdletName);
            powerShell.Runspace = this.runspace.Item.Runspace;
            powerShell.AddCommand(this.cmdletInfo.CmdletName);
            foreach (string key in this.parameters.Keys)
            {
                if (!this.cmdletInfo.IsSwitch(key))
                {
                    powerShell.AddParameter(key, this.parameters[key]);
                }
                else
                {
                    powerShell.AddParameter(key);
                }
            }
            this.isExecutionCompleted = false;
            using (OperationTracer operationTracer = new OperationTracer(new Action <string>(TraceHelper.Current.CmdletExecutionStart), new Action <string>(TraceHelper.Current.CmdletExecutionEnd), powerShell.Commands.ToTraceMessage()))
            {
                try
                {
                    this.timer.Start();
                    powerShell.Invoke <PSObject>(null, this.output, Utils.GetPSInvocationSettings());
                }
                catch (CommandNotFoundException commandNotFoundException1)
                {
                    CommandNotFoundException commandNotFoundException = commandNotFoundException1;
                    throw new CommandInvocationFailedException(powerShell.Commands.Commands[0].CommandText, commandNotFoundException);
                }
                catch (ParameterBindingException parameterBindingException1)
                {
                    ParameterBindingException parameterBindingException = parameterBindingException1;
                    throw new CommandInvocationFailedException(powerShell.Commands.Commands[0].CommandText, parameterBindingException);
                }
                catch (CmdletInvocationException cmdletInvocationException1)
                {
                    CmdletInvocationException cmdletInvocationException = cmdletInvocationException1;
                    throw new CommandInvocationFailedException(powerShell.Commands.Commands[0].CommandText, cmdletInvocationException);
                }
            }
            return(new BlockingEnumerator <DSResource>(this.dataStore));
        }
Esempio n. 5
0
        /// <summary>
        /// When the command is complete, Command should be disposed.
        /// This enables cmdlets to reliably release file handles etc.
        /// without waiting for garbage collection.
        /// Exceptions occurring while disposing commands are recorded
        /// but not passed through.
        /// </summary>
        private void DisposeCommands()
        {
            // Note that this is not in a lock.
            // We do not make Dispose() wait until StopProcessing()
            // has completed.
            _stopping = true;

            LogToEventLog();

            if (_commands != null)
            {
                for (int i = 0; i < _commands.Count; i++)
                {
                    CommandProcessorBase commandProcessor = _commands[i];
                    if (null != commandProcessor)
                    {
#pragma warning disable 56500
                        // If Dispose throws an exception, record it as a
                        // pipeline failure and continue disposing cmdlets.
                        try
                        {
                            commandProcessor.CommandRuntime.RemoveVariableListsInPipe();
                            commandProcessor.Dispose();
                        }
                        // 2005/04/13-JonN: The only vaguely plausible reason
                        // for a failure here is an exception in Command.Dispose.
                        // As such, this should be covered by the overall
                        // exemption.
                        catch (Exception e) // Catch-all OK, 3rd party callout.
                        {
                            CommandProcessorBase.CheckForSevereException(e);

                            InvocationInfo myInvocation = null;
                            if (null != commandProcessor.Command)
                                myInvocation = commandProcessor.Command.MyInvocation;

                            ProviderInvocationException pie =
                                e as ProviderInvocationException;
                            if (null != pie)
                            {
                                e = new CmdletProviderInvocationException(
                                    pie,
                                    myInvocation);
                            }
                            else
                            {
                                e = new CmdletInvocationException(
                                    e,
                                    myInvocation);

                                // Log a command health event

                                MshLog.LogCommandHealthEvent(
                                    commandProcessor.Command.Context,
                                    e,
                                    Severity.Warning);
                            }

                            RecordFailure(e, commandProcessor.Command);
                        }
#pragma warning restore 56500
                    }
                }
            }

            _commands = null;

            // Now dispose any pipes that were used for redirection...
            if (_redirectionPipes != null)
            {
                foreach (PipelineProcessor redirPipe in _redirectionPipes)
                {
#pragma warning disable 56500
                    // The complicated logic of disposing the commands is taken care
                    // of through recursion, this routine should not be getting any
                    // exceptions...
                    try
                    {
                        if (redirPipe != null)
                        {
                            redirPipe.Dispose();
                        }
                    }
                    catch (Exception e)
                    {
                        CommandProcessorBase.CheckForSevereException(e);
                    }
#pragma warning restore 56500
                }
            }
            _redirectionPipes = null;
        }