Powershell Multithreading with SQL Agent

  • Hello All,

    So I have 4 SQL Agent jobs that all run PowerShell scripts that utilize the multithreading abilities of PowerShell but I am running into issues.

    These 4 jobs are set to run every 5 minutes because they collect perfmon stats about our servers. Each job takes between 1-2:30 minutes to run to completion, each job is set to grab data for approximately 75 servers, and each PowerShell script is set to have a maximum of 6 threads running at a time. The issue I am encountering is SQL Agent will mark the job as failed if the PowerShell script reaches the end of the script but there are still active threads. I currently have it set up to check the total number of running threads and when that count reaches zero, it will finish the script. However, with all 4 of these jobs running, the script will count the total number of running threads from any of the scripts to calculate if the script is finished or not and this will get hung as the day goes on and the run times begin to overlap.

    Does anyone know of a good way to manage how many threads are running for a given SQL Agent job and only finish the script if the PowerShell threads for the current script have all finished executing?

  • I think that you are using the wrong technique.

    Dependent upon how you are creating the threads there are many synchronisation techniques available to .NET code but for PowerShell in particular there is the Wait-Job command.

    Get-Job | Wait-Job

    The documentation has the following to say about the above code: Suppresses the command prompt until one or all of the Windows PowerShell background jobs running in the session are complete.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply