ShouldProcess

Schweregrad: Warnung

Description

Diese Regel erkennt Diskrepanzen zwischen SupportsShouldProcess Deklarationen und ShouldProcess Anrufen. Wenn ein Cmdlet das SupportsShouldProcess Attribut deklariert, sollte es auch die Methode ShouldProcess aufrufen.

Verstöße treten auf, wenn:

  • Eine Funktion deklariert, SupportsShouldProcess ruft aber nicht auf ShouldProcess
  • Eine Funktion ruft auf, ShouldProcess deklariert aber nicht SupportsShouldProcess

Um diesen Verstoß zu beheben, stellen Sie sicher, dass ShouldProcess Aufrufe mit der Attributdeklaration SupportsShouldProcess gepaart werden.

Weitere Informationen findest du in den folgenden Artikeln:

Example

Nicht konform

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

Konform

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