Search Wiki:

AcceptForceParameterWhenCallingShouldContinue


TypeName AcceptForceParameterWhenCallingShouldContinue
CheckId PS1004
Error-Level Warning
Category Microsoft.PowerShell
Certainty 95%
BreakingChangeDepends on fix
Cause A cmdlet calls the ShouldContinue method, but it does not have a Force parameter.
Rule descriptionIf your cmdlet calls the ShouldContinue method, it must have a Force parameter that bypasses the call to ShouldContinue and proceeds with the operation. This allows the cmdlet to be used in non-interactive scripts and hosts.
How to fix violations Add a Force switch parameter to the <className> cmdlet. Add code that skips the call to the ShouldContinue method when the Force parameter is used. Adding a new Force parameter can be a breaking change. If an existing parameter begins with the same letters as "Force," uses of the parameter abbreviation that worked previously will now be ambiguous. To mitigate this breaking change, add a parameter alias that associates the original parameter with its most likely abbreviation.
When to exclude warnings Do not suppress warnings from this rule.
Related rules (None)
See also FxCop Rules for Windows PowerShell

Examples


// Example of a cmdlet that violates this rule.
    [Cmdlet("Set", "ExecutionPolicy")]
    public class SetExecutionPolicyCommand : PSCmdlet
    {
        protected override void BeginProcessing()
        {
            // Because the prompt cannot be suppressed, this 
            // cmdlet cannot be used in non-interactive 
            // scripts and hosts.
            if (this.ShouldContinue(null, null))
            {
                // ...
            }
        }
    }

// Example of a cmdlet that follows this rule.
    [Cmdlet("Set", "ExecutionPolicy")]
    public class SetExecutionPolicyCommand : PSCmdlet
    {
        [Parameter]
        public SwitchParameter Force
        {
            get { return this.force; }
            set { this.force = value; }
        }
        private SwitchParameter force;
 
        protected override void BeginProcessing()
        {
            if (this.Force.IsPresent ||
                this.ShouldContinue(null, null))
            {
                // ...
            }
        }
    }

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