PowerShell / PowerShell/vscode-powershell

Nested ForEach-Object -Parallel

Offen
#4,926 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Issue-Bug Needs: Triage
Vorherrschende Sprache
TypeScript
Sterne
1.9k
Forks
548
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Prerequisites
  • I have written a descriptive issue title.
  • I have searched all open and closed issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
  • If this is a security issue, I have read the security issue reporting guidance.
Summary

I am expecting the same behaviour between the PowerShell Extension terminal and the standard PowerShell terminal.

It appears that there is a scoping issue or not cleaning up of variable names within the PowerShell Extension terminal.

PowerShell Version
Name                           Value
----                           -----
PSVersion                      7.4.1
PSEdition                      Core
GitCommitId                    7.4.1
OS                             Microsoft Windows 10.0.20348
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Name             : ConsoleHost
Version          : 7.4.1
InstanceId       : 1a36e6cd-b018-4307-8cf6-1068ec5a11bf
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
Visual Studio Code Version
1.86.2
903b1e9d8990623e3d7da1df3d33db3e42d80eda
x64
Extension Version
ms-vscode.powershell@2024.3.0
Steps to Reproduce
Test.ps1
Param (
    [int]$MaxConcurrentJobs = 2
)

$Tasks = @(
    @{
        Type    = 'PC'
        Actions = @(
            @{
                ComputerName = 'PC1'
                Result       = $null
            }
            @{
                ComputerName = 'PC2'
                Result       = $null
            }
        )
    }
    @{
        Type    = 'SERVER'
        Actions = @(
            @{
                ComputerName = 'SERVER1'
                Result       = $null
            }
            @{
                ComputerName = 'SERVER2'
                Result       = $null
            }
        )
    }
)

$outerScriptBlock = {
    $task = $_

    $innerScriptBlock = {
        $action = $_

        if (-not $MaxConcurrentJobs) {
            $task = $using:task
        }

        $action.Result = '{0} - {1}' -f $task.Type, $action.ComputerName
    }

    $innerForeachParams = @{
        Process = $innerScriptBlock
    }

    if (-not $MaxConcurrentJobs) {
        $innerForeachParams = @{
            Parallel      = $innerScriptBlock
            ThrottleLimit = $using:MaxConcurrentJobs
        }
    }

    $task.Actions | ForEach-Object @innerForeachParams
}

$foreachParams = @{
    Process = $outerScriptBlock
}

if ($MaxConcurrentJobs -gt 1) {
    $foreachParams = @{
        Parallel      = $outerScriptBlock
        ThrottleLimit = $MaxConcurrentJobs
    }
}

$Tasks | ForEach-Object @foreachParams

$Tasks.Actions.Result

Execute in terminal PowerShell Extension:

Step 1
  • Open file Test.ps1 with $MaxConcurrentJobs = 1 and press F5 # no error
  • Open file Test.ps1 with $MaxConcurrentJobs = 2 and press F5 # no error
Step 2
& 'T:\Test\Brecht\PowerShell\Test.ps1' -MaxConcurrentJobs 2 # no error 

Execute in a standard PowerShell 7 terminal:

Step 1
& 'T:\Test\Brecht\PowerShell\Test.ps1' -MaxConcurrentJobs 1 # no error
Step 2
& 'T:\Test\Brecht\PowerShell\Test.ps1' -MaxConcurrentJobs 2 # ERROR

image

Visuals

No response

Logs

No response

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 Reproduktion in Test.ps1 und führe sie sowohl über das PowerShell Extension-Terminal als auch über ein standardmäßiges PowerShell 7-Terminal mit den Werten 1 und 2 für MaxConcurrentJobs aus. Verfolge den Terminal- oder Debugging-Einstiegspunkt der Erweiterung, um festzustellen, warum sich der Gültigkeitsbereich bei verschachteltem ForEach-Object -Parallel unterscheidet. Als abgeschlossen gilt die Aufgabe, wenn sich dasselbe Skript in beiden Terminals konsistent verhält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
powershell, vscode
Bereich
developer-experience, devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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