Ejemplo n.º 1
0
        /// <summary>
        /// Returns a CmdletParameterBinderController for the specified command
        /// </summary>
        /// <param name="command">
        /// The cmdlet to bind parameters to.
        /// </param>
        /// <returns>
        /// A new instance of a CmdletParameterBinderController.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// if <paramref name="command"/> is not a Cmdlet.
        /// </exception>
        internal ParameterBinderController NewParameterBinderController(InternalCommand command)
        {
            Cmdlet cmdlet = command as Cmdlet;

            if (cmdlet == null)
            {
                throw PSTraceSource.NewArgumentException("command");
            }

            ParameterBinderBase parameterBinder;
            IScriptCommandInfo  scriptCommandInfo = CommandInfo as IScriptCommandInfo;

            if (scriptCommandInfo != null)
            {
                parameterBinder = new ScriptParameterBinder(scriptCommandInfo.ScriptBlock, cmdlet.MyInvocation, this._context, cmdlet, CommandScope);
            }
            else
            {
                parameterBinder = new ReflectionParameterBinder(cmdlet, cmdlet);
            }

            _cmdletParameterBinderController = new CmdletParameterBinderController(cmdlet, CommandInfo.CommandMetadata, parameterBinder);

            return(_cmdletParameterBinderController);
        }
Ejemplo n.º 2
0
        internal override ParameterBinderController NewParameterBinderController(
            InternalCommand command)
        {
            if (!(command is Cmdlet cmdlet))
            {
                throw CommandProcessor.tracer.NewArgumentException(nameof(command));
            }
            ParameterBinderBase parameterBinder = !(this.CommandInfo is IScriptCommandInfo commandInfo) ? (ParameterBinderBase) new ReflectionParameterBinder((object)cmdlet, cmdlet) : (ParameterBinderBase) new ScriptParameterBinder(commandInfo.ScriptBlock, cmdlet.MyInvocation, this.context, (InternalCommand)cmdlet);

            this.cmdletParameterBinderController = new CmdletParameterBinderController(cmdlet, this.CommandInfo.CommandMetadata, parameterBinder);
            return((ParameterBinderController)this.cmdletParameterBinderController);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Populates the parameters specified from the pipeline.
        /// </summary>
        /// <returns>
        /// A bool indicating whether read succeeded.
        /// </returns>
        /// <exception cref="ParameterBindingException">
        /// If a parameter fails to bind.
        /// or
        /// If a mandatory parameter is missing.
        /// </exception>
        /// <exception cref="PipelineStoppedException">
        /// The pipeline was already stopped.
        /// </exception>
        // 2003/10/07-JonN was public, now internal
        internal sealed override bool Read()
        {
            // (1) If Read() is called for the first time and with pipe closed and
            //     no object in the input pipe and
            //     (typically for the first cmdlet in the pipe & during programmatic
            //     execution of a command), Read() will succeed (return true) for
            //     only one time (so that the
            // (2) If Read() is called with some input objects in the pipeline, it
            //     processes the input
            //     object one at a time and adds parameters from the input object
            //     to the list of parameters. If
            //     added to the error pipe and Read() will continue to read the
            //     next object in the pipe.
            // (3) Read() will return false if there are no objects in the pipe
            //     for processing.
            // (4) Read() will return true if the parameters are encoded in the
            //     request - signals ready for execution.
            // (5) Read() will refresh the properties that are encoded via pipeline
            //     parameters in the next
            //     call to Read() [To their default values, so that the
            //     next execution of the command will
            //     not work on previously specified parameter].
            // If the flag 'bail in next call' is true, then bail out returning false.
            if (_bailInNextCall)
            {
                return(false);
            }

            // ProcessRecord() will loop on Command.Read(), and continue calling
            // ProcessRecord() until the incoming pipe is empty.  We need to
            // stop this loop if a downstream cmdlet broke guidelines and
            // "swallowed" a PipelineStoppedException.
            Command.ThrowIfStopping();

            // Prepare the default value parameter list if this is the first call to Read
            if (_firstCallToRead)
            {
                _firstCallToRead = false;
                if (!IsPipelineInputExpected())
                {
                    // Cmdlet should operate only with command-line parameters
                    // Let the command Execute with the specified command line parameters
                    // And Read should return false in the next call.
                    _bailInNextCall = true;
                    return(true);
                }
            }

            // If this cmdlet has any members that could be bound
            // from the pipeline, do that now. In fact, we always try and
            // do it once anyway because this BindPipelineParameters() does
            // the final binding stage in before executing the cmdlet.

            bool mandatoryParametersSpecified = false;

            while (!mandatoryParametersSpecified)
            {
                // Retrieve the object from the input pipeline
                object inputObject = this.commandRuntime.InputPipe.Retrieve();

                if (inputObject == AutomationNull.Value)
                {
                    // no object in the pipeline, stop reading
                    Command.CurrentPipelineObject = null;
                    return(false);
                }

                // If we are reading input for the first command in the pipeline increment PipelineIterationInfo[0], which is the number of items read from the input
                if (this.Command.MyInvocation.PipelinePosition == 1)
                {
                    this.Command.MyInvocation.PipelineIterationInfo[0]++;
                }

                try
                {
                    // Process the input pipeline object
                    if (!ProcessInputPipelineObject(inputObject))
                    {
                        // The input object was not bound to any parameters of the cmdlet.
                        // Write a non-terminating error and continue with the next input
                        // object.
                        WriteInputObjectError(
                            inputObject,
                            ParameterBinderStrings.InputObjectNotBound,
                            "InputObjectNotBound");
                        continue;
                    }
                }
                catch (ParameterBindingException bindingError)
                {
                    // Set the target and write the error
                    bindingError.ErrorRecord.SetTargetObject(inputObject);

                    ErrorRecord errorRecord =
                        new ErrorRecord(
                            bindingError.ErrorRecord,
                            bindingError);

                    this.commandRuntime._WriteErrorSkipAllowCheck(errorRecord);
                    continue;
                }

                Collection <MergedCompiledCommandParameter> missingMandatoryParameters;

                using (ParameterBinderBase.bindingTracer.TraceScope(
                           "MANDATORY PARAMETER CHECK on cmdlet [{0}]",
                           this.CommandInfo.Name))
                {
                    // Check for unbound mandatory parameters but don't prompt
                    mandatoryParametersSpecified =
                        this.CmdletParameterBinderController.HandleUnboundMandatoryParameters(out missingMandatoryParameters);
                }

                if (!mandatoryParametersSpecified)
                {
                    string missingParameters =
                        CmdletParameterBinderController.BuildMissingParamsString(missingMandatoryParameters);

                    // Since the input object did not satisfy all mandatory parameters
                    // for the command, write an ErrorRecord to the error pipe with
                    // the target as the input object.

                    WriteInputObjectError(
                        inputObject,
                        ParameterBinderStrings.InputObjectMissingMandatory,
                        "InputObjectMissingMandatory",
                        missingParameters);
                }
            }

            return(true);
        }