Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the new PowerShell 7 ForEach-Object Parallel implemented?

Tags:

PowerShell 7 introduced a much needed feature for running pipeline input in parallel.

The documentation for PowerShell 7 does not provide any detail on how this is implemented.

Having leveraged PoshRSJob and Invoke-Parallel modules before, I'm aware that runspaces were traditionally considered the much more efficient approach for parallel operations in powershell over running PowerShell jobs. I've read some mixed content indicating that this is using threading now and not runspaces, but can't find anything else specific.

I'd really appreciate some technical insight into:

  1. What is the lifecycle of an execution from a .NET perspective
  2. Is the new functionality runspaces or threads? (or is a runspace just a .NET thread in System.Management.Automation?)
  3. Does this bring about any complexity in traditional debugging now that we are moving into parallel operations? Historically I had a rough time debugging with runspaces, and not sure what options might have been improved
like image 837
sheldonhull Avatar asked Mar 24 '20 19:03

sheldonhull


People also ask

How use foreach loop in PowerShell?

Syntax. The part of the foreach statement enclosed in parenthesis represents a variable and a collection to iterate. PowerShell creates the variable $<item> automatically when the foreach loop runs. Prior to each iteration through the loop, the variable is set to a value in the collection.

Can PowerShell run commands in parallel?

PowerShell workflows provide a powerful way to run PowerShell modules and scripts against multiple servers in parallel. There are a lot of different ways in PowerShell to run scripts against multiple instances, but most methods simply run serially one server at a time.

What is the difference in the foreach Object cmdlet and the foreach scripting construct?

The core difference here is where you use the command, one is used in the midst of a pipeline, while the other starts its own pipeline. In production style scripts, I'd recommend using the ForEach keyword instead of the cmdlet.

What does $_ in PowerShell mean?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


1 Answers

Debugging foreach-object -parallel:

I need a second pwsh process to do it. In the first one do:

foreach-object -parallel { Wait-Debugger;1;2;3 }

Then in the second window, figure out what the pid of the other pwsh is. Then enter that pshostprocess. Look at the runspaces, and debug the one whose availability says "InBreakpoint". "v" means "step over".

get-process pwsh

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     64    44.32      82.23       1.70    3912  12 pwsh
     63    40.66      78.03       1.36    6472  12 pwsh

$pid
6472

Enter-PSHostProcess 3912

get-runspace

 Id Name            ComputerName    Type          State         Availability
 -- ----            ------------    ----          -----         ------------
  1 Runspace1       localhost       Local         Opened        Busy
  2 PSTask:1        localhost       Local         Opened        InBreakpoint
  3 RemoteHost      localhost       Local         Opened        Busy

debug-runspace 2
v
v
v

If you run foreach-object -parallel -asjob, you can use get-runspace and debug-runspace in the same window. But you couldn't see the output when stepping.

foreach-object -parallel { Wait-Debugger;1;2;3 } -asjob
get-runspace

 Id Name            ComputerName    Type          State         Availability
 -- ----            ------------    ----          -----         ------------
  1 Runspace1       localhost       Local         Opened        Available
  2 PSTask:1        localhost       Local         Opened        InBreakpoint

debug-runspace 2
v
v
v

Here's a new debugging video that has some advanced setups with Vscode: https://www.reddit.com/r/PowerShell/comments/gn0270/advanced_powershell_debugging_techniques/

like image 68
js2010 Avatar answered Sep 20 '22 17:09

js2010