Using Start-Job with foreach RRS feed

  • Question

  • Hey Scripting Guy,

    What is the best way to run a script concurrently on multiple IP addresses? Currently, I'm using a foreach loop within the Start-Job command. The foreach loop pings a set of IP addresses in a CSV file, however, this does not start multiple jobs. Using the Start-Job within the foreach loop fails to read the set of IP addresses. I'm sure I'm missing something very obvious, but I can't quite figure what it is.

    Thank you for your time!

    • Moved by Bill_Stewart Monday, March 12, 2018 8:17 PM Abandoned
    Monday, January 15, 2018 6:28 PM

All replies

  • $jobs = foreach($computer in $computers) {
           Start-Job -ScriptBlock {
    		Param ($computer)
    Test-Connection $computer } -ArgumentList $computer } $jobs | Wait-Job


    • Edited by jrv Monday, January 15, 2018 6:44 PM
    Monday, January 15, 2018 6:44 PM
  • Hi jrv,

    Thank you for your response! So actually, I'm using this ping script as a test to be able to run a job on multiple computers. The actual job invokes some ssh commands, however, in order to test to make sure a script is running on multiple machines concurrently as a job, I'm doing the following in the hopes that the PingTest.txt will display the same time stamp for allmachines in the csv.

    $IPList = Get-Content 'C:\LinuxNVR\Sitelists\IPList.csv'
    ForEach ($IP in $IPList)
     Start-Job -ScriptBlock {
                 $([DateTime]::Now) | Out-File -FilePath C:\temp\PingTest.txt -Append
                   ping -n 30 $IP | Out-File -FilePath C:\temp\PingTest.txt -Append
                    }  -ArgumentList $IP #end start-job
    } #end foreach

    I keep getting "IP address must be specified" in my PingTest.txt file. Hope that makes sense!

    I still have a lot to learn in powershell, so please excuse me if this is not the correct approach for what I'm trying to do.

    Thank you!

    Monday, January 15, 2018 11:08 PM
  • Unfortunately you job only runs on the local computer.  Also $IP is never passed to the script block.

    I think you need to take some time to learn how PowerShell and its commands work.  You are now runnin multiple jobs on the local computer that all do nothing.

    start here: Learn PowerShell  

    Then read about how the command works:

    help Start-Job -Online

    Without some basic understanding of PowerShell you will not be able to ask a correct question and you will not be able to understand any of our answers.

    We have answered the question you have asked.  If this is  not what you want then you will have to learn enough to ask a clear question.  We cannot read your  mind.


    Monday, January 15, 2018 11:44 PM
  • Hi jrv,

    After reading up on how jobs work from the Hey, Scripting Guy! blog, I have a better understanding of how jobs work and got my script to work as needed! To tie up this post, here's how to run a job on multiple computers within a foreach loop that calls a computer list from a csv file:

    $computers = get-content "C:\ComputerList.csv"

    foreach ($computer in $computers) {

    Start-Job -ScriptBlock {

          param ([String[]]$computer)

          Do-Something $computer

       } -Argumentlist $computer #end Start-job scriptblock

    } #end foreach

    I used this script to open an SSH session and run a command on a VMWare machine. This seems to have worked perfectly for me, but please advise if there's anything I'm missing!

    Thank you!

    Thursday, January 18, 2018 8:31 PM
  • You code does not run one job.  It runs many jobs one for each computer in your list.  If that is what you want then you are OK.


    Thursday, January 18, 2018 8:43 PM
  • Yes sir! That is the intended usage.

    Thank you for your guidance.

    Thursday, January 18, 2018 11:16 PM