ShouldProcess

Şiddet Seviyesi: Uyarı

Description

Bu kural, beyanlar ve SupportsShouldProcess aramalar arasındaki ShouldProcess uyumsuzlukları tespit eder. Bir cmdlet özniteliği SupportsShouldProcess ilan ettiğinde, aynı zamanda metodu ShouldProcess da çağırmalıdır.

İhlaller şu durumlarda meydana gelir:

  • Bir fonksiyon ilan SupportsShouldProcess eder ama çağırmaz ShouldProcess
  • Bir fonksiyon çağırır ShouldProcess ama bildirmez SupportsShouldProcess

Bu ihlali düzeltmek için, çağrıların öznitelik bildirgesiyle ShouldProcess eşleştirildiğinden emin SupportsShouldProcess olun.

Daha fazla bilgi için aşağıdaki makaleleri inceleyin:

Example

Standartlara Uygun Değil

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