ShouldProcess

ernstniveau: waarschuwing

Beschrijving

Deze regel detecteert mismatches tussen SupportsShouldProcess verklaringen en ShouldProcess calls. Wanneer een cmdlet het SupportsShouldProcess attribuut declareert, moet het ook de ShouldProcess methode aanroepen.

Overtredingen vinden plaats wanneer:

  • Een functie declareert SupportsShouldProcess maar roept niet aan ShouldProcess
  • Een functie roept ShouldProcess aan, maar declareert niet SupportsShouldProcess

Om deze overtreding te verhelpen, zorg ervoor dat ShouldProcess aanroepen worden gekoppeld aan de SupportsShouldProcess attribuutdeclaratie.

Zie de volgende artikelen voor meer informatie:

Voorbeeld

Niet conform

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