Udostępnij przez


ShouldProcess

Poziom ważności: ostrzeżenie

Opis

Jeśli polecenie cmdlet deklaruje atrybut, należy również wywołać metodę SupportsShouldProcessShouldProcess. Naruszenie jest dowolną funkcją, która deklaruje atrybut, SupportsShouldProcess ale nie wykonuje wywołań ShouldProcess ani nie wywołuje ShouldProcess , ale nie deklaruje SupportsShouldProcess.

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

Jak

Aby naprawić naruszenie tej reguły, wywołaj ShouldProcess metodę, gdy polecenie cmdlet deklaruje SupportsShouldProcess atrybut. Możesz też dodać SupportsShouldProcess argument atrybutu podczas wywoływania metody ShouldProcess.

Przykład

Nieodpowiednim

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

Odpowiedź prawidłowa

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