Background Jobs Interfering with one another

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5272

    by the_hutch at 2012-11-13 09:32:51

    Hey guys. I've just recently started working with Powershell, mostly because of its capabilities with multi-threading.

    I've got a script that uses IP ranges to pull each system's hostname and MAC address (using nbtstat and nslookup). It works when I run the script as a single job. But when I try to run multiple threads as background jobs, they seem to interfere with one another. Each job starts, but it only fully generates the output for the last job. It seems like as each successive job starts, it cuts off the former.

    Are there some limitations to running multiple background jobs that I should be aware of? Thanks in advance.

    by Jason_Yoder_MCT at 2012-11-13 11:08:12

    the_hutch,

    Anyway I can look at the code?

    by the_hutch at 2012-11-13 13:16:16

    I figured out what I was doing wrong. For each job, the functionality depended on me piping an array ($Range1) into a ForEach loop. The problem was, I wasn't passing the array correctly to the scriptblock.

    I was doing this:

    Start-Job -ScriptBlock $job1 -ArgumentList ($Range1)

    Instead of this:

    Start-Job -ScriptBlock $job1 -ArgumentList (,$Range1)

    The difference being the comma at the beginning of the passed value. Without the comma, it just passes the first value in the array, instead of the entire thing. That's why I thought the job was starting in each case, but not finishing. The script seems to be working now. Thanks for the response.

You must be logged in to reply to this topic.