Exemple #1
0
        internal CommandProcessorBase CreateScriptProcessorForMiniShell(
            ExternalScriptInfo scriptInfo,
            bool useLocalScope)
        {
            CommandDiscovery.VerifyPSVersion(scriptInfo);
            if (string.IsNullOrEmpty(scriptInfo.RequiresApplicationID))
            {
                if (scriptInfo.RequiresPSSnapIns != null && scriptInfo.RequiresPSSnapIns.Count > 0)
                {
                    Collection <string>     psSnapinNames     = this.GetPSSnapinNames(scriptInfo.RequiresPSSnapIns);
                    ScriptRequiresException requiresException = new ScriptRequiresException(scriptInfo.Name, psSnapinNames, "ScriptRequiresMissingPSSnapIns");
                    CommandDiscovery.tracer.TraceException((Exception)requiresException);
                    throw requiresException;
                }
                return(CommandDiscovery.CreateCommandProcessorForScript(scriptInfo, this._context, useLocalScope));
            }
            if (string.Equals(this._context.ShellID, scriptInfo.RequiresApplicationID, StringComparison.OrdinalIgnoreCase))
            {
                return(CommandDiscovery.CreateCommandProcessorForScript(scriptInfo, this._context, useLocalScope));
            }
            string pathFromRegistry = CommandDiscovery.GetShellPathFromRegistry(scriptInfo.RequiresApplicationID);
            ScriptRequiresException requiresException1 = new ScriptRequiresException(scriptInfo.Name, scriptInfo.ApplicationIDLineNumber, scriptInfo.RequiresApplicationID, pathFromRegistry, "ScriptRequiresUnmatchedShellId");

            CommandDiscovery.tracer.TraceException((Exception)requiresException1);
            throw requiresException1;
        }
Exemple #2
0
        private CommandProcessorBase CreateScriptProcessorForSingleShell(
            ExternalScriptInfo scriptInfo,
            RunspaceConfigForSingleShell ssRunConfig,
            bool useLocalScope)
        {
            CommandDiscovery.VerifyPSVersion(scriptInfo);
            Collection <PSSnapInNameVersionPair> requiresPsSnapIns = scriptInfo.RequiresPSSnapIns;

            if (requiresPsSnapIns != null)
            {
                Collection <string> missingPSSnapIns = (Collection <string>)null;
                foreach (PSSnapInNameVersionPair PSSnapin in requiresPsSnapIns)
                {
                    Collection <PSSnapInInfo> psSnapIn = ssRunConfig.ConsoleInfo.GetPSSnapIn(PSSnapin.PSSnapInName, false);
                    if (psSnapIn == null || psSnapIn.Count == 0)
                    {
                        if (missingPSSnapIns == null)
                        {
                            missingPSSnapIns = new Collection <string>();
                        }
                        missingPSSnapIns.Add(CommandDiscovery.BuildPSSnapInDisplayName(PSSnapin));
                    }
                    else if (PSSnapin.Version != (Version)null && !CommandDiscovery.AreInstalledRequiresVersionsCompatible(PSSnapin.Version, psSnapIn[0].Version))
                    {
                        if (missingPSSnapIns == null)
                        {
                            missingPSSnapIns = new Collection <string>();
                        }
                        missingPSSnapIns.Add(CommandDiscovery.BuildPSSnapInDisplayName(PSSnapin));
                    }
                }
                if (missingPSSnapIns != null)
                {
                    ScriptRequiresException requiresException = new ScriptRequiresException(scriptInfo.Name, missingPSSnapIns, "ScriptRequiresMissingPSSnapIns");
                    CommandDiscovery.tracer.TraceException((Exception)requiresException);
                    throw requiresException;
                }
            }
            else if (!string.IsNullOrEmpty(scriptInfo.RequiresApplicationID))
            {
                CommandDiscovery.GetShellPathFromRegistry(scriptInfo.RequiresApplicationID);
                ScriptRequiresException requiresException = new ScriptRequiresException(scriptInfo.Name, scriptInfo.ApplicationIDLineNumber, string.Empty, string.Empty, "RequiresShellIDInvalidForSingleShell");
                CommandDiscovery.tracer.TraceException((Exception)requiresException);
                throw requiresException;
            }
            return(CommandDiscovery.CreateCommandProcessorForScript(scriptInfo, this._context, useLocalScope));
        }
Exemple #3
0
        private CommandProcessorBase CreateCommandProcessor(
            out int index,
            ExecutionContext context)
        {
            index = 0;
            CommandProcessorBase commandProcessorBase = (CommandProcessorBase)null;

            if (this._expression != null)
            {
                return((CommandProcessorBase) new ScriptCommandProcessor(ScriptBlock.CreateSynthesized(this._expression, this.NodeToken), context, false));
            }
            bool createScope;
            SessionStateInternal commandSessionState;
            bool   forceSessionState;
            object commandName = this.GetCommandName(out createScope, out index, out commandSessionState, out forceSessionState, context);

            switch (commandName)
            {
            case ScriptBlock scriptblock:
                commandProcessorBase = CommandDiscovery.CreateCommandProcessorForScript(scriptblock, context, createScope);
                if (commandProcessorBase.CommandSessionState == null || forceSessionState)
                {
                    commandProcessorBase.CommandSessionState = commandSessionState;
                    break;
                }
                break;

            case CommandInfo commandInfo:
                commandProcessorBase = context.CommandDiscovery.LookupCommandProcessor(commandInfo, context.EngineSessionState.currentScope.ScopeOrigin, new bool?(false));
                break;

            case string stringParser:
label_7:
                if (string.IsNullOrEmpty(stringParser))
                {
                    throw InterpreterError.NewInterpreterException(commandName, typeof(RuntimeException), (Token)this._elements[0], "BadExpression", (object)((Token)this._elements[0]).TokenText);
                }
                try
                {
                    if (commandSessionState != context.EngineSessionState)
                    {
                        SessionStateInternal engineSessionState = context.EngineSessionState;
                        try
                        {
                            context.EngineSessionState = commandSessionState;
                            commandProcessorBase       = context.CreateCommand(stringParser);
                            break;
                        }
                        finally
                        {
                            context.EngineSessionState = engineSessionState;
                        }
                    }
                    else
                    {
                        commandProcessorBase = context.CreateCommand(stringParser);
                        break;
                    }
                }
                catch (ParseException ex)
                {
                    throw;
                }
                catch (RuntimeException ex)
                {
                    if (ex.ErrorRecord.InvocationInfo == null)
                    {
                        ex.ErrorRecord.SetInvocationInfo(new InvocationInfo((CommandInfo)null, (Token)this._elements[0], context));
                    }
                    throw;
                }

            default:
                stringParser = PSObject.ToStringParser(context, commandName);
                goto label_7;
            }
            if (commandProcessorBase.CommandSessionState == null || forceSessionState)
            {
                commandProcessorBase.CommandSessionState = commandSessionState;
            }
            InternalCommand command = commandProcessorBase.Command;

            command.MyInvocation.ScriptToken = (Token)this._elements[0];
            if (context.IsStrictVersion(2) && this._elements.Count == index + 1 && (this._elements[index - 1] is Token && this._elements[index] is ArrayLiteralNode))
            {
                Token element = (Token)this._elements[index - 1];
                if (element.Script[element.End] == '(')
                {
                    throw InterpreterError.NewInterpreterException((object)null, typeof(RuntimeException), (Token)this._elements[index - 1], "StrictModeFunctionCallWithParens");
                }
            }
            if (createScope)
            {
                switch (command)
                {
                case ScriptCommand _:
                case PSScriptCmdlet _:
                    commandProcessorBase.UseLocalScope = true;
                    commandProcessorBase.CommandScope  = commandProcessorBase.CommandSessionState == null?context.EngineSessionState.NewScope(commandProcessorBase.FromScriptFile) : commandProcessorBase.CommandSessionState.NewScope(commandProcessorBase.FromScriptFile);

                    goto label_27;
                }
            }
            commandProcessorBase.UseLocalScope = false;
            commandProcessorBase.CommandScope  = commandProcessorBase.CommandSessionState.CurrentScope;
label_27:
            commandProcessorBase.Command.CallingToken = (Token)this._elements[0];
            return(commandProcessorBase);
        }
Exemple #4
0
        private static CommandProcessorBase AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, System.Management.Automation.ExecutionContext context)
        {
            object               parameterText;
            IScriptExtent        parameterExtent;
            CommandProcessorBase base2;
            InternalCommand      command;
            string               str3;
            HelpCategory         category;
            CommandAst           ast          = commandBaseAst as CommandAst;
            TokenKind            kind         = (ast != null) ? ast.InvocationOperator : TokenKind.Unknown;
            bool dotSource                    = kind == TokenKind.Dot;
            SessionStateInternal sessionState = null;
            int          index                = 0;
            PSModuleInfo info                 = PSObject.Base(commandElements[0].ArgumentValue) as PSModuleInfo;

            if (info != null)
            {
                if ((info.ModuleType == ModuleType.Binary) && (info.SessionState == null))
                {
                    throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException), null, "CantInvokeInBinaryModule", ParserStrings.CantInvokeInBinaryModule, new object[] { info.Name });
                }
                if (info.SessionState == null)
                {
                    throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException), null, "CantInvokeInNonImportedModule", ParserStrings.CantInvokeInNonImportedModule, new object[] { info.Name });
                }
                sessionState = info.SessionState.Internal;
                index++;
            }
            CommandParameterInternal internal3 = commandElements[index];

            if (internal3.ParameterNameSpecified)
            {
                parameterText   = internal3.ParameterText;
                parameterExtent = internal3.ParameterExtent;
                if (!internal3.ArgumentSpecified)
                {
                }
            }
            else
            {
                parameterText   = PSObject.Base(internal3.ArgumentValue);
                parameterExtent = internal3.ArgumentExtent;
            }
            string      str         = dotSource ? "." : ((kind == TokenKind.Ampersand) ? "&" : null);
            ScriptBlock scriptblock = parameterText as ScriptBlock;

            if (scriptblock != null)
            {
                base2 = CommandDiscovery.CreateCommandProcessorForScript(scriptblock, context, !dotSource, sessionState);
            }
            else
            {
                CommandInfo commandInfo = parameterText as CommandInfo;
                if (commandInfo != null)
                {
                    base2 = context.CommandDiscovery.LookupCommandProcessor(commandInfo, context.EngineSessionState.CurrentScope.ScopeOrigin, new bool?(!dotSource), sessionState);
                }
                else
                {
                    string str2 = (parameterText as string) ?? PSObject.ToStringParser(context, parameterText);
                    str = str ?? str2;
                    if (string.IsNullOrEmpty(str2))
                    {
                        throw InterpreterError.NewInterpreterException(parameterText, typeof(RuntimeException), parameterExtent, "BadExpression", ParserStrings.BadExpression, new object[] { dotSource ? "." : "&" });
                    }
                    try
                    {
                        if (sessionState != null)
                        {
                            SessionStateInternal engineSessionState = context.EngineSessionState;
                            try
                            {
                                context.EngineSessionState = sessionState;
                                base2 = context.CreateCommand(str2, dotSource);
                                goto Label_025D;
                            }
                            finally
                            {
                                context.EngineSessionState = engineSessionState;
                            }
                        }
                        base2 = context.CreateCommand(str2, dotSource);
                    }

                    catch (RuntimeException exception)
                    {
                        if (exception.ErrorRecord.InvocationInfo == null)
                        {
                            InvocationInfo invocationInfo = new InvocationInfo(null, parameterExtent, context)
                            {
                                InvocationName = str
                            };
                            exception.ErrorRecord.SetInvocationInfo(invocationInfo);
                        }
                        throw;
                    }
                }
            }
Label_025D:
            command             = base2.Command;
            base2.UseLocalScope = !dotSource && ((command is ScriptCommand) || (command is PSScriptCmdlet));
            bool flag2 = base2 is NativeCommandProcessor;

            for (int i = index + 1; i < commandElements.Length; i++)
            {
                CommandParameterInternal parameter = commandElements[i];
                if ((!parameter.ParameterNameSpecified || !parameter.ParameterName.Equals("-", StringComparison.OrdinalIgnoreCase)) || flag2)
                {
                    if (parameter.ArgumentSplatted)
                    {
                        foreach (CommandParameterInternal internal6 in Splat(parameter.ArgumentValue, parameter.ArgumentExtent))
                        {
                            base2.AddParameter(internal6);
                        }
                    }
                    else
                    {
                        base2.AddParameter(parameter);
                    }
                }
            }
            if (base2.IsHelpRequested(out str3, out category))
            {
                base2 = CommandProcessorBase.CreateGetHelpCommandProcessor(context, str3, category);
            }
            base2.Command.InvocationExtent            = commandBaseAst.Extent;
            base2.Command.MyInvocation.ScriptPosition = commandBaseAst.Extent;
            base2.Command.MyInvocation.InvocationName = str;
            pipe.Add(base2);
            bool flag3 = false;
            bool flag4 = false;
            bool flag5 = false;
            bool flag6 = false;

            if (redirections != null)
            {
                foreach (CommandRedirection redirection in redirections)
                {
                    redirection.Bind(pipe, base2, context);
                    switch (redirection.FromStream)
                    {
                    case RedirectionStream.All:
                        flag3 = true;
                        flag4 = true;
                        flag5 = true;
                        flag6 = true;
                        break;

                    case RedirectionStream.Error:
                        flag3 = true;
                        break;

                    case RedirectionStream.Warning:
                        flag4 = true;
                        break;

                    case RedirectionStream.Verbose:
                        flag5 = true;
                        break;

                    case RedirectionStream.Debug:
                        flag6 = true;
                        break;
                    }
                }
            }
            if (!flag3)
            {
                if (context.ShellFunctionErrorOutputPipe != null)
                {
                    base2.CommandRuntime.ErrorOutputPipe = context.ShellFunctionErrorOutputPipe;
                }
                else
                {
                    base2.CommandRuntime.ErrorOutputPipe.ExternalWriter = context.ExternalErrorOutput;
                }
            }
            if (!flag4 && (context.ExpressionWarningOutputPipe != null))
            {
                base2.CommandRuntime.WarningOutputPipe = context.ExpressionWarningOutputPipe;
                flag4 = true;
            }
            if (!flag5 && (context.ExpressionVerboseOutputPipe != null))
            {
                base2.CommandRuntime.VerboseOutputPipe = context.ExpressionVerboseOutputPipe;
                flag5 = true;
            }
            if (!flag6 && (context.ExpressionDebugOutputPipe != null))
            {
                base2.CommandRuntime.DebugOutputPipe = context.ExpressionDebugOutputPipe;
                flag6 = true;
            }
            if ((context.CurrentCommandProcessor != null) && (context.CurrentCommandProcessor.CommandRuntime != null))
            {
                if (!flag4 && (context.CurrentCommandProcessor.CommandRuntime.WarningOutputPipe != null))
                {
                    base2.CommandRuntime.WarningOutputPipe = context.CurrentCommandProcessor.CommandRuntime.WarningOutputPipe;
                }
                if (!flag5 && (context.CurrentCommandProcessor.CommandRuntime.VerboseOutputPipe != null))
                {
                    base2.CommandRuntime.VerboseOutputPipe = context.CurrentCommandProcessor.CommandRuntime.VerboseOutputPipe;
                }
                if (!flag6 && (context.CurrentCommandProcessor.CommandRuntime.DebugOutputPipe != null))
                {
                    base2.CommandRuntime.DebugOutputPipe = context.CurrentCommandProcessor.CommandRuntime.DebugOutputPipe;
                }
            }
            return(base2);
        }
Exemple #5
0
        internal CommandProcessorBase LookupCommandProcessor(
            CommandInfo commandInfo,
            CommandOrigin commandOrigin,
            bool?useLocalScope)
        {
            if (commandInfo.CommandType == CommandTypes.Alias && (commandOrigin == CommandOrigin.Internal || commandInfo.Visibility == SessionStateEntryVisibility.Public))
            {
                AliasInfo aliasInfo = (AliasInfo)commandInfo;
                commandInfo = aliasInfo.ResolvedCommand;
                if (commandInfo == null)
                {
                    CommandNotFoundException notFoundException = new CommandNotFoundException(aliasInfo.Name, (Exception)null, "AliasNotResolvedException", new object[1]
                    {
                        (object)aliasInfo.UnresolvedCommandName
                    });
                    CommandDiscovery.tracer.TraceException((Exception)notFoundException);
                    throw notFoundException;
                }
                PSSQMAPI.IncrementData(CommandTypes.Alias);
            }
            CommandDiscovery.ShouldRun(this._context, (PSHost)this._context.EngineHostInterface, commandInfo, commandOrigin);
            CommandProcessorBase commandProcessorBase;

            switch (commandInfo.CommandType)
            {
            case CommandTypes.Function:
            case CommandTypes.Filter:
                FunctionInfo     functionInfo = (FunctionInfo)commandInfo;
                ExecutionContext context1     = this._context;
                bool?            nullable1    = useLocalScope;
                int num1 = nullable1.HasValue ? (nullable1.GetValueOrDefault() ? 1 : 0) : 1;
                commandProcessorBase = CommandDiscovery.CreateCommandProcessorForScript(functionInfo, context1, num1 != 0);
                break;

            case CommandTypes.Cmdlet:
                commandProcessorBase = (CommandProcessorBase) new CommandProcessor((CmdletInfo)commandInfo, this._context);
                break;

            case CommandTypes.ExternalScript:
                ExternalScriptInfo externalScriptInfo = (ExternalScriptInfo)commandInfo;
                externalScriptInfo.SignatureChecked = true;
                try
                {
                    if (!this._context.IsSingleShell)
                    {
                        ExternalScriptInfo scriptInfo = externalScriptInfo;
                        bool?nullable2 = useLocalScope;
                        int  num2      = nullable2.HasValue ? (nullable2.GetValueOrDefault() ? 1 : 0) : 1;
                        commandProcessorBase = this.CreateScriptProcessorForMiniShell(scriptInfo, num2 != 0);
                        break;
                    }
                    ExternalScriptInfo           scriptInfo1           = externalScriptInfo;
                    RunspaceConfigForSingleShell runspaceConfiguration = this._context.RunspaceConfiguration as RunspaceConfigForSingleShell;
                    bool?nullable3 = useLocalScope;
                    int  num3      = nullable3.HasValue ? (nullable3.GetValueOrDefault() ? 1 : 0) : 1;
                    commandProcessorBase = this.CreateScriptProcessorForSingleShell(scriptInfo1, runspaceConfiguration, num3 != 0);
                    break;
                }
                catch (ScriptRequiresSyntaxException ex)
                {
                    CommandNotFoundException notFoundException = new CommandNotFoundException(ex.Message, (Exception)ex);
                    CommandDiscovery.tracer.TraceException((Exception)notFoundException);
                    throw notFoundException;
                }
                catch (PSArgumentException ex)
                {
                    CommandNotFoundException notFoundException = new CommandNotFoundException(commandInfo.Name, (Exception)ex, "ScriptRequiresInvalidFormat", new object[0]);
                    CommandDiscovery.tracer.TraceException((Exception)notFoundException);
                    throw notFoundException;
                }

            case CommandTypes.Application:
                commandProcessorBase = (CommandProcessorBase) new NativeCommandProcessor((ApplicationInfo)commandInfo, this._context);
                break;

            case CommandTypes.Script:
                ScriptInfo       scriptInfo2 = (ScriptInfo)commandInfo;
                ExecutionContext context2    = this._context;
                bool?            nullable4   = useLocalScope;
                int num4 = nullable4.HasValue ? (nullable4.GetValueOrDefault() ? 1 : 0) : 1;
                commandProcessorBase = CommandDiscovery.CreateCommandProcessorForScript(scriptInfo2, context2, num4 != 0);
                break;

            default:
                CommandNotFoundException notFoundException1 = new CommandNotFoundException(commandInfo.Name, (Exception)null, "CommandNotFoundException", new object[0]);
                CommandDiscovery.tracer.TraceException((Exception)notFoundException1);
                throw notFoundException1;
            }
            PSSQMAPI.IncrementData(commandInfo.CommandType);
            commandProcessorBase.Command.CommandOriginInternal       = commandOrigin;
            commandProcessorBase.Command.MyInvocation.InvocationName = commandInfo.Name;
            return(commandProcessorBase);
        }