question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Will it be an issue in foreach -Parallel under my scenario.

See original GitHub issue

Steps to reproduce


$sip= 25..135 | foreach {"10.10.10.$_"}
$ship | foreach -Parallel {if (Test-Connection -ComputerName $_ -Quiet -Count 2) {Add-Content -Path .\online.txt -Value $_} } -ThrottleLimit 10  -Verbose

Expected behavior

Every IP address that response to ICMP request will be added to online.txt

Actual behavior

The whole script runs without any error. But just wonder:

Script will test 10 computers at the same time. If the 10 testing finish at the same time, and try to add the IP address to online.txt at the same time, seems it will be a conflict, as 10 sessions try to access the same file, will it causes some sessions give up the addition of IP address.

Environment data

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tlsalexcommented, Dec 15, 2020

thanks ,now I do that in bellow way, hope PS team improve it more. $ship | foreach -Parallel {if (Test-Connection -ComputerName $_ -Quiet -Count 2 -Delay 1) {$_}} -ThrottleLimit 10 | tee -Path .\2online.txt

0reactions
msftbot[bot]commented, Dec 16, 2020

This issue has been marked as duplicate and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Should I always use Parallel.Foreach because more ...
The short answer is no, you should not just use Parallel.ForEach or related constructs on each loop that you can. Parallel has some...
Read more >
Potential Pitfalls in Data and Task Parallelism
ForEach can provide significant performance improvements over ordinary sequential loops. However, the work of parallelizing the loop introduces ...
Read more >
PowerShell ForEach-Object Parallel Feature
So it is possible to have a non thread-safe variable used in two scripts running on different threads, which can lead to unpredictable...
Read more >
Difference between Parallel ForEach and ForEach Activity
This video demonstrates how and when to use ForEach Activity and Parallel ForEach Activity with the use case scenario examples on both.
Read more >
parallel #csharp #dotnet - YouTube
Process took 36 hours to run on an i5, I improved and refactored the code a little to put it in a Parallel....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found