Search Wiki:

CallShouldProcessOnlyIfDeclaringSupport


TypeName CallShouldProcessOnlyIfDeclaringSupport
CheckId PS1012
Error-Level Error
Category Microsoft.PowerShell
Certainty 95%
BreakingChangeNon-breaking
Cause A cmdlet calls the ShouldProcess method but does not have the SupportsShouldProcess parameter in its CmdletAttribute declaration, or it has the SupportsShouldProcess parameter in its CmdletAttribute declaration but does not call the ShouldProcess method.
Rule descriptionCmdlets that call the ShouldProcess method must have the SupportsShouldProcess parameter in their CmdletAttribute declaration. Cmdlets that have the SupportsShouldProcess parameter must actually call the ShouldProcess method. The SupportShouldProcess property adds the WhatIf and Confirm parameters to the cmdlet. If the cmdlet has those parameters but does not call the ShouldProcess method, the user might change the system unintentionally. If a cmdlet calls the ShouldProcess method but does not have the WhatIf and Confirm parameters, the user cannot control the action of the ShouldProcess method.
How to fix violations If a cmdlet calls the ShouldProcess method, set the SupportsShouldProcess property of its CmdletAttribute declaration to true. If the SupportsShouldProcess property is set to true, add a call to the ShouldProcess method, or set the SupportsShouldProcess property to false.
When to exclude warnings Do not suppress warnings from this rule.
Related rules (None)
See also FxCop Rules for Windows PowerShell

Examples


// This cmdlet code violates the rule, because it calls
// ShouldProcess but the SupportsShouldProcess property
// is not set to true.

    [Cmdlet("Write", "String")]
    public class WriteStringCommand : PSCmdlet
    {
        protected override void BeginProcessing()
        {
            foreach (string s in this.inputObject)
            {
                if (this.ShouldProcess(s))
                {
                    this.Host.UI.WriteLine(s);
                }
            }
        }
    }


// In this example, SupportsShouldProcess
// is set to True.

    [Cmdlet("Write", "String", 
        SupportsShouldProcess = true)]
    public class WriteStringCommand : PSCmdlet
    {
        protected override void BeginProcessing()
        {
            foreach (string s in this.inputObject)
            {
                if (this.ShouldProcess(s))
                {
                    this.Host.UI.WriteLine(s);
                }
            }
        }
    }


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