0

I want to test for the existence of a file path on many computers, using SMB. Many of these computers will be offline, and it takes several seconds for the SMB request to time out, so I want to run this command in parallel. Foreach -parallel, however, does not seem to be running in parallel. It seems to just go one computer at a time, running no faster than regular foreach.

This code gets a list of recently-logged-into computers. It then records whether the path \\$computer\users\foo\ exists on each of those computers.

workflow Find-FoosComputer
{
    $computers = Get-Adcomputer -filter * -properties name,LastLogon | where {$_.lastlogon -ne $NULL} | where {[datetime]($_.lastlogon) -le (get-date).AddDays(-14)}|select -expandproperty name
    foreach -parallel ($computer in $computers){
        ($computer, (Test-Path "\\$computer\users\foo\")) > "c:\temp\results.txt"
    }
}

What can I do to get foreach -parallel to actually run in parallel, and run test-path on all the computers at once?

Strill
  • 228
  • 3
  • 10
  • 1
    `foreach -parallel` from a `workflow` is useless. its slower than a regular loop. if you want multithreading look for `Start-ThreadJob` or `ForEach-Object -Parallel` if you have pwsh 7+. the function from this answer https://stackoverflow.com/questions/74257556/is-there-an-easier-way-to-run-commands-in-parallel-while-keeping-it-efficient-in can also help with multithreading – Santiago Squarzon Mar 03 '23 at 19:30
  • 1
    Good points, @Santiago, but I think you meant if you _don't_ have pwsh 7+, with respect to the linked solution. – mklement0 Apr 11 '23 at 22:23
  • 1
    Probably not quite sure what I meant @mklement0 – Santiago Squarzon Apr 11 '23 at 22:34

0 Answers0