ShouldProcess

Nivel de gravedad: Advertencia

Descripción

Esta regla detecta desajustes entre SupportsShouldProcess declaraciones y ShouldProcess llamadas. Cuando un cmdlet declara el SupportsShouldProcess atributo, también debe llamar al ShouldProcess método.

Las infracciones ocurren cuando:

  • Una función declara SupportsShouldProcess pero no llama ShouldProcess
  • Una función llama ShouldProcess pero no declara SupportsShouldProcess

Para corregir esta violación, asegúrate de que ShouldProcess las llamadas se emparejan con la SupportsShouldProcess declaración de atributo.

Para obtener más información, consulte los artículos siguientes:

Example

No 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
    }
}