ShouldProcess

súlyossági szint: Figyelmeztetési

Description

Ez a szabály észleli a kijelentések és SupportsShouldProcess hívások közötti eltéréseketShouldProcess. Amikor egy cmdlet kihirdeti az SupportsShouldProcess attribútumot, akkor a metódust is hívja ShouldProcess .

A szabálysértések akkor fordulnak elő, ha:

  • Egy függvény deklarál SupportsShouldProcess , de nem hív ShouldProcess
  • Egy függvény hív ShouldProcess , de nem deklarál SupportsShouldProcess

Ennek a szabálysértésnek a javításához győződjön meg róla, hogy ShouldProcess a hívások párosítják az SupportsShouldProcess attribútum-kijelentést.

További információkért lásd a következő cikkeket:

Example

Nem megfelelő

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

Megfelelő

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