示例#1
0
        private Collection <AstParameterArgumentPair> BindPositionalParameter(Collection <AstParameterArgumentPair> unboundArguments, int validParameterSetFlags, int defaultParameterSetFlag, bool honorDefaultParameterSet)
        {
            Collection <AstParameterArgumentPair> nonPositionalArguments = new Collection <AstParameterArgumentPair>();

            if (this._bindingEffective && (unboundArguments.Count > 0))
            {
                SortedDictionary <int, Dictionary <MergedCompiledCommandParameter, PositionalCommandParameter> > dictionary;
                List <AstParameterArgumentPair> unboundArgumentsCollection = new List <AstParameterArgumentPair>(unboundArguments);
                try
                {
                    dictionary = ParameterBinderController.EvaluateUnboundPositionalParameters(this._unboundParameters, validParameterSetFlags);
                }
                catch (InvalidOperationException)
                {
                    this._bindingEffective = false;
                    return(nonPositionalArguments);
                }
                if (dictionary.Count == 0)
                {
                    return(unboundArguments);
                }
                int unboundArgumentsIndex = 0;
                foreach (Dictionary <MergedCompiledCommandParameter, PositionalCommandParameter> dictionary2 in dictionary.Values)
                {
                    if (dictionary2.Count != 0)
                    {
                        AstParameterArgumentPair argument = GetNextPositionalArgument(unboundArgumentsCollection, nonPositionalArguments, ref unboundArgumentsIndex);
                        if (argument == null)
                        {
                            break;
                        }
                        bool flag = false;
                        if ((honorDefaultParameterSet && (defaultParameterSetFlag != 0)) && ((validParameterSetFlags & defaultParameterSetFlag) != 0))
                        {
                            flag = this.BindPseudoPositionalParameterInSet(defaultParameterSetFlag, dictionary2, argument, false);
                        }
                        if (!flag)
                        {
                            flag = this.BindPseudoPositionalParameterInSet(validParameterSetFlags, dictionary2, argument, true);
                        }
                        if (!flag)
                        {
                            nonPositionalArguments.Add(argument);
                        }
                        else if (validParameterSetFlags != this._currentParameterSetFlag)
                        {
                            validParameterSetFlags = this._currentParameterSetFlag;
                            ParameterBinderController.UpdatePositionalDictionary(dictionary, validParameterSetFlags);
                        }
                    }
                }
                for (int i = unboundArgumentsIndex; i < unboundArgumentsCollection.Count; i++)
                {
                    nonPositionalArguments.Add(unboundArgumentsCollection[i]);
                }
            }
            return(nonPositionalArguments);
        }
示例#2
0
        /// <summary>
        /// Gets the PSTraceSource instances that match the names specified.
        /// </summary>
        protected override void BeginProcessing()
        {
            Collection <PSTraceSource> preconfiguredSources = null;

            _matchingSources = ConfigureTraceSource(base.NameInternal, false, out preconfiguredSources);

            TurnOnTracing(_matchingSources, false);
            TurnOnTracing(preconfiguredSources, true);

            // Now that tracing has been configured, move all the sources into a
            // single collection

            foreach (PSTraceSource preconfiguredSource in preconfiguredSources)
            {
                _matchingSources.Add(preconfiguredSource);
            }

            if (ParameterSetName == "commandSet")
            {
                // Create the CommandProcessor and add it to a pipeline

                CommandProcessorBase commandProcessor =
                    this.Context.CommandDiscovery.LookupCommandProcessor(Command, CommandOrigin.Runspace, false);

                // Add the parameters that were specified

                ParameterBinderController.AddArgumentsToCommandProcessor(commandProcessor, ArgumentList);

                _pipeline = new PipelineProcessor();
                _pipeline.Add(commandProcessor);

                // Hook up the success and error pipelines to this cmdlet's WriteObject and
                // WriteError methods

                _pipeline.ExternalErrorOutput   = new TracePipelineWriter(this, true, _matchingSources);
                _pipeline.ExternalSuccessOutput = new TracePipelineWriter(this, false, _matchingSources);
            }

            ResetTracing(_matchingSources);
        }
示例#3
0
        protected override void BeginProcessing()
        {
            Collection <PSTraceSource> preconfiguredSources = null;

            this.matchingSources = base.ConfigureTraceSource(base.NameInternal, false, out preconfiguredSources);
            base.TurnOnTracing(this.matchingSources, false);
            base.TurnOnTracing(preconfiguredSources, true);
            foreach (PSTraceSource source in preconfiguredSources)
            {
                this.matchingSources.Add(source);
            }
            if (base.ParameterSetName == "commandSet")
            {
                CommandProcessorBase commandProcessor = base.Context.CommandDiscovery.LookupCommandProcessor(this.command, CommandOrigin.Runspace, false);
                ParameterBinderController.AddArgumentsToCommandProcessor(commandProcessor, this.ArgumentList);
                this.pipeline = new PipelineProcessor();
                this.pipeline.Add(commandProcessor);
                this.pipeline.ExternalErrorOutput   = new TracePipelineWriter(this, true, this.matchingSources);
                this.pipeline.ExternalSuccessOutput = new TracePipelineWriter(this, false, this.matchingSources);
            }
            base.ResetTracing(this.matchingSources);
        }
示例#4
0
        private bool PrepareCommandElements(ExecutionContext context)
        {
            int  num                   = 0;
            bool dotSource             = this._commandAst.InvocationOperator == TokenKind.Dot;
            CommandProcessorBase base2 = null;
            string resolvedCommandName = null;
            bool   flag2               = false;

            try
            {
                base2 = this.PrepareFromAst(context, out resolvedCommandName) ?? context.CreateCommand(resolvedCommandName, dotSource);
            }
            catch (RuntimeException exception)
            {
                CommandProcessorBase.CheckForSevereException(exception);
                if ((this._commandAst.IsInWorkflow() && (resolvedCommandName != null)) && CompletionCompleters.PseudoWorkflowCommands.Contains <string>(resolvedCommandName, StringComparer.OrdinalIgnoreCase))
                {
                    flag2 = true;
                }
                else
                {
                    return(false);
                }
            }
            CommandProcessor           commandProcessor = base2 as CommandProcessor;
            ScriptCommandProcessorBase base3            = base2 as ScriptCommandProcessorBase;
            bool          flag3 = (commandProcessor != null) && commandProcessor.CommandInfo.ImplementsDynamicParameters;
            List <object> list  = flag3 ? new List <object>(this._commandElements.Count) : null;

            if (((commandProcessor != null) || (base3 != null)) || flag2)
            {
                num++;
                while (num < this._commandElements.Count)
                {
                    CommandParameterAst parameterAst = this._commandElements[num] as CommandParameterAst;
                    if (parameterAst != null)
                    {
                        if (list != null)
                        {
                            list.Add(parameterAst.Extent.Text);
                        }
                        AstPair item = (parameterAst.Argument != null) ? new AstPair(parameterAst, parameterAst.Argument) : new AstPair(parameterAst);
                        this._arguments.Add(item);
                    }
                    else
                    {
                        StringConstantExpressionAst ast2 = this._commandElements[num] as StringConstantExpressionAst;
                        if ((ast2 == null) || !ast2.Value.Trim().Equals("-", StringComparison.OrdinalIgnoreCase))
                        {
                            ExpressionAst argumentAst = this._commandElements[num] as ExpressionAst;
                            if (argumentAst != null)
                            {
                                if (list != null)
                                {
                                    list.Add(argumentAst.Extent.Text);
                                }
                                this._arguments.Add(new AstPair(null, argumentAst));
                            }
                        }
                    }
                    num++;
                }
            }
            if (commandProcessor != null)
            {
                this._function = false;
                if (flag3)
                {
                    ParameterBinderController.AddArgumentsToCommandProcessor(commandProcessor, list.ToArray());
                    bool flag4 = false;
                    bool flag5 = false;
                    do
                    {
                        CommandProcessorBase currentCommandProcessor = context.CurrentCommandProcessor;
                        try
                        {
                            context.CurrentCommandProcessor = commandProcessor;
                            commandProcessor.SetCurrentScopeToExecutionScope();
                            if (!flag4)
                            {
                                commandProcessor.CmdletParameterBinderController.BindCommandLineParametersNoValidation(commandProcessor.arguments);
                            }
                            else
                            {
                                flag5 = true;
                                commandProcessor.CmdletParameterBinderController.ClearUnboundArguments();
                                commandProcessor.CmdletParameterBinderController.BindCommandLineParametersNoValidation(new Collection <CommandParameterInternal>());
                            }
                        }
                        catch (ParameterBindingException exception2)
                        {
                            if ((exception2.ErrorId == "MissingArgument") || (exception2.ErrorId == "AmbiguousParameter"))
                            {
                                flag4 = true;
                            }
                        }
                        catch (Exception exception3)
                        {
                            CommandProcessorBase.CheckForSevereException(exception3);
                        }
                        finally
                        {
                            context.CurrentCommandProcessor = currentCommandProcessor;
                            commandProcessor.RestorePreviousScope();
                        }
                    }while (flag4 && !flag5);
                }
                this._commandInfo             = commandProcessor.CommandInfo;
                this._commandName             = commandProcessor.CommandInfo.Name;
                this._bindableParameters      = commandProcessor.CmdletParameterBinderController.BindableParameters;
                this._defaultParameterSetFlag = commandProcessor.CommandInfo.CommandMetadata.DefaultParameterSetFlag;
            }
            else if (base3 != null)
            {
                this._function                = true;
                this._commandInfo             = base3.CommandInfo;
                this._commandName             = base3.CommandInfo.Name;
                this._bindableParameters      = base3.ScriptParameterBinderController.BindableParameters;
                this._defaultParameterSetFlag = 0;
            }
            else if (!flag2)
            {
                return(false);
            }
            if (this._commandAst.IsInWorkflow())
            {
                Type type = Type.GetType("Microsoft.PowerShell.Workflow.AstToWorkflowConverter, Microsoft.PowerShell.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
                if (type != null)
                {
                    Dictionary <string, Type> dictionary = (Dictionary <string, Type>)type.GetMethod("GetActivityParameters").Invoke(null, new object[] { this._commandAst });
                    if (dictionary != null)
                    {
                        bool flag6 = dictionary.ContainsKey("PSComputerName") && !dictionary.ContainsKey("ComputerName");
                        List <MergedCompiledCommandParameter> source = new List <MergedCompiledCommandParameter>();
                        Collection <Attribute> attributes            = new Collection <Attribute> {
                            new ParameterAttribute()
                        };
                        foreach (KeyValuePair <string, Type> pair2 in dictionary)
                        {
                            if (flag2 || !this._bindableParameters.BindableParameters.ContainsKey(pair2.Key))
                            {
                                Type actualActivityParameterType = GetActualActivityParameterType(pair2.Value);
                                RuntimeDefinedParameter  runtimeDefinedParameter = new RuntimeDefinedParameter(pair2.Key, actualActivityParameterType, attributes);
                                CompiledCommandParameter parameter = new CompiledCommandParameter(runtimeDefinedParameter, false)
                                {
                                    IsInAllSets = true
                                };
                                MergedCompiledCommandParameter parameter3 = new MergedCompiledCommandParameter(parameter, ParameterBinderAssociation.DeclaredFormalParameters);
                                source.Add(parameter3);
                            }
                        }
                        if (source.Any <MergedCompiledCommandParameter>())
                        {
                            MergedCommandParameterMetadata metadata = new MergedCommandParameterMetadata();
                            if (!flag2)
                            {
                                metadata.ReplaceMetadata(this._bindableParameters);
                            }
                            foreach (MergedCompiledCommandParameter parameter5 in source)
                            {
                                metadata.BindableParameters.Add(parameter5.Parameter.Name, parameter5);
                            }
                            this._bindableParameters = metadata;
                        }
                        foreach (string str2 in ignoredWorkflowParameters)
                        {
                            if (this._bindableParameters.BindableParameters.ContainsKey(str2))
                            {
                                this._bindableParameters.BindableParameters.Remove(str2);
                            }
                        }
                        if (this._bindableParameters.BindableParameters.ContainsKey("ComputerName") && flag6)
                        {
                            this._bindableParameters.BindableParameters.Remove("ComputerName");
                            string key = (from aliasPair in this._bindableParameters.AliasedParameters
                                          where string.Equals("ComputerName", aliasPair.Value.Parameter.Name)
                                          select aliasPair.Key).FirstOrDefault <string>();
                            this._bindableParameters.AliasedParameters.Remove(key);
                        }
                    }
                }
            }
            this._unboundParameters.AddRange(this._bindableParameters.BindableParameters.Values);
            CommandBaseAst ast4   = null;
            PipelineAst    parent = this._commandAst.Parent as PipelineAst;

            if (parent.PipelineElements.Count > 1)
            {
                foreach (CommandBaseAst ast6 in parent.PipelineElements)
                {
                    if (ast6.GetHashCode() == this._commandAst.GetHashCode())
                    {
                        this._isPipelineInputExpected = ast4 != null;
                        if (this._isPipelineInputExpected)
                        {
                            this._pipelineInputType = typeof(object);
                        }
                        break;
                    }
                    ast4 = ast6;
                }
            }
            return(true);
        }