ShouldProcess

Livello di gravità: Avviso

Description

Questa regola rileva disallineamenti tra SupportsShouldProcess dichiarazioni e ShouldProcess chiamate. Quando un cmdlet dichiara l'attributo SupportsShouldProcess , dovrebbe anche chiamare il ShouldProcess metodo.

Le violazioni si verificano quando:

  • Una funzione dichiara SupportsShouldProcess ma non chiama ShouldProcess
  • Una funzione chiama ShouldProcess ma non dichiara SupportsShouldProcess

Per correggere questa violazione, assicurati che ShouldProcess le chiamate siano abbinate alla SupportsShouldProcess dichiarazione di attributo.

Per altre informazioni, consultare gli articoli seguenti:

Example

Non conforme

function Set-File
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    Param
    (
        # Path to file
        [Parameter(Mandatory=$true)]
        $Path
    )
    'String' | Out-File -FilePath $Path
}

Compliant

function Set-File
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    Param
    (
        # Path to file
        [Parameter(Mandatory=$true)]
        $Path,

        [Parameter(Mandatory=$true)]
        [string]$Content
    )

    if ($PSCmdlet.ShouldProcess($Path, ("Setting content to '{0}'" -f $Content)))
    {
        $Content | Out-File -FilePath $Path
    }
    else
    {
        # Code that should be processed if doing a WhatIf operation
        # Must NOT change anything outside of the function / script
    }
}