How do I run my PowerShell scripts in parallel without using Jobs?How do I remotely issue a CLI command to...

How much cash can I safely carry into the USA and avoid civil forfeiture?

Work requires me to come in early to start computer but wont let me clock in to get paid for it

Apply a different color ramp to subset of categorized symbols in QGIS?

Drawing a german abacus as in the books of Adam Ries

What was Apollo 13's "Little Jolt" after MECO?

A ​Note ​on ​N!

Contradiction proof for inequality of P and NP?

"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?

Why did C use the -> operator instead of reusing the . operator?

I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?

"The cow" OR "a cow" OR "cows" in this context

Can I criticise the more senior developers around me for not writing clean code?

Is there metaphorical meaning of "aus der Haft entlassen"?

Help with my training data

How do I check if a string is entirely made of the same substring?

Crossed out red box fitting tightly around image

Suing a Police Officer Instead of the Police Department

Why do games have consumables?

Magical attacks and overcoming damage resistance

Does a large simulator bay have standard public address announcements?

Which big number is bigger?

Find a stone which is not the lightest one

Combinatorics problem, right solution?

Complex numbers z=-3-4i polar form



How do I run my PowerShell scripts in parallel without using Jobs?


How do I remotely issue a CLI command to thousands of Windows domain computers at once?How to run Azure cmdlets in background?How to retrieve data from powershell runspace jobHow to run PowerShell/PowerCli scripts on esxi 4?Method to integrate Powershell scripts with non-Windows workflow?Remote location management copy and install large software updates to 50 LANExecute Powershell Add-Computer remotely via Invoke-CommandPowershell Workflow ParallelWindows Remote Management Over Untrusted DomainsHow can I run this powershell script in parallel?Powershell DSC File copy - Workgroup machinesWhy does Get-Winevent in parallel workflow wants to use PSRemoting?Remote Powershell for non-admin user in non-domain pc






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







26















If I have a script that I need to run against multiple computers, or with multiple different arguments, how can I execute it in parallel, without having to incur the overhead of spawning a new PSJob with Start-Job?



As an example, I want to re-sync the time on all domain members, like so:



$computers = Get-ADComputer -filter * |Select-Object -ExpandProperty dnsHostName
$creds = Get-Credential domainuser
foreach($computer in $computers)
{
$session = New-PSSession -ComputerName $computer -Credential $creds
Invoke-Command -Session $session -ScriptBlock { w32tm /resync /nowait /rediscover }
}


But I don't want to wait for each PSSession to connect and invoke the command. How can this be done in parallel, without Jobs?










share|improve this question































    26















    If I have a script that I need to run against multiple computers, or with multiple different arguments, how can I execute it in parallel, without having to incur the overhead of spawning a new PSJob with Start-Job?



    As an example, I want to re-sync the time on all domain members, like so:



    $computers = Get-ADComputer -filter * |Select-Object -ExpandProperty dnsHostName
    $creds = Get-Credential domainuser
    foreach($computer in $computers)
    {
    $session = New-PSSession -ComputerName $computer -Credential $creds
    Invoke-Command -Session $session -ScriptBlock { w32tm /resync /nowait /rediscover }
    }


    But I don't want to wait for each PSSession to connect and invoke the command. How can this be done in parallel, without Jobs?










    share|improve this question



























      26












      26








      26


      15






      If I have a script that I need to run against multiple computers, or with multiple different arguments, how can I execute it in parallel, without having to incur the overhead of spawning a new PSJob with Start-Job?



      As an example, I want to re-sync the time on all domain members, like so:



      $computers = Get-ADComputer -filter * |Select-Object -ExpandProperty dnsHostName
      $creds = Get-Credential domainuser
      foreach($computer in $computers)
      {
      $session = New-PSSession -ComputerName $computer -Credential $creds
      Invoke-Command -Session $session -ScriptBlock { w32tm /resync /nowait /rediscover }
      }


      But I don't want to wait for each PSSession to connect and invoke the command. How can this be done in parallel, without Jobs?










      share|improve this question
















      If I have a script that I need to run against multiple computers, or with multiple different arguments, how can I execute it in parallel, without having to incur the overhead of spawning a new PSJob with Start-Job?



      As an example, I want to re-sync the time on all domain members, like so:



      $computers = Get-ADComputer -filter * |Select-Object -ExpandProperty dnsHostName
      $creds = Get-Credential domainuser
      foreach($computer in $computers)
      {
      $session = New-PSSession -ComputerName $computer -Credential $creds
      Invoke-Command -Session $session -ScriptBlock { w32tm /resync /nowait /rediscover }
      }


      But I don't want to wait for each PSSession to connect and invoke the command. How can this be done in parallel, without Jobs?







      performance powershell automation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 13 '17 at 12:14









      Community

      1




      1










      asked Sep 6 '14 at 13:56









      Mathias R. JessenMathias R. Jessen

      22.7k35189




      22.7k35189






















          4 Answers
          4






          active

          oldest

          votes


















          47














          Update - While this answer explains the process and mechanics of PowerShell runspaces and how they can help you multi-thread non-sequential workloads, fellow PowerShell aficionado Warren 'Cookie Monster' F has gone the extra mile and incorporated these same concepts into a single tool called Invoke-Parallel - it does what I describe below, and he has since expanded it with optional switches for logging and prepared session state including imported modules, really cool stuff - I strongly recommend you check it out before building you own shiny solution!





          With Parallel Runspace execution:



          Reducing inescapable waiting time



          In the original specific case, the executable invoked has a /nowait option which prevents blocking the invoking thread while the job (in this case, time re-synchronization) finishes on its own.



          This greatly reduces the overall execution time from the issuers perspective, but connecting to each machine is still done in sequential order. Connecting to thousands of clients in sequence may take a long time depending on the number of machines that are for one reason or another inaccessible, due to an accumulation of timeout waits.



          To get around having to queue up all subsequent connections in case of a single or a few consecutive timeouts, we can dispatch the job of connecting and invoking commands to separate PowerShell Runspaces, executing in parallel.



          What is a Runspace?



          A Runspace is the virtual container in which your powershell code executes, and represents/holds the Environment from the perspective of a PowerShell statement/command.



          In broad terms, 1 Runspace = 1 thread of execution, so all we need to "multi-thread" our PowerShell script is a collection of Runspaces that can then in turn execute in parallel.



          Like the original problem, the job of invoking commands multiple runspaces can be broken down into:




          1. Creating a RunspacePool

          2. Assigning a PowerShell script or an equivalent piece of executable code to the RunspacePool

          3. Invoke the code asynchronously (ie. not having to wait for the code to return)


          RunspacePool template



          PowerShell has a type accelerator called [RunspaceFactory] that will assist us in the creation of runspace components - let's put it to work



          1. Create a RunspacePool and Open() it:



          $RunspacePool = [runspacefactory]::CreateRunspacePool(1,8)
          $RunspacePool.Open()


          The two arguments passed to CreateRunspacePool(), 1 and 8 is the minimum and maximum number of runspaces allowed to execute at any given time, giving us an effective maximum degree of parallelism of 8.



          2. Create an instance of PowerShell, attach some executable code to it and assign it to our RunspacePool:



          An instance of PowerShell is not the same as the powershell.exe process (which is really a Host application), but an internal runtime object representing the PowerShell code to execute. We can use the [powershell] type accelerator to create a new PowerShell instance within PowerShell:



          $Code = {
          param($Credentials,$ComputerName)
          $session = New-PSSession -ComputerName $ComputerName -Credential $Credentials
          Invoke-Command -Session $session -ScriptBlock {w32tm /resync /nowait /rediscover}
          }
          $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($creds).AddArgument("computer1.domain.tld")
          $PSinstance.RunspacePool = $RunspacePool


          3. Invoke the PowerShell instance asynchronously using APM:



          Using what is known in .NET development terminology as the Asynchronous Programming Model, we can split the invocation of a command into a Begin method, for giving a "green light" to execute the code, and an End method to collect the results. Since we in this case are not really interested in any feedback (we don't wait for the output from w32tm anyways), we can make due by simply calling the first method



          $PSinstance.BeginInvoke()




          Wrapping it up in a RunspacePool



          Using the above technique, we can wrap the sequential iterations of creating new connections and invoking the remote command in a parallel execution flow:



          $ComputerNames = Get-ADComputer -filter * -Properties dnsHostName |select -Expand dnsHostName

          $Code = {
          param($Credentials,$ComputerName)
          $session = New-PSSession -ComputerName $ComputerName -Credential $Credentials
          Invoke-Command -Session $session -ScriptBlock {w32tm /resync /nowait /rediscover}
          }

          $creds = Get-Credential domainuser

          $rsPool = [runspacefactory]::CreateRunspacePool(1,8)
          $rsPool.Open()

          foreach($ComputerName in $ComputerNames)
          {
          $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($creds).AddArgument($ComputerName)
          $PSinstance.RunspacePool = $rsPool
          $PSinstance.BeginInvoke()
          }


          Assuming that the CPU has the capacity to execute all 8 runspaces at once, we should be able to see that the execution time is greatly reduced, but at the cost of readability of the script due to the rather "advanced" methods used.





          Determining the optimum degree of parallism:



          We could easily create a RunspacePool that allows for the execution of a 100 runspaces at the same time:



          [runspacefactory]::CreateRunspacePool(1,100)


          But at the end of the day, it all comes down to how many units of execution our local CPU can handle. In other words, as long as your code is executing, it does not make sense to allow more runspaces than you have logical processors to dispatch execution of code to.



          Thanks to WMI, this threshold is fairly easy to determine:



          $NumberOfLogicalProcessor = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
          [runspacefactory]::CreateRunspacePool(1,$NumberOfLogicalProcessors)


          If, on the other hand, the code you are executing itself incurs a lot of wait time due to external factors like network latency, you can still benefit from running more simultanous runspaces than you have logical processors, so you'd probably want to test of range possible maximum runspaces to find break-even:



          foreach($n in ($NumberOfLogicalProcessors..($NumberOfLogicalProcessors*3)))
          {
          Write-Host "$n: " -NoNewLine
          (Measure-Command {
          $Computers = Get-ADComputer -filter * -Properties dnsHostName |select -Expand dnsHostName -First 100
          ...
          [runspacefactory]::CreateRunspacePool(1,$n)
          ...
          }).TotalSeconds
          }





          share|improve this answer





















          • 4





            If the jobs are waiting on the network, e.g. you are running PowerShell commands on remote computers, you could easily go far over the number of logical processors before you hit any CPU bottleneck.

            – Michael Hampton
            Sep 6 '14 at 14:00













          • Well, that is true. Changed it a bit and provided an example for testing

            – Mathias R. Jessen
            Sep 6 '14 at 14:09











          • How to make sure all the job is done at the end? (May need to something after all the script blocks finished)

            – sjzls
            Nov 18 '14 at 18:04











          • @NickW Great question. I'll do a follow-up on tracking the jobs and "harvesting" potential output later today, stay tuned

            – Mathias R. Jessen
            Nov 19 '14 at 12:23






          • 1





            @MathiasR.Jessen Very well-written answer! Looking forward to the update.

            – Signal15
            Dec 3 '14 at 16:55



















          5














          Adding to this discussion, what's missing is a collector to store the data that is created from the runspace, and a variable to check the status of the runspace, i.e. is it completed or not.



          #Add an collector object that will store the data
          $Object = New-Object 'System.Management.Automation.PSDataCollection[psobject]'

          #Create a variable to check the status
          $Handle = $PSinstance.BeginInvoke($Object,$Object)

          #So if you want to check the status simply type:
          $Handle

          #If you want to see the data collected, type:
          $Object





          share|improve this answer

































            3














            Check out PoshRSJob. It provides same/similar functions as the native *-Job functions, but uses Runspaces which tend to be much quicker and more responsive than the standard Powershell jobs.






            share|improve this answer































              0














              @mathias-r-jessen has a great answer though there are details I'd like to add.



              Max Threads



              In theory threads should be limited by the number of system processors. However, while testing AsyncTcpScan I achieved far better performance by choosing a much larger value for MaxThreads. Thus why that module has a -MaxThreads input parameter. Keep in mind that allocating too many threads will hinder performance.



              Returning Data



              Getting data back from the ScriptBlock is tricky. I've updated the OP code and integrated it into what was used for AsyncTcpScan.




              WARNING: I wasn't able to test the following code. I made some changes
              to the OP script based on my experience working with the Active
              Directory cmdlets.




              # Script to run in each thread.
              [System.Management.Automation.ScriptBlock]$ScriptBlock = {

              $result = New-Object PSObject -Property @{ 'Computer' = $args[0];
              'Success' = $false; }

              try {
              $session = New-PSSession -ComputerName $args[0] -Credential $args[1]
              Invoke-Command -Session $session -ScriptBlock { w32tm /resync /nowait /rediscover }
              Disconnect-PSSession -Session $session
              $result.Success = $true
              } catch {

              }

              return $result

              } # End Scriptblock

              function Invoke-AsyncJob
              {
              [CmdletBinding()]
              param(
              [parameter(Mandatory=$true)]
              [System.Management.Automation.PSCredential]
              # Credential object to login to remote systems
              $Credentials
              )

              Import-Module ActiveDirectory

              $Results = @()

              $AllJobs = New-Object System.Collections.ArrayList

              $AllDomainComputers = Get-ADComputer -Filter * -Property dnsHostName

              $HostRunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(2,10,$Host)

              $HostRunspacePool.Open()

              foreach($DomainComputer in $AllDomainComputers)
              {
              $asyncJob = [System.Management.Automation.PowerShell]::Create().AddScript($ScriptBlock).AddParameters($($($DomainComputer.dnsName),$Credentials))

              $asyncJob.RunspacePool = $HostRunspacePool

              $asyncJobObj = @{ JobHandle = $asyncJob;
              AsyncHandle = $asyncJob.BeginInvoke() }

              $AllJobs.Add($asyncJobObj) | Out-Null
              }

              $ProcessingJobs = $true

              Do {

              $CompletedJobs = $AllJobs | Where-Object { $_.AsyncHandle.IsCompleted }

              if($null -ne $CompletedJobs)
              {
              foreach($job in $CompletedJobs)
              {
              $result = $job.JobHandle.EndInvoke($job.AsyncHandle)

              if($null -ne $result)
              {
              $Results += $result
              }

              $job.JobHandle.Dispose()

              $AllJobs.Remove($job)
              }

              } else {

              if($AllJobs.Count -eq 0)
              {
              $ProcessingJobs = $false

              } else {

              Start-Sleep -Milliseconds 500
              }
              }

              } While ($ProcessingJobs)

              $HostRunspacePool.Close()
              $HostRunspacePool.Dispose()

              return $Results

              } # End function Invoke-AsyncJob




              share








              New contributor




              phbits is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





















                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "2"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f626711%2fhow-do-i-run-my-powershell-scripts-in-parallel-without-using-jobs%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                47














                Update - While this answer explains the process and mechanics of PowerShell runspaces and how they can help you multi-thread non-sequential workloads, fellow PowerShell aficionado Warren 'Cookie Monster' F has gone the extra mile and incorporated these same concepts into a single tool called Invoke-Parallel - it does what I describe below, and he has since expanded it with optional switches for logging and prepared session state including imported modules, really cool stuff - I strongly recommend you check it out before building you own shiny solution!





                With Parallel Runspace execution:



                Reducing inescapable waiting time



                In the original specific case, the executable invoked has a /nowait option which prevents blocking the invoking thread while the job (in this case, time re-synchronization) finishes on its own.



                This greatly reduces the overall execution time from the issuers perspective, but connecting to each machine is still done in sequential order. Connecting to thousands of clients in sequence may take a long time depending on the number of machines that are for one reason or another inaccessible, due to an accumulation of timeout waits.



                To get around having to queue up all subsequent connections in case of a single or a few consecutive timeouts, we can dispatch the job of connecting and invoking commands to separate PowerShell Runspaces, executing in parallel.



                What is a Runspace?



                A Runspace is the virtual container in which your powershell code executes, and represents/holds the Environment from the perspective of a PowerShell statement/command.



                In broad terms, 1 Runspace = 1 thread of execution, so all we need to "multi-thread" our PowerShell script is a collection of Runspaces that can then in turn execute in parallel.



                Like the original problem, the job of invoking commands multiple runspaces can be broken down into:




                1. Creating a RunspacePool

                2. Assigning a PowerShell script or an equivalent piece of executable code to the RunspacePool

                3. Invoke the code asynchronously (ie. not having to wait for the code to return)


                RunspacePool template



                PowerShell has a type accelerator called [RunspaceFactory] that will assist us in the creation of runspace components - let's put it to work



                1. Create a RunspacePool and Open() it:



                $RunspacePool = [runspacefactory]::CreateRunspacePool(1,8)
                $RunspacePool.Open()


                The two arguments passed to CreateRunspacePool(), 1 and 8 is the minimum and maximum number of runspaces allowed to execute at any given time, giving us an effective maximum degree of parallelism of 8.



                2. Create an instance of PowerShell, attach some executable code to it and assign it to our RunspacePool:



                An instance of PowerShell is not the same as the powershell.exe process (which is really a Host application), but an internal runtime object representing the PowerShell code to execute. We can use the [powershell] type accelerator to create a new PowerShell instance within PowerShell:



                $Code = {
                param($Credentials,$ComputerName)
                $session = New-PSSession -ComputerName $ComputerName -Credential $Credentials
                Invoke-Command -Session $session -ScriptBlock {w32tm /resync /nowait /rediscover}
                }
                $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($creds).AddArgument("computer1.domain.tld")
                $PSinstance.RunspacePool = $RunspacePool


                3. Invoke the PowerShell instance asynchronously using APM:



                Using what is known in .NET development terminology as the Asynchronous Programming Model, we can split the invocation of a command into a Begin method, for giving a "green light" to execute the code, and an End method to collect the results. Since we in this case are not really interested in any feedback (we don't wait for the output from w32tm anyways), we can make due by simply calling the first method



                $PSinstance.BeginInvoke()




                Wrapping it up in a RunspacePool



                Using the above technique, we can wrap the sequential iterations of creating new connections and invoking the remote command in a parallel execution flow:



                $ComputerNames = Get-ADComputer -filter * -Properties dnsHostName |select -Expand dnsHostName

                $Code = {
                param($Credentials,$ComputerName)
                $session = New-PSSession -ComputerName $ComputerName -Credential $Credentials
                Invoke-Command -Session $session -ScriptBlock {w32tm /resync /nowait /rediscover}
                }

                $creds = Get-Credential domainuser

                $rsPool = [runspacefactory]::CreateRunspacePool(1,8)
                $rsPool.Open()

                foreach($ComputerName in $ComputerNames)
                {
                $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($creds).AddArgument($ComputerName)
                $PSinstance.RunspacePool = $rsPool
                $PSinstance.BeginInvoke()
                }


                Assuming that the CPU has the capacity to execute all 8 runspaces at once, we should be able to see that the execution time is greatly reduced, but at the cost of readability of the script due to the rather "advanced" methods used.





                Determining the optimum degree of parallism:



                We could easily create a RunspacePool that allows for the execution of a 100 runspaces at the same time:



                [runspacefactory]::CreateRunspacePool(1,100)


                But at the end of the day, it all comes down to how many units of execution our local CPU can handle. In other words, as long as your code is executing, it does not make sense to allow more runspaces than you have logical processors to dispatch execution of code to.



                Thanks to WMI, this threshold is fairly easy to determine:



                $NumberOfLogicalProcessor = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
                [runspacefactory]::CreateRunspacePool(1,$NumberOfLogicalProcessors)


                If, on the other hand, the code you are executing itself incurs a lot of wait time due to external factors like network latency, you can still benefit from running more simultanous runspaces than you have logical processors, so you'd probably want to test of range possible maximum runspaces to find break-even:



                foreach($n in ($NumberOfLogicalProcessors..($NumberOfLogicalProcessors*3)))
                {
                Write-Host "$n: " -NoNewLine
                (Measure-Command {
                $Computers = Get-ADComputer -filter * -Properties dnsHostName |select -Expand dnsHostName -First 100
                ...
                [runspacefactory]::CreateRunspacePool(1,$n)
                ...
                }).TotalSeconds
                }





                share|improve this answer





















                • 4





                  If the jobs are waiting on the network, e.g. you are running PowerShell commands on remote computers, you could easily go far over the number of logical processors before you hit any CPU bottleneck.

                  – Michael Hampton
                  Sep 6 '14 at 14:00













                • Well, that is true. Changed it a bit and provided an example for testing

                  – Mathias R. Jessen
                  Sep 6 '14 at 14:09











                • How to make sure all the job is done at the end? (May need to something after all the script blocks finished)

                  – sjzls
                  Nov 18 '14 at 18:04











                • @NickW Great question. I'll do a follow-up on tracking the jobs and "harvesting" potential output later today, stay tuned

                  – Mathias R. Jessen
                  Nov 19 '14 at 12:23






                • 1





                  @MathiasR.Jessen Very well-written answer! Looking forward to the update.

                  – Signal15
                  Dec 3 '14 at 16:55
















                47














                Update - While this answer explains the process and mechanics of PowerShell runspaces and how they can help you multi-thread non-sequential workloads, fellow PowerShell aficionado Warren 'Cookie Monster' F has gone the extra mile and incorporated these same concepts into a single tool called Invoke-Parallel - it does what I describe below, and he has since expanded it with optional switches for logging and prepared session state including imported modules, really cool stuff - I strongly recommend you check it out before building you own shiny solution!





                With Parallel Runspace execution:



                Reducing inescapable waiting time



                In the original specific case, the executable invoked has a /nowait option which prevents blocking the invoking thread while the job (in this case, time re-synchronization) finishes on its own.



                This greatly reduces the overall execution time from the issuers perspective, but connecting to each machine is still done in sequential order. Connecting to thousands of clients in sequence may take a long time depending on the number of machines that are for one reason or another inaccessible, due to an accumulation of timeout waits.



                To get around having to queue up all subsequent connections in case of a single or a few consecutive timeouts, we can dispatch the job of connecting and invoking commands to separate PowerShell Runspaces, executing in parallel.



                What is a Runspace?



                A Runspace is the virtual container in which your powershell code executes, and represents/holds the Environment from the perspective of a PowerShell statement/command.



                In broad terms, 1 Runspace = 1 thread of execution, so all we need to "multi-thread" our PowerShell script is a collection of Runspaces that can then in turn execute in parallel.



                Like the original problem, the job of invoking commands multiple runspaces can be broken down into:




                1. Creating a RunspacePool

                2. Assigning a PowerShell script or an equivalent piece of executable code to the RunspacePool

                3. Invoke the code asynchronously (ie. not having to wait for the code to return)


                RunspacePool template



                PowerShell has a type accelerator called [RunspaceFactory] that will assist us in the creation of runspace components - let's put it to work



                1. Create a RunspacePool and Open() it:



                $RunspacePool = [runspacefactory]::CreateRunspacePool(1,8)
                $RunspacePool.Open()


                The two arguments passed to CreateRunspacePool(), 1 and 8 is the minimum and maximum number of runspaces allowed to execute at any given time, giving us an effective maximum degree of parallelism of 8.



                2. Create an instance of PowerShell, attach some executable code to it and assign it to our RunspacePool:



                An instance of PowerShell is not the same as the powershell.exe process (which is really a Host application), but an internal runtime object representing the PowerShell code to execute. We can use the [powershell] type accelerator to create a new PowerShell instance within PowerShell:



                $Code = {
                param($Credentials,$ComputerName)
                $session = New-PSSession -ComputerName $ComputerName -Credential $Credentials
                Invoke-Command -Session $session -ScriptBlock {w32tm /resync /nowait /rediscover}
                }
                $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($creds).AddArgument("computer1.domain.tld")
                $PSinstance.RunspacePool = $RunspacePool


                3. Invoke the PowerShell instance asynchronously using APM:



                Using what is known in .NET development terminology as the Asynchronous Programming Model, we can split the invocation of a command into a Begin method, for giving a "green light" to execute the code, and an End method to collect the results. Since we in this case are not really interested in any feedback (we don't wait for the output from w32tm anyways), we can make due by simply calling the first method



                $PSinstance.BeginInvoke()




                Wrapping it up in a RunspacePool



                Using the above technique, we can wrap the sequential iterations of creating new connections and invoking the remote command in a parallel execution flow:



                $ComputerNames = Get-ADComputer -filter * -Properties dnsHostName |select -Expand dnsHostName

                $Code = {
                param($Credentials,$ComputerName)
                $session = New-PSSession -ComputerName $ComputerName -Credential $Credentials
                Invoke-Command -Session $session -ScriptBlock {w32tm /resync /nowait /rediscover}
                }

                $creds = Get-Credential domainuser

                $rsPool = [runspacefactory]::CreateRunspacePool(1,8)
                $rsPool.Open()

                foreach($ComputerName in $ComputerNames)
                {
                $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($creds).AddArgument($ComputerName)
                $PSinstance.RunspacePool = $rsPool
                $PSinstance.BeginInvoke()
                }


                Assuming that the CPU has the capacity to execute all 8 runspaces at once, we should be able to see that the execution time is greatly reduced, but at the cost of readability of the script due to the rather "advanced" methods used.





                Determining the optimum degree of parallism:



                We could easily create a RunspacePool that allows for the execution of a 100 runspaces at the same time:



                [runspacefactory]::CreateRunspacePool(1,100)


                But at the end of the day, it all comes down to how many units of execution our local CPU can handle. In other words, as long as your code is executing, it does not make sense to allow more runspaces than you have logical processors to dispatch execution of code to.



                Thanks to WMI, this threshold is fairly easy to determine:



                $NumberOfLogicalProcessor = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
                [runspacefactory]::CreateRunspacePool(1,$NumberOfLogicalProcessors)


                If, on the other hand, the code you are executing itself incurs a lot of wait time due to external factors like network latency, you can still benefit from running more simultanous runspaces than you have logical processors, so you'd probably want to test of range possible maximum runspaces to find break-even:



                foreach($n in ($NumberOfLogicalProcessors..($NumberOfLogicalProcessors*3)))
                {
                Write-Host "$n: " -NoNewLine
                (Measure-Command {
                $Computers = Get-ADComputer -filter * -Properties dnsHostName |select -Expand dnsHostName -First 100
                ...
                [runspacefactory]::CreateRunspacePool(1,$n)
                ...
                }).TotalSeconds
                }





                share|improve this answer





















                • 4





                  If the jobs are waiting on the network, e.g. you are running PowerShell commands on remote computers, you could easily go far over the number of logical processors before you hit any CPU bottleneck.

                  – Michael Hampton
                  Sep 6 '14 at 14:00













                • Well, that is true. Changed it a bit and provided an example for testing

                  – Mathias R. Jessen
                  Sep 6 '14 at 14:09











                • How to make sure all the job is done at the end? (May need to something after all the script blocks finished)

                  – sjzls
                  Nov 18 '14 at 18:04











                • @NickW Great question. I'll do a follow-up on tracking the jobs and "harvesting" potential output later today, stay tuned

                  – Mathias R. Jessen
                  Nov 19 '14 at 12:23






                • 1





                  @MathiasR.Jessen Very well-written answer! Looking forward to the update.

                  – Signal15
                  Dec 3 '14 at 16:55














                47












                47








                47







                Update - While this answer explains the process and mechanics of PowerShell runspaces and how they can help you multi-thread non-sequential workloads, fellow PowerShell aficionado Warren 'Cookie Monster' F has gone the extra mile and incorporated these same concepts into a single tool called Invoke-Parallel - it does what I describe below, and he has since expanded it with optional switches for logging and prepared session state including imported modules, really cool stuff - I strongly recommend you check it out before building you own shiny solution!





                With Parallel Runspace execution:



                Reducing inescapable waiting time



                In the original specific case, the executable invoked has a /nowait option which prevents blocking the invoking thread while the job (in this case, time re-synchronization) finishes on its own.



                This greatly reduces the overall execution time from the issuers perspective, but connecting to each machine is still done in sequential order. Connecting to thousands of clients in sequence may take a long time depending on the number of machines that are for one reason or another inaccessible, due to an accumulation of timeout waits.



                To get around having to queue up all subsequent connections in case of a single or a few consecutive timeouts, we can dispatch the job of connecting and invoking commands to separate PowerShell Runspaces, executing in parallel.



                What is a Runspace?



                A Runspace is the virtual container in which your powershell code executes, and represents/holds the Environment from the perspective of a PowerShell statement/command.



                In broad terms, 1 Runspace = 1 thread of execution, so all we need to "multi-thread" our PowerShell script is a collection of Runspaces that can then in turn execute in parallel.



                Like the original problem, the job of invoking commands multiple runspaces can be broken down into:




                1. Creating a RunspacePool

                2. Assigning a PowerShell script or an equivalent piece of executable code to the RunspacePool

                3. Invoke the code asynchronously (ie. not having to wait for the code to return)


                RunspacePool template



                PowerShell has a type accelerator called [RunspaceFactory] that will assist us in the creation of runspace components - let's put it to work



                1. Create a RunspacePool and Open() it:



                $RunspacePool = [runspacefactory]::CreateRunspacePool(1,8)
                $RunspacePool.Open()


                The two arguments passed to CreateRunspacePool(), 1 and 8 is the minimum and maximum number of runspaces allowed to execute at any given time, giving us an effective maximum degree of parallelism of 8.



                2. Create an instance of PowerShell, attach some executable code to it and assign it to our RunspacePool:



                An instance of PowerShell is not the same as the powershell.exe process (which is really a Host application), but an internal runtime object representing the PowerShell code to execute. We can use the [powershell] type accelerator to create a new PowerShell instance within PowerShell:



                $Code = {
                param($Credentials,$ComputerName)
                $session = New-PSSession -ComputerName $ComputerName -Credential $Credentials
                Invoke-Command -Session $session -ScriptBlock {w32tm /resync /nowait /rediscover}
                }
                $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($creds).AddArgument("computer1.domain.tld")
                $PSinstance.RunspacePool = $RunspacePool


                3. Invoke the PowerShell instance asynchronously using APM:



                Using what is known in .NET development terminology as the Asynchronous Programming Model, we can split the invocation of a command into a Begin method, for giving a "green light" to execute the code, and an End method to collect the results. Since we in this case are not really interested in any feedback (we don't wait for the output from w32tm anyways), we can make due by simply calling the first method



                $PSinstance.BeginInvoke()




                Wrapping it up in a RunspacePool



                Using the above technique, we can wrap the sequential iterations of creating new connections and invoking the remote command in a parallel execution flow:



                $ComputerNames = Get-ADComputer -filter * -Properties dnsHostName |select -Expand dnsHostName

                $Code = {
                param($Credentials,$ComputerName)
                $session = New-PSSession -ComputerName $ComputerName -Credential $Credentials
                Invoke-Command -Session $session -ScriptBlock {w32tm /resync /nowait /rediscover}
                }

                $creds = Get-Credential domainuser

                $rsPool = [runspacefactory]::CreateRunspacePool(1,8)
                $rsPool.Open()

                foreach($ComputerName in $ComputerNames)
                {
                $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($creds).AddArgument($ComputerName)
                $PSinstance.RunspacePool = $rsPool
                $PSinstance.BeginInvoke()
                }


                Assuming that the CPU has the capacity to execute all 8 runspaces at once, we should be able to see that the execution time is greatly reduced, but at the cost of readability of the script due to the rather "advanced" methods used.





                Determining the optimum degree of parallism:



                We could easily create a RunspacePool that allows for the execution of a 100 runspaces at the same time:



                [runspacefactory]::CreateRunspacePool(1,100)


                But at the end of the day, it all comes down to how many units of execution our local CPU can handle. In other words, as long as your code is executing, it does not make sense to allow more runspaces than you have logical processors to dispatch execution of code to.



                Thanks to WMI, this threshold is fairly easy to determine:



                $NumberOfLogicalProcessor = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
                [runspacefactory]::CreateRunspacePool(1,$NumberOfLogicalProcessors)


                If, on the other hand, the code you are executing itself incurs a lot of wait time due to external factors like network latency, you can still benefit from running more simultanous runspaces than you have logical processors, so you'd probably want to test of range possible maximum runspaces to find break-even:



                foreach($n in ($NumberOfLogicalProcessors..($NumberOfLogicalProcessors*3)))
                {
                Write-Host "$n: " -NoNewLine
                (Measure-Command {
                $Computers = Get-ADComputer -filter * -Properties dnsHostName |select -Expand dnsHostName -First 100
                ...
                [runspacefactory]::CreateRunspacePool(1,$n)
                ...
                }).TotalSeconds
                }





                share|improve this answer















                Update - While this answer explains the process and mechanics of PowerShell runspaces and how they can help you multi-thread non-sequential workloads, fellow PowerShell aficionado Warren 'Cookie Monster' F has gone the extra mile and incorporated these same concepts into a single tool called Invoke-Parallel - it does what I describe below, and he has since expanded it with optional switches for logging and prepared session state including imported modules, really cool stuff - I strongly recommend you check it out before building you own shiny solution!





                With Parallel Runspace execution:



                Reducing inescapable waiting time



                In the original specific case, the executable invoked has a /nowait option which prevents blocking the invoking thread while the job (in this case, time re-synchronization) finishes on its own.



                This greatly reduces the overall execution time from the issuers perspective, but connecting to each machine is still done in sequential order. Connecting to thousands of clients in sequence may take a long time depending on the number of machines that are for one reason or another inaccessible, due to an accumulation of timeout waits.



                To get around having to queue up all subsequent connections in case of a single or a few consecutive timeouts, we can dispatch the job of connecting and invoking commands to separate PowerShell Runspaces, executing in parallel.



                What is a Runspace?



                A Runspace is the virtual container in which your powershell code executes, and represents/holds the Environment from the perspective of a PowerShell statement/command.



                In broad terms, 1 Runspace = 1 thread of execution, so all we need to "multi-thread" our PowerShell script is a collection of Runspaces that can then in turn execute in parallel.



                Like the original problem, the job of invoking commands multiple runspaces can be broken down into:




                1. Creating a RunspacePool

                2. Assigning a PowerShell script or an equivalent piece of executable code to the RunspacePool

                3. Invoke the code asynchronously (ie. not having to wait for the code to return)


                RunspacePool template



                PowerShell has a type accelerator called [RunspaceFactory] that will assist us in the creation of runspace components - let's put it to work



                1. Create a RunspacePool and Open() it:



                $RunspacePool = [runspacefactory]::CreateRunspacePool(1,8)
                $RunspacePool.Open()


                The two arguments passed to CreateRunspacePool(), 1 and 8 is the minimum and maximum number of runspaces allowed to execute at any given time, giving us an effective maximum degree of parallelism of 8.



                2. Create an instance of PowerShell, attach some executable code to it and assign it to our RunspacePool:



                An instance of PowerShell is not the same as the powershell.exe process (which is really a Host application), but an internal runtime object representing the PowerShell code to execute. We can use the [powershell] type accelerator to create a new PowerShell instance within PowerShell:



                $Code = {
                param($Credentials,$ComputerName)
                $session = New-PSSession -ComputerName $ComputerName -Credential $Credentials
                Invoke-Command -Session $session -ScriptBlock {w32tm /resync /nowait /rediscover}
                }
                $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($creds).AddArgument("computer1.domain.tld")
                $PSinstance.RunspacePool = $RunspacePool


                3. Invoke the PowerShell instance asynchronously using APM:



                Using what is known in .NET development terminology as the Asynchronous Programming Model, we can split the invocation of a command into a Begin method, for giving a "green light" to execute the code, and an End method to collect the results. Since we in this case are not really interested in any feedback (we don't wait for the output from w32tm anyways), we can make due by simply calling the first method



                $PSinstance.BeginInvoke()




                Wrapping it up in a RunspacePool



                Using the above technique, we can wrap the sequential iterations of creating new connections and invoking the remote command in a parallel execution flow:



                $ComputerNames = Get-ADComputer -filter * -Properties dnsHostName |select -Expand dnsHostName

                $Code = {
                param($Credentials,$ComputerName)
                $session = New-PSSession -ComputerName $ComputerName -Credential $Credentials
                Invoke-Command -Session $session -ScriptBlock {w32tm /resync /nowait /rediscover}
                }

                $creds = Get-Credential domainuser

                $rsPool = [runspacefactory]::CreateRunspacePool(1,8)
                $rsPool.Open()

                foreach($ComputerName in $ComputerNames)
                {
                $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($creds).AddArgument($ComputerName)
                $PSinstance.RunspacePool = $rsPool
                $PSinstance.BeginInvoke()
                }


                Assuming that the CPU has the capacity to execute all 8 runspaces at once, we should be able to see that the execution time is greatly reduced, but at the cost of readability of the script due to the rather "advanced" methods used.





                Determining the optimum degree of parallism:



                We could easily create a RunspacePool that allows for the execution of a 100 runspaces at the same time:



                [runspacefactory]::CreateRunspacePool(1,100)


                But at the end of the day, it all comes down to how many units of execution our local CPU can handle. In other words, as long as your code is executing, it does not make sense to allow more runspaces than you have logical processors to dispatch execution of code to.



                Thanks to WMI, this threshold is fairly easy to determine:



                $NumberOfLogicalProcessor = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors
                [runspacefactory]::CreateRunspacePool(1,$NumberOfLogicalProcessors)


                If, on the other hand, the code you are executing itself incurs a lot of wait time due to external factors like network latency, you can still benefit from running more simultanous runspaces than you have logical processors, so you'd probably want to test of range possible maximum runspaces to find break-even:



                foreach($n in ($NumberOfLogicalProcessors..($NumberOfLogicalProcessors*3)))
                {
                Write-Host "$n: " -NoNewLine
                (Measure-Command {
                $Computers = Get-ADComputer -filter * -Properties dnsHostName |select -Expand dnsHostName -First 100
                ...
                [runspacefactory]::CreateRunspacePool(1,$n)
                ...
                }).TotalSeconds
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 8 '18 at 13:29









                mklement

                21516




                21516










                answered Sep 6 '14 at 13:56









                Mathias R. JessenMathias R. Jessen

                22.7k35189




                22.7k35189








                • 4





                  If the jobs are waiting on the network, e.g. you are running PowerShell commands on remote computers, you could easily go far over the number of logical processors before you hit any CPU bottleneck.

                  – Michael Hampton
                  Sep 6 '14 at 14:00













                • Well, that is true. Changed it a bit and provided an example for testing

                  – Mathias R. Jessen
                  Sep 6 '14 at 14:09











                • How to make sure all the job is done at the end? (May need to something after all the script blocks finished)

                  – sjzls
                  Nov 18 '14 at 18:04











                • @NickW Great question. I'll do a follow-up on tracking the jobs and "harvesting" potential output later today, stay tuned

                  – Mathias R. Jessen
                  Nov 19 '14 at 12:23






                • 1





                  @MathiasR.Jessen Very well-written answer! Looking forward to the update.

                  – Signal15
                  Dec 3 '14 at 16:55














                • 4





                  If the jobs are waiting on the network, e.g. you are running PowerShell commands on remote computers, you could easily go far over the number of logical processors before you hit any CPU bottleneck.

                  – Michael Hampton
                  Sep 6 '14 at 14:00













                • Well, that is true. Changed it a bit and provided an example for testing

                  – Mathias R. Jessen
                  Sep 6 '14 at 14:09











                • How to make sure all the job is done at the end? (May need to something after all the script blocks finished)

                  – sjzls
                  Nov 18 '14 at 18:04











                • @NickW Great question. I'll do a follow-up on tracking the jobs and "harvesting" potential output later today, stay tuned

                  – Mathias R. Jessen
                  Nov 19 '14 at 12:23






                • 1





                  @MathiasR.Jessen Very well-written answer! Looking forward to the update.

                  – Signal15
                  Dec 3 '14 at 16:55








                4




                4





                If the jobs are waiting on the network, e.g. you are running PowerShell commands on remote computers, you could easily go far over the number of logical processors before you hit any CPU bottleneck.

                – Michael Hampton
                Sep 6 '14 at 14:00







                If the jobs are waiting on the network, e.g. you are running PowerShell commands on remote computers, you could easily go far over the number of logical processors before you hit any CPU bottleneck.

                – Michael Hampton
                Sep 6 '14 at 14:00















                Well, that is true. Changed it a bit and provided an example for testing

                – Mathias R. Jessen
                Sep 6 '14 at 14:09





                Well, that is true. Changed it a bit and provided an example for testing

                – Mathias R. Jessen
                Sep 6 '14 at 14:09













                How to make sure all the job is done at the end? (May need to something after all the script blocks finished)

                – sjzls
                Nov 18 '14 at 18:04





                How to make sure all the job is done at the end? (May need to something after all the script blocks finished)

                – sjzls
                Nov 18 '14 at 18:04













                @NickW Great question. I'll do a follow-up on tracking the jobs and "harvesting" potential output later today, stay tuned

                – Mathias R. Jessen
                Nov 19 '14 at 12:23





                @NickW Great question. I'll do a follow-up on tracking the jobs and "harvesting" potential output later today, stay tuned

                – Mathias R. Jessen
                Nov 19 '14 at 12:23




                1




                1





                @MathiasR.Jessen Very well-written answer! Looking forward to the update.

                – Signal15
                Dec 3 '14 at 16:55





                @MathiasR.Jessen Very well-written answer! Looking forward to the update.

                – Signal15
                Dec 3 '14 at 16:55













                5














                Adding to this discussion, what's missing is a collector to store the data that is created from the runspace, and a variable to check the status of the runspace, i.e. is it completed or not.



                #Add an collector object that will store the data
                $Object = New-Object 'System.Management.Automation.PSDataCollection[psobject]'

                #Create a variable to check the status
                $Handle = $PSinstance.BeginInvoke($Object,$Object)

                #So if you want to check the status simply type:
                $Handle

                #If you want to see the data collected, type:
                $Object





                share|improve this answer






























                  5














                  Adding to this discussion, what's missing is a collector to store the data that is created from the runspace, and a variable to check the status of the runspace, i.e. is it completed or not.



                  #Add an collector object that will store the data
                  $Object = New-Object 'System.Management.Automation.PSDataCollection[psobject]'

                  #Create a variable to check the status
                  $Handle = $PSinstance.BeginInvoke($Object,$Object)

                  #So if you want to check the status simply type:
                  $Handle

                  #If you want to see the data collected, type:
                  $Object





                  share|improve this answer




























                    5












                    5








                    5







                    Adding to this discussion, what's missing is a collector to store the data that is created from the runspace, and a variable to check the status of the runspace, i.e. is it completed or not.



                    #Add an collector object that will store the data
                    $Object = New-Object 'System.Management.Automation.PSDataCollection[psobject]'

                    #Create a variable to check the status
                    $Handle = $PSinstance.BeginInvoke($Object,$Object)

                    #So if you want to check the status simply type:
                    $Handle

                    #If you want to see the data collected, type:
                    $Object





                    share|improve this answer















                    Adding to this discussion, what's missing is a collector to store the data that is created from the runspace, and a variable to check the status of the runspace, i.e. is it completed or not.



                    #Add an collector object that will store the data
                    $Object = New-Object 'System.Management.Automation.PSDataCollection[psobject]'

                    #Create a variable to check the status
                    $Handle = $PSinstance.BeginInvoke($Object,$Object)

                    #So if you want to check the status simply type:
                    $Handle

                    #If you want to see the data collected, type:
                    $Object






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 9 '17 at 16:25









                    Mathias R. Jessen

                    22.7k35189




                    22.7k35189










                    answered Feb 9 '17 at 16:22









                    Nate StoneNate Stone

                    4912




                    4912























                        3














                        Check out PoshRSJob. It provides same/similar functions as the native *-Job functions, but uses Runspaces which tend to be much quicker and more responsive than the standard Powershell jobs.






                        share|improve this answer




























                          3














                          Check out PoshRSJob. It provides same/similar functions as the native *-Job functions, but uses Runspaces which tend to be much quicker and more responsive than the standard Powershell jobs.






                          share|improve this answer


























                            3












                            3








                            3







                            Check out PoshRSJob. It provides same/similar functions as the native *-Job functions, but uses Runspaces which tend to be much quicker and more responsive than the standard Powershell jobs.






                            share|improve this answer













                            Check out PoshRSJob. It provides same/similar functions as the native *-Job functions, but uses Runspaces which tend to be much quicker and more responsive than the standard Powershell jobs.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 6 '17 at 23:21









                            RoscoRosco

                            311




                            311























                                0














                                @mathias-r-jessen has a great answer though there are details I'd like to add.



                                Max Threads



                                In theory threads should be limited by the number of system processors. However, while testing AsyncTcpScan I achieved far better performance by choosing a much larger value for MaxThreads. Thus why that module has a -MaxThreads input parameter. Keep in mind that allocating too many threads will hinder performance.



                                Returning Data



                                Getting data back from the ScriptBlock is tricky. I've updated the OP code and integrated it into what was used for AsyncTcpScan.




                                WARNING: I wasn't able to test the following code. I made some changes
                                to the OP script based on my experience working with the Active
                                Directory cmdlets.




                                # Script to run in each thread.
                                [System.Management.Automation.ScriptBlock]$ScriptBlock = {

                                $result = New-Object PSObject -Property @{ 'Computer' = $args[0];
                                'Success' = $false; }

                                try {
                                $session = New-PSSession -ComputerName $args[0] -Credential $args[1]
                                Invoke-Command -Session $session -ScriptBlock { w32tm /resync /nowait /rediscover }
                                Disconnect-PSSession -Session $session
                                $result.Success = $true
                                } catch {

                                }

                                return $result

                                } # End Scriptblock

                                function Invoke-AsyncJob
                                {
                                [CmdletBinding()]
                                param(
                                [parameter(Mandatory=$true)]
                                [System.Management.Automation.PSCredential]
                                # Credential object to login to remote systems
                                $Credentials
                                )

                                Import-Module ActiveDirectory

                                $Results = @()

                                $AllJobs = New-Object System.Collections.ArrayList

                                $AllDomainComputers = Get-ADComputer -Filter * -Property dnsHostName

                                $HostRunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(2,10,$Host)

                                $HostRunspacePool.Open()

                                foreach($DomainComputer in $AllDomainComputers)
                                {
                                $asyncJob = [System.Management.Automation.PowerShell]::Create().AddScript($ScriptBlock).AddParameters($($($DomainComputer.dnsName),$Credentials))

                                $asyncJob.RunspacePool = $HostRunspacePool

                                $asyncJobObj = @{ JobHandle = $asyncJob;
                                AsyncHandle = $asyncJob.BeginInvoke() }

                                $AllJobs.Add($asyncJobObj) | Out-Null
                                }

                                $ProcessingJobs = $true

                                Do {

                                $CompletedJobs = $AllJobs | Where-Object { $_.AsyncHandle.IsCompleted }

                                if($null -ne $CompletedJobs)
                                {
                                foreach($job in $CompletedJobs)
                                {
                                $result = $job.JobHandle.EndInvoke($job.AsyncHandle)

                                if($null -ne $result)
                                {
                                $Results += $result
                                }

                                $job.JobHandle.Dispose()

                                $AllJobs.Remove($job)
                                }

                                } else {

                                if($AllJobs.Count -eq 0)
                                {
                                $ProcessingJobs = $false

                                } else {

                                Start-Sleep -Milliseconds 500
                                }
                                }

                                } While ($ProcessingJobs)

                                $HostRunspacePool.Close()
                                $HostRunspacePool.Dispose()

                                return $Results

                                } # End function Invoke-AsyncJob




                                share








                                New contributor




                                phbits is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.

























                                  0














                                  @mathias-r-jessen has a great answer though there are details I'd like to add.



                                  Max Threads



                                  In theory threads should be limited by the number of system processors. However, while testing AsyncTcpScan I achieved far better performance by choosing a much larger value for MaxThreads. Thus why that module has a -MaxThreads input parameter. Keep in mind that allocating too many threads will hinder performance.



                                  Returning Data



                                  Getting data back from the ScriptBlock is tricky. I've updated the OP code and integrated it into what was used for AsyncTcpScan.




                                  WARNING: I wasn't able to test the following code. I made some changes
                                  to the OP script based on my experience working with the Active
                                  Directory cmdlets.




                                  # Script to run in each thread.
                                  [System.Management.Automation.ScriptBlock]$ScriptBlock = {

                                  $result = New-Object PSObject -Property @{ 'Computer' = $args[0];
                                  'Success' = $false; }

                                  try {
                                  $session = New-PSSession -ComputerName $args[0] -Credential $args[1]
                                  Invoke-Command -Session $session -ScriptBlock { w32tm /resync /nowait /rediscover }
                                  Disconnect-PSSession -Session $session
                                  $result.Success = $true
                                  } catch {

                                  }

                                  return $result

                                  } # End Scriptblock

                                  function Invoke-AsyncJob
                                  {
                                  [CmdletBinding()]
                                  param(
                                  [parameter(Mandatory=$true)]
                                  [System.Management.Automation.PSCredential]
                                  # Credential object to login to remote systems
                                  $Credentials
                                  )

                                  Import-Module ActiveDirectory

                                  $Results = @()

                                  $AllJobs = New-Object System.Collections.ArrayList

                                  $AllDomainComputers = Get-ADComputer -Filter * -Property dnsHostName

                                  $HostRunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(2,10,$Host)

                                  $HostRunspacePool.Open()

                                  foreach($DomainComputer in $AllDomainComputers)
                                  {
                                  $asyncJob = [System.Management.Automation.PowerShell]::Create().AddScript($ScriptBlock).AddParameters($($($DomainComputer.dnsName),$Credentials))

                                  $asyncJob.RunspacePool = $HostRunspacePool

                                  $asyncJobObj = @{ JobHandle = $asyncJob;
                                  AsyncHandle = $asyncJob.BeginInvoke() }

                                  $AllJobs.Add($asyncJobObj) | Out-Null
                                  }

                                  $ProcessingJobs = $true

                                  Do {

                                  $CompletedJobs = $AllJobs | Where-Object { $_.AsyncHandle.IsCompleted }

                                  if($null -ne $CompletedJobs)
                                  {
                                  foreach($job in $CompletedJobs)
                                  {
                                  $result = $job.JobHandle.EndInvoke($job.AsyncHandle)

                                  if($null -ne $result)
                                  {
                                  $Results += $result
                                  }

                                  $job.JobHandle.Dispose()

                                  $AllJobs.Remove($job)
                                  }

                                  } else {

                                  if($AllJobs.Count -eq 0)
                                  {
                                  $ProcessingJobs = $false

                                  } else {

                                  Start-Sleep -Milliseconds 500
                                  }
                                  }

                                  } While ($ProcessingJobs)

                                  $HostRunspacePool.Close()
                                  $HostRunspacePool.Dispose()

                                  return $Results

                                  } # End function Invoke-AsyncJob




                                  share








                                  New contributor




                                  phbits is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.























                                    0












                                    0








                                    0







                                    @mathias-r-jessen has a great answer though there are details I'd like to add.



                                    Max Threads



                                    In theory threads should be limited by the number of system processors. However, while testing AsyncTcpScan I achieved far better performance by choosing a much larger value for MaxThreads. Thus why that module has a -MaxThreads input parameter. Keep in mind that allocating too many threads will hinder performance.



                                    Returning Data



                                    Getting data back from the ScriptBlock is tricky. I've updated the OP code and integrated it into what was used for AsyncTcpScan.




                                    WARNING: I wasn't able to test the following code. I made some changes
                                    to the OP script based on my experience working with the Active
                                    Directory cmdlets.




                                    # Script to run in each thread.
                                    [System.Management.Automation.ScriptBlock]$ScriptBlock = {

                                    $result = New-Object PSObject -Property @{ 'Computer' = $args[0];
                                    'Success' = $false; }

                                    try {
                                    $session = New-PSSession -ComputerName $args[0] -Credential $args[1]
                                    Invoke-Command -Session $session -ScriptBlock { w32tm /resync /nowait /rediscover }
                                    Disconnect-PSSession -Session $session
                                    $result.Success = $true
                                    } catch {

                                    }

                                    return $result

                                    } # End Scriptblock

                                    function Invoke-AsyncJob
                                    {
                                    [CmdletBinding()]
                                    param(
                                    [parameter(Mandatory=$true)]
                                    [System.Management.Automation.PSCredential]
                                    # Credential object to login to remote systems
                                    $Credentials
                                    )

                                    Import-Module ActiveDirectory

                                    $Results = @()

                                    $AllJobs = New-Object System.Collections.ArrayList

                                    $AllDomainComputers = Get-ADComputer -Filter * -Property dnsHostName

                                    $HostRunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(2,10,$Host)

                                    $HostRunspacePool.Open()

                                    foreach($DomainComputer in $AllDomainComputers)
                                    {
                                    $asyncJob = [System.Management.Automation.PowerShell]::Create().AddScript($ScriptBlock).AddParameters($($($DomainComputer.dnsName),$Credentials))

                                    $asyncJob.RunspacePool = $HostRunspacePool

                                    $asyncJobObj = @{ JobHandle = $asyncJob;
                                    AsyncHandle = $asyncJob.BeginInvoke() }

                                    $AllJobs.Add($asyncJobObj) | Out-Null
                                    }

                                    $ProcessingJobs = $true

                                    Do {

                                    $CompletedJobs = $AllJobs | Where-Object { $_.AsyncHandle.IsCompleted }

                                    if($null -ne $CompletedJobs)
                                    {
                                    foreach($job in $CompletedJobs)
                                    {
                                    $result = $job.JobHandle.EndInvoke($job.AsyncHandle)

                                    if($null -ne $result)
                                    {
                                    $Results += $result
                                    }

                                    $job.JobHandle.Dispose()

                                    $AllJobs.Remove($job)
                                    }

                                    } else {

                                    if($AllJobs.Count -eq 0)
                                    {
                                    $ProcessingJobs = $false

                                    } else {

                                    Start-Sleep -Milliseconds 500
                                    }
                                    }

                                    } While ($ProcessingJobs)

                                    $HostRunspacePool.Close()
                                    $HostRunspacePool.Dispose()

                                    return $Results

                                    } # End function Invoke-AsyncJob




                                    share








                                    New contributor




                                    phbits is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.










                                    @mathias-r-jessen has a great answer though there are details I'd like to add.



                                    Max Threads



                                    In theory threads should be limited by the number of system processors. However, while testing AsyncTcpScan I achieved far better performance by choosing a much larger value for MaxThreads. Thus why that module has a -MaxThreads input parameter. Keep in mind that allocating too many threads will hinder performance.



                                    Returning Data



                                    Getting data back from the ScriptBlock is tricky. I've updated the OP code and integrated it into what was used for AsyncTcpScan.




                                    WARNING: I wasn't able to test the following code. I made some changes
                                    to the OP script based on my experience working with the Active
                                    Directory cmdlets.




                                    # Script to run in each thread.
                                    [System.Management.Automation.ScriptBlock]$ScriptBlock = {

                                    $result = New-Object PSObject -Property @{ 'Computer' = $args[0];
                                    'Success' = $false; }

                                    try {
                                    $session = New-PSSession -ComputerName $args[0] -Credential $args[1]
                                    Invoke-Command -Session $session -ScriptBlock { w32tm /resync /nowait /rediscover }
                                    Disconnect-PSSession -Session $session
                                    $result.Success = $true
                                    } catch {

                                    }

                                    return $result

                                    } # End Scriptblock

                                    function Invoke-AsyncJob
                                    {
                                    [CmdletBinding()]
                                    param(
                                    [parameter(Mandatory=$true)]
                                    [System.Management.Automation.PSCredential]
                                    # Credential object to login to remote systems
                                    $Credentials
                                    )

                                    Import-Module ActiveDirectory

                                    $Results = @()

                                    $AllJobs = New-Object System.Collections.ArrayList

                                    $AllDomainComputers = Get-ADComputer -Filter * -Property dnsHostName

                                    $HostRunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(2,10,$Host)

                                    $HostRunspacePool.Open()

                                    foreach($DomainComputer in $AllDomainComputers)
                                    {
                                    $asyncJob = [System.Management.Automation.PowerShell]::Create().AddScript($ScriptBlock).AddParameters($($($DomainComputer.dnsName),$Credentials))

                                    $asyncJob.RunspacePool = $HostRunspacePool

                                    $asyncJobObj = @{ JobHandle = $asyncJob;
                                    AsyncHandle = $asyncJob.BeginInvoke() }

                                    $AllJobs.Add($asyncJobObj) | Out-Null
                                    }

                                    $ProcessingJobs = $true

                                    Do {

                                    $CompletedJobs = $AllJobs | Where-Object { $_.AsyncHandle.IsCompleted }

                                    if($null -ne $CompletedJobs)
                                    {
                                    foreach($job in $CompletedJobs)
                                    {
                                    $result = $job.JobHandle.EndInvoke($job.AsyncHandle)

                                    if($null -ne $result)
                                    {
                                    $Results += $result
                                    }

                                    $job.JobHandle.Dispose()

                                    $AllJobs.Remove($job)
                                    }

                                    } else {

                                    if($AllJobs.Count -eq 0)
                                    {
                                    $ProcessingJobs = $false

                                    } else {

                                    Start-Sleep -Milliseconds 500
                                    }
                                    }

                                    } While ($ProcessingJobs)

                                    $HostRunspacePool.Close()
                                    $HostRunspacePool.Dispose()

                                    return $Results

                                    } # End function Invoke-AsyncJob





                                    share








                                    New contributor




                                    phbits is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.








                                    share


                                    share






                                    New contributor




                                    phbits is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    answered 1 min ago









                                    phbitsphbits

                                    111




                                    111




                                    New contributor




                                    phbits is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.





                                    New contributor





                                    phbits is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.






                                    phbits is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Server Fault!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f626711%2fhow-do-i-run-my-powershell-scripts-in-parallel-without-using-jobs%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        117736 Шеррод Примітки | Див. також | Посилання | Навігаційне...

                                        As a Security Precaution, the user account has been locked The Next CEO of Stack OverflowMS...

                                        Маріан Котлеба Зміст Життєпис | Політичні погляди |...