Search Wiki:

AllCmdletsShouldAcceptPipelineInput


TypeName AllCmdletsShouldAcceptPipelineInput
CheckId PS1101
Error-Level Warning
Category Microsoft.PowerShell
Certainty 95%
BreakingChange Non-breaking
Cause A cmdlet does not accept pipeline input.
Rule descriptionIn each cmdlet, there should be at least one parameter that accepts input from the pipeline by means of another cmdlet or variable. If none of the parameters support the ValueFromPipeline or ValueFromPipelineByPropertyName properties, the cmdlet cannot be used in a pipeline. To indicate that a parameter accepts input from the pipeline, add the ValueFromPipeline or ValueFromPipelineByPropertyName named parameters of the Parameter attribute to the parameter declaration.
How to fix violations Add the ValueFromPipeline or ValueFromPipelineByPropertyName parameters to at least one parameter in at least one parameter set.
When to exclude warnings Do not suppress warnings from this rule.
Related rules (None)
See also FxCop Rules for Windows PowerShell

Examples


// This cmdlet violates the rule, because none of its
// parameters accepts pipeline input.

    [Cmdlet("Write", "String")]
    public class WriteStringCommand : PSCmdlet
    {
        [Parameter]
        public string[] InputObject
        {
            get { return this.inputObject; }
            set { this.inputObject = value; }
        }
        private string[] inputObject;
 
        protected override void BeginProcessing()
        {
            foreach (string s in this.inputObject)
            {
                this.Host.UI.WriteLine(s);
            }
        }
    }

//To fix the violation, add ValueFromPipeline or
//ValueFromPipelineByProperty name as follows:

    [Cmdlet("Write", "String")]
    public class WriteStringCommand : PSCmdlet
    {
        [Parameter(ValueFromPipeline = true)]        
        public string[] InputObject
        {
            get { return this.inputObject; }
            set { this.inputObject = value; }
        }
        private string[] inputObject;
 
        protected override void BeginProcessing()
        {
            foreach (string s in this.inputObject)
            {
                this.Host.UI.WriteLine(s);
            }
        }
    }

Back to FxCop Rules for Windows PowerShell.
Last edited Mar 24 2010 at 11:46 PM  by JuneB, version 3
Updating...
Page view tracker