ForeEach-Object Paraellel in PowerShell 7 - Debugging/Breakpoint

reuvygroovy 781 Reputation points
2020-08-30T11:57:16.733+00:00

We started testing out the features of PS7's parallel option for ForEach-Object items. One of the difficulties we had a hard time overcoming is how is one supposed to debug code (via ISE/VSCode) when setting a breakpoint?

For example, in the below code block I set a breakpoint on the inner Get-ChildItem (gci) and the code never stops but immediately runs to completion.

$a = Get-ChildItem

$a | ForEach-Object -Parallel {
    $b = Get-ChildItem $a -Recurse
    Write-Host $b.count
}
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,546 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. js2010 191 Reputation points
    2020-08-30T13:37:01.47+00:00

    foreach -parallel runs in a different runspace like start-threadjob.

    Adam Driscoll made a video on debugging runspaces. We actually talk in that reddit thread about foreach -parallel.
    https://www.reddit.com/r/PowerShell/comments/gn0270/advanced_powershell_debugging_techniques/

    Powershell 7.1 preview 6 has an experimental feature (it may get changed in production) to debug other runspaces: https://devblogs.microsoft.com/powershell/powershell-7-1-preview-6/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.