locked
Please help with foreach -parallel RRS feed

  • Question

  • I want to retrieve information on hundreds AD computers

    Test.ps1

    #################

    $computerlist = gc .\Computers.txt

    foreach ($computer in $computerlist)

    { actions like get disk space,

    send email if disk space low

    }

    ###################

    Please help with this script Test.ps1 to use -parallel  workflow onto all computers simultaneously.

    -  first time to try with the -parallel workflow, please help. Thanks

    Wednesday, September 25, 2019 1:18 AM

All replies

  • help about_workflows

    Read the help and write a script.  There is\an example of what you are trying to do.

    Please carefully review the following links to set your expectation for posting in technical forums.

    This forum is also not for personal instruction.


    \_(ツ)_/

    Wednesday, September 25, 2019 1:27 AM
  • Invoke-command with multiple computers will run in parallel.  There is also an -asjob option.  Then all jobs will run as child jobs.

    Invoke-Command comp1,comp2,comp3 { pwd } -asjob
    get-job -includechildjob

    • Edited by JS2010 Wednesday, September 25, 2019 2:59 AM
    Wednesday, September 25, 2019 2:54 AM
  • Invoke-command with multiple computers will run in parallel.  There is also an -asjob option.  Then all jobs will run as child jobs.

    Invoke-Command comp1,comp2,comp3 { pwd } -asjob
    get-job -includechildjob

    No it doesn't. It runs one at a time.

    \_(ツ)_/

    Wednesday, September 25, 2019 4:10 AM
  • Invoke-command with multiple computers will run in parallel.  There is also an -asjob option.  Then all jobs will run as child jobs.

    Invoke-Command comp1,comp2,comp3 { pwd } -asjob
    get-job -includechildjob

    No it doesn't. It runs one at a time.

    \_(ツ)_/

    Yes, it does run in parallel.  In fact, there's a throttlelimit parameter that defaults to 32.

    measure-command { invoke-command a001,a002,a003 {sleep 10;
      "done $env:computername"} }


    Days              : 0
    Hours             : 0
    Minutes           : 0
    Seconds           : 10
    Milliseconds      : 562
    Ticks             : 105626750
    TotalDays         : 0.00012225318287037
    TotalHours        : 0.00293407638888889
    TotalMinutes      : 0.176044583333333
    TotalSeconds      : 10.562675
    TotalMilliseconds : 10562.675

    • Edited by JS2010 Wednesday, September 25, 2019 12:35 PM
    Wednesday, September 25, 2019 12:33 PM
  • Ok - I see you added "AsJob" to the command which will crate parallel jobs for remoted connections but only for remote commands.


    \_(ツ)_/

    Wednesday, September 25, 2019 6:04 PM
  • Ok - I see you added "AsJob" to the command which will crate parallel jobs for remoted connections but only for remote commands.


    \_(ツ)_/

    It's parallel with or without -asjob, as I have shown.

    Wednesday, September 25, 2019 6:06 PM
  • I might unnecessarily use the  example like "AD computer" to run the parallel commands.

    However Lets say 1000 "AD users" as $users. I want foreach -parallel ($user in $users) { something like change some AD attributes and export each user's certain information into individual $ReportPath\$user.txt }

    I read that I need to use workflow, and I need to pass $users and $ReportPath into workflow. 

    Any simple example? Thx

    Wednesday, September 25, 2019 10:44 PM
  • Always s5tart by reading the help carefully and completely.

    help about_workflows

    This forum is not for personal instructions in PowerShell.

    There are hundreds of articles on the net that will help you to learn this.  The help also has good examples.

    If you need personal instruction then there are many computer schools that now teach PowerShell.


    \_(ツ)_/

    Thursday, September 26, 2019 12:06 AM
  • Here's a workflow with a foreach -parallel:

    workflow work {
      foreach -parallel ($i in 1..3) { 
        sleep 5 
        "$i done" 
      }
    }
    
    work
    
    3 done
    1 done
    2 done


    Or a workflow with a parallel block:

    function sleepfor($time) { sleep $time; "sleepfor $time done"}
    
    workflow work {
      parallel {
        sleepfor 3
        sleepfor 2
        sleepfor 1
      }
      'hi'
    }
    
    work 
    
    sleepfor 1 done
    sleepfor 2 done
    sleepfor 3 done
    hi
    Thursday, September 26, 2019 4:27 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Thursday, October 3, 2019 2:19 AM