Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Increase number of cuncurrent powershell instance on VM?

I am trying to trigger a powershell workflow which should spin 10 threads in parallel. I am using PS version 4. Code -

Workflow CreateVMs
{  
    $i = 0
    #somecodehere...
    foreach -parallel -throttlelimit 10($i in 0..30)
    {
      #somemorecodehere...
      # INVOKING BATCH FILE USING POWERSHELL
     }
}

I am invoking batch files using powershell inside my inline script. What I observed in the task manager is, only 5 threads were active at a time-

enter image description here

May be the next thread getting picked up only one of the five is completed. I never saw more than 5 ps instance. However, When i checked the ps sessions allowed per user it is far more than 5.

enter image description here

How can I spin 10 threads of PS in parallel in a PS workflow. What am I missing here ?

like image 981
Aatif Akhter Avatar asked Apr 10 '17 12:04

Aatif Akhter


People also ask

What is Throttle limit in PowerShell?

Description of the documentation error. Per this issue, PowerShell/PowerShell#16898, we should clarify how the -ThrottleLimit parameter works in ForEach-Object -Parallel , especially when the parallel scripts are run as a job. The ThrottleLimit parameter limits the number of parallel scripts running at a time.

Can you run 2 PowerShell scripts in parallel?

Parallel foreach (PowerShell 7.0)You can run all scripts in parallel for each piped input object. If your script is crunching a lot of data over a significant period of time and if the machine you are running on has multiple cores that can host the script block threads.

Which cmdlet can you use to run a PowerShell command on multiple systems in parallel?

Normally, when you use the ForEach-Object cmdlet, each object piped to the cmdlet is processed sequentially. But with the new ForEach-Object -Parallel parameter set, you can run all script in parallel for each piped input object.


1 Answers

Given the following sample script:

Workflow Test-Workflow
{
    foreach -Parallel -ThrottleLimit 10 ( $Number in 1..20 )
    {
        $RandomSeconds = Get-Random -Minimum 10 -Maximum 60

        InlineScript
        {
            $String = '{0:s}: Starting number {1:D2} for {2:D2} seconds' -f (Get-Date),$Using:Number,$Using:RandomSeconds
            Write-Host -Object $String
        }

        Start-Sleep -Seconds $RandomSeconds

        InlineScript
        {
            $String = '{0:s}: Stopping number {1:D2} after {2:D2} seconds' -f (Get-Date),$Using:Number,$Using:RandomSeconds
            Write-Host -Object $String
        }
    }
}
Test-Workflow

You will get output similar to the following:

2017-11-28T13:27:34: Starting number 09 for 25 seconds
2017-11-28T13:27:34: Starting number 10 for 36 seconds
2017-11-28T13:27:34: Starting number 08 for 53 seconds
2017-11-28T13:27:35: Starting number 06 for 17 seconds
2017-11-28T13:27:35: Starting number 07 for 28 seconds
2017-11-28T13:27:35: Starting number 05 for 33 seconds
2017-11-28T13:27:35: Starting number 04 for 49 seconds
2017-11-28T13:27:35: Starting number 02 for 18 seconds
2017-11-28T13:27:35: Starting number 03 for 47 seconds
2017-11-28T13:27:35: Starting number 01 for 45 seconds
2017-11-28T13:27:52: Stopping number 06 after 17 seconds
2017-11-28T13:27:55: Starting number 11 for 49 seconds
2017-11-28T13:27:55: Stopping number 02 after 18 seconds
2017-11-28T13:27:55: Starting number 12 for 55 seconds
2017-11-28T13:28:00: Stopping number 09 after 25 seconds
2017-11-28T13:28:00: Starting number 13 for 37 seconds
2017-11-28T13:28:03: Stopping number 07 after 28 seconds
2017-11-28T13:28:03: Starting number 14 for 46 seconds
2017-11-28T13:28:08: Stopping number 05 after 33 seconds
2017-11-28T13:28:08: Starting number 15 for 48 seconds
2017-11-28T13:28:11: Stopping number 10 after 36 seconds
2017-11-28T13:28:11: Starting number 16 for 57 seconds
2017-11-28T13:28:21: Stopping number 01 after 45 seconds
2017-11-28T13:28:21: Starting number 17 for 22 seconds
2017-11-28T13:28:22: Stopping number 03 after 47 seconds
2017-11-28T13:28:22: Starting number 18 for 39 seconds
2017-11-28T13:28:24: Stopping number 04 after 49 seconds
2017-11-28T13:28:24: Starting number 19 for 34 seconds
2017-11-28T13:28:28: Stopping number 08 after 53 seconds
2017-11-28T13:28:28: Starting number 20 for 58 seconds
2017-11-28T13:28:37: Stopping number 13 after 37 seconds
2017-11-28T13:28:43: Stopping number 17 after 22 seconds
2017-11-28T13:28:44: Stopping number 11 after 49 seconds
2017-11-28T13:28:49: Stopping number 14 after 46 seconds
2017-11-28T13:28:50: Stopping number 12 after 55 seconds
2017-11-28T13:28:56: Stopping number 15 after 48 seconds
2017-11-28T13:28:58: Stopping number 19 after 34 seconds
2017-11-28T13:29:01: Stopping number 18 after 39 seconds
2017-11-28T13:29:08: Stopping number 16 after 57 seconds
2017-11-28T13:29:26: Stopping number 20 after 58 seconds

As you can see from the Output, 10 instances of the loop are running concurrently even though I do not have 10 powershell.exe instances running. You can use InlineScript like in my example above to visually see when your workflow is starting and/or stopping an instance of your loop.

like image 111
Shawn Esterman Avatar answered Sep 28 '22 04:09

Shawn Esterman