ShouldProcess

Tingkat Keparahan : Peringatan

Description

Aturan ini mendeteksi ketidakcocokan antara SupportsShouldProcess deklarasi dan ShouldProcess panggilan. Saat cmdlet mendeklarasikan SupportsShouldProcess atribut, cmdlet juga harus memanggil ShouldProcess metode.

Pelanggaran terjadi ketika:

  • Fungsi mendeklarasikan SupportsShouldProcess tetapi tidak memanggil ShouldProcess
  • Fungsi memanggil ShouldProcess tetapi tidak mendeklarasikan SupportsShouldProcess

Untuk memperbaiki pelanggaran ini, pastikan ShouldProcess panggilan dipasangkan dengan SupportsShouldProcess deklarasi atribut.

Untuk informasi lebih lanjut, baca artikel berikut:

Example

Tidak sesuai

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

Sesuai

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