Will it be an issue in foreach -Parallel under my scenario.
See original GitHub issueSteps 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:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
This issue has been marked as duplicate and has not had any activity for 1 day. It has been closed for housekeeping purposes.