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?

3
  • 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 stackoverflow.com/questions/74257556/… can also help with multithreading Commented Mar 3, 2023 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. Commented Apr 11, 2023 at 22:23
  • 1
    Probably 😁 not quite sure what I meant @mklement0 Commented Apr 11, 2023 at 22:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.