PowerShell / PowerShell/PSScriptAnalyzer

PSShouldProcess rule ignores ShouldContinue

Offen
#1,304 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Area - Rules Issue - Bug
Vorherrschende Sprache
C#
Sterne
2.2k
Forks
414
Ø Merge
13 Std. 1 Min.
Gemergte PRs (30 T.)
2

Beschreibung

Steps to reproduce

# File: c:\temp\Invoke-MVP.ps1
function Invoke-MVP {
    [CmdletBinding(SupportsShouldProcess = $true)] # This line shows an exception
    [OutputType('System.String')]
    Param (
        [Parameter()]
        [switch]$Force # just to hide PSAvoidShouldContinueWithoutForce for this MVP
    )
    {
        if ($PSCmdlet.ShouldContinue('Should I continue','Should I continue')) {
            'I continued'
        }
    }
}

Invoke-ScriptAnalyzer -Path 'C:\temp\Invoke-MVP.ps1'

Expected behavior

No error; using ShouldContinue should be equivalent to using ShouldProcess (for which we don't see this issue).

Actual behavior


RuleName                            Severity     ScriptName      Line  Message                                                     
--------                            --------     --------------  ----  -------                                                     
PSShouldProcess                     Warning      Invoke-MVP.ps1 2     'Invoke-POC' has the ShouldProcess attribute but does not   
                                                                  call ShouldProcess/ShouldContinue.                          

Environment data

> $PSVersionTable

Name                           Value                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                             
PSVersion                      5.1.14409.1018                                                                                                                                                                                                    
PSEdition                      Desktop                                                                                                                                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                           
BuildVersion                   10.0.14409.1018                                                                                                                                                                                                   
CLRVersion                     4.0.30319.42000                                                                                                                                                                                                   
WSManStackVersion              3.0                                                                                                                                                                                                               
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                               
SerializationVersion           1.1.0.1           
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.18.1

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der PSShouldProcess-Regel und reproduziere die Warnung mit C:\temp\Invoke-MVP.ps1 über Invoke-ScriptAnalyzer. Verfolge, wie die Regel Aufrufe von ShouldProcess und ShouldContinue erkennt; als abgeschlossen gilt, dass die bereitgestellte Funktion keine PSShouldProcess-Warnung mehr erzeugt, wenn sie ShouldContinue aufruft.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp, powershell
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.