ShouldProcess

poziom ważności: ostrzeżenie

Description

Ta reguła wykrywa niezgodności między SupportsShouldProcess deklaracjami a ShouldProcess wywołaniami. Gdy cmdlet deklaruje atrybut, SupportsShouldProcess powinien również wywołać ShouldProcess metodę.

Naruszenia mają miejsce, gdy:

  • Funkcja deklaruje SupportsShouldProcess , ale nie wywołuje ShouldProcess
  • Funkcja wywołuje ShouldProcess , ale nie deklaruje SupportsShouldProcess

Aby naprawić to naruszenie, upewnij się, że ShouldProcess wywołania są sparowane z deklaracją atrybutów SupportsShouldProcess .

Aby uzyskać więcej informacji, zobacz następujące artykuły:

Example

Niezgodne

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

Zgodne

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