Beispiel #1
0
        private void InitCommon()
        {
            // set the metadata
            this.Command.CommandInfo = this.CommandInfo;

            // set the ObsoleteAttribute of the current command
            _obsoleteAttribute = this.CommandInfo.CommandMetadata.Obsolete;

            // set the execution context
            this.Command.Context = this._context;

            // Now set up the command runtime for this command.
            try
            {
                this.commandRuntime         = new MshCommandRuntime(_context, this.CommandInfo, this.Command);
                this.Command.commandRuntime = this.commandRuntime;
            }
            catch (Exception e) // Catch-all OK, 3rd party callout.
            {
                // Log a command health event

                MshLog.LogCommandHealthEvent(
                    this._context,
                    e,
                    Severity.Warning);

                throw;
            }
        }
Beispiel #2
0
        private void Init(CmdletInfo cmdletInformation)
        {
            Cmdlet    cmdlet               = (Cmdlet)null;
            Exception exception            = (Exception)null;
            string    errorIdAndResourceId = "CmdletNotFoundException";

            try
            {
                cmdlet = (Cmdlet)Activator.CreateInstance(cmdletInformation.ImplementingType);
            }
            catch (TargetInvocationException ex)
            {
                CommandProcessor.tracer.TraceException((Exception)ex);
                CmdletInvocationException invocationException = new CmdletInvocationException(ex.InnerException, (InvocationInfo)null);
                MshLog.LogCommandHealthEvent(this.context, (Exception)invocationException, Severity.Warning);
                throw invocationException;
            }
            catch (MemberAccessException ex)
            {
                exception = (Exception)ex;
            }
            catch (TypeLoadException ex)
            {
                exception = (Exception)ex;
            }
            catch (InvalidCastException ex)
            {
                exception            = (Exception)ex;
                errorIdAndResourceId = "CmdletDoesNotDeriveFromCmdletType";
            }
            catch (Exception ex)
            {
                CommandProcessorBase.CheckForSevereException(ex);
                CommandProcessor.tracer.TraceException(ex);
                MshLog.LogCommandHealthEvent(this.context, ex, Severity.Warning);
                throw;
            }
            if (exception != null)
            {
                CommandProcessor.tracer.TraceException(exception);
                MshLog.LogCommandHealthEvent(this.context, exception, Severity.Warning);
                CommandNotFoundException notFoundException = new CommandNotFoundException(cmdletInformation.Name, exception, errorIdAndResourceId, new object[1]
                {
                    (object)exception.Message
                });
                CommandProcessor.tracer.TraceException((Exception)notFoundException);
                throw notFoundException;
            }
            this.Command = (InternalCommand)cmdlet;
            this.InitCommon();
        }
Beispiel #3
0
 private void InitCommon()
 {
     base.Command.CommandInfo = base.CommandInfo;
     base.Command.Context     = base._context;
     try
     {
         base.commandRuntime         = new MshCommandRuntime(base._context, base.CommandInfo, base.Command);
         base.Command.commandRuntime = base.commandRuntime;
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         MshLog.LogCommandHealthEvent(base._context, exception, Severity.Warning);
         throw;
     }
 }
Beispiel #4
0
 private void InitCommon()
 {
     this.Command.CommandInfo = this.CommandInfo;
     this.Command.Context     = this.context;
     try
     {
         this.commandRuntime         = new MshCommandRuntime(this.context, this.CommandInfo, this.Command);
         this.Command.commandRuntime = (ICommandRuntime)this.commandRuntime;
     }
     catch (Exception ex)
     {
         CommandProcessorBase.CheckForSevereException(ex);
         CommandProcessor.tracer.TraceException(ex);
         MshLog.LogCommandHealthEvent(this.context, ex, Severity.Warning);
         throw;
     }
 }
Beispiel #5
0
 internal static void CheckForSevereException(Exception e)
 {
     if ((e is AccessViolationException) || (e is StackOverflowException))
     {
         try
         {
             if (!alreadyFailing)
             {
                 alreadyFailing = true;
                 MshLog.LogCommandHealthEvent(LocalPipeline.GetExecutionContextFromTLS(), e, Severity.Critical);
             }
         }
         finally
         {
             WindowsErrorReporting.FailFast(e);
         }
     }
 }
Beispiel #6
0
        private void Init(CmdletInfo cmdletInformation)
        {
            Cmdlet    cmdlet    = null;
            Exception exception = null;
            string    str       = null;
            string    cmdletDoesNotDeriveFromCmdletType = null;

            try
            {
                cmdlet = ConstructInstance(cmdletInformation.ImplementingType);
                if (cmdlet == null)
                {
                    exception = new InvalidCastException();
                    str       = "CmdletDoesNotDeriveFromCmdletType";
                    cmdletDoesNotDeriveFromCmdletType = DiscoveryExceptions.CmdletDoesNotDeriveFromCmdletType;
                }
            }
            catch (MemberAccessException exception2)
            {
                exception = exception2;
            }
            catch (TypeLoadException exception3)
            {
                exception = exception3;
            }
            catch (Exception exception4)
            {
                CommandProcessorBase.CheckForSevereException(exception4);
                CmdletInvocationException exception5 = new CmdletInvocationException(exception4, null);
                MshLog.LogCommandHealthEvent(base._context, exception5, Severity.Warning);
                throw exception5;
            }
            if (exception != null)
            {
                MshLog.LogCommandHealthEvent(base._context, exception, Severity.Warning);
                CommandNotFoundException exception6 = new CommandNotFoundException(cmdletInformation.Name, exception, str ?? "CmdletNotFoundException", cmdletDoesNotDeriveFromCmdletType ?? DiscoveryExceptions.CmdletNotFoundException, new object[] { exception.Message });
                throw exception6;
            }
            base.Command      = cmdlet;
            base.CommandScope = base.Context.EngineSessionState.CurrentScope;
            this.InitCommon();
        }
        // Keep in sync:
        // S.M.A.CommandProcessorBase.CheckForSevereException
        // S.M.A.Internal.ConsoleHost.CheckForSevereException
        // S.M.A.Commands.CommandsCommon.CheckForSevereException
        // S.M.A.Commands.UtilityCommon.CheckForSevereException
        /// <summary>
        /// Checks whether the exception is a severe exception which should
        /// cause immediate process failure.
        /// </summary>
        /// <param name="e"></param>
        /// <remarks>
        /// CB says 02/23/2005: I personally would err on the side
        /// of treating OOM like an application exception, rather than
        /// a critical system failure.I think this will be easier to justify
        /// in Orcas, if we tease apart the two cases of OOM better.
        /// But even in Whidbey, how likely is it that we couldnt JIT
        /// some backout code?  At that point, the process or possibly
        /// the machine is likely to stop executing soon no matter
        /// what you do in this routine.  So I would just consider
        /// AccessViolationException.  (I understand why you have SO here,
        /// at least temporarily).
        /// </remarks>
        internal static void CheckForSevereException(Exception e)
        {
            if (e is AccessViolationException || e is StackOverflowException)
            {
                try
                {
                    if (!alreadyFailing)
                    {
                        alreadyFailing = true;

                        // Get the ExecutionContext from the thread.
                        ExecutionContext context = Runspaces.LocalPipeline.GetExecutionContextFromTLS();

                        // Log a command health event for this critical error.
                        MshLog.LogCommandHealthEvent(context, e, Severity.Critical);
                    }
                }
                finally
                {
                    WindowsErrorReporting.FailFast(e);
                }
            }
        }
Beispiel #8
0
 internal static void ShouldRun(
     ExecutionContext context,
     PSHost host,
     CommandInfo commandInfo,
     CommandOrigin commandOrigin)
 {
     try
     {
         if (commandOrigin == CommandOrigin.Runspace && commandInfo.Visibility != SessionStateEntryVisibility.Public)
         {
             CommandNotFoundException notFoundException = new CommandNotFoundException(commandInfo.Name, (Exception)null, "CommandNotFoundException", new object[0]);
             CommandDiscovery.tracer.TraceException((Exception)notFoundException);
             throw notFoundException;
         }
         context.AuthorizationManager.ShouldRunInternal(commandInfo, commandOrigin, host);
     }
     catch (PSSecurityException ex)
     {
         CommandDiscovery.tracer.TraceException((Exception)ex);
         MshLog.LogCommandHealthEvent(context, (Exception)ex, Severity.Warning);
         MshLog.LogCommandLifecycleEvent(context, CommandState.Terminated, commandInfo.Name);
         throw;
     }
 }
Beispiel #9
0
        /// <summary>
        /// Initializes the command's request object.
        /// </summary>
        /// <param name="cmdletInformation">
        /// The information about the cmdlet.
        /// </param>
        /// <exception cref="CmdletInvocationException">
        /// If the constructor for the cmdlet threw an exception.
        /// </exception>
        /// <exception cref="MemberAccessException">
        /// The type referenced by <paramref name="cmdletInformation"/> refered to an
        /// abstract type or them member was invoked via a late-binding mechanism.
        /// </exception>
        /// <exception cref="TypeLoadException">
        /// If <paramref name="cmdletInformation"/> refers to a type that is invalid.
        /// </exception>
        private void Init(CmdletInfo cmdletInformation)
        {
            Diagnostics.Assert(cmdletInformation != null, "Constructor should throw exception if LookupCommand returned null.");

            Cmdlet    newCmdlet            = null;
            Exception initError            = null;
            string    errorIdAndResourceId = null;
            string    resourceStr          = null;

            try
            {
                // Create the request object
                newCmdlet = ConstructInstance(cmdletInformation.ImplementingType);
                if (newCmdlet == null)
                {
                    // We could test the inheritance before constructing, but that's
                    // expensive.  Much cheaper to just check for null.
                    initError            = new InvalidCastException();
                    errorIdAndResourceId = "CmdletDoesNotDeriveFromCmdletType";
                    resourceStr          = DiscoveryExceptions.CmdletDoesNotDeriveFromCmdletType;
                }
            }
            catch (MemberAccessException memberAccessException)
            {
                initError = memberAccessException;
            }
            catch (TypeLoadException typeLoadException)
            {
                initError = typeLoadException;
            }
            catch (Exception e) // Catch-all OK, 3rd party callout.
            {
                // We don't have a Command or InvocationInfo at this point,
                // since the command failed to initialize.
                var commandException = new CmdletInvocationException(e, null);

                // Log a command health event
                MshLog.LogCommandHealthEvent(
                    this._context,
                    commandException,
                    Severity.Warning);

                throw commandException;
            }

            if (initError != null)
            {
                // Log a command health event
                MshLog.LogCommandHealthEvent(
                    this._context,
                    initError,
                    Severity.Warning);

                CommandNotFoundException exception =
                    new CommandNotFoundException(
                        cmdletInformation.Name,
                        initError,
                        errorIdAndResourceId ?? "CmdletNotFoundException",
                        resourceStr ?? DiscoveryExceptions.CmdletNotFoundException,
                        initError.Message);
                throw exception;
            }

            this.Command      = newCmdlet;
            this.CommandScope = Context.EngineSessionState.CurrentScope;

            InitCommon();
        }