Rej Rej - 1 year ago 128
PowerShell Question

Powershell adding multithreading to finished script

I got a scirpt, that is working fine, but I want to speed up it a little, by using multithreading. The problem is, that it's kinda difficult to do it, without changing a lot of code. I would love to avoid jobs with new separated scopes, but it looks like it's impossible. Also starting-job is really slow, it needs ~~150miliseconds to start execution. Is there any way to avoid jobs, or atleast make this script block work, as expected? (no doubled functions, passing arguments correctly)

[int]$arg1 = 2, [int]$arg2 = 3
$functionDoSomething = {
function doSomething($inp) {
return $inp + 10

function doSomething($inp) {
return $inp + 10

# time: 1523337 ticks
Measure-Command {
Start-Job -Name Job1 -InitializationScript $functionDoSomething -ScriptBlock {
return doSomething ($arg1 + $arg2)
} -ArgumentList $arg1, $arg2
Wait-Job Job1
$var2 = Receive-Job Job1 # result is 10, so arguements aren't passed correctly, trick with @($arg1, arg2) = the same result

# time: 6867 ticks
Measure-Command {
$var3 = doSomething ($arg1 + $arg2) # result is 15

Answer Source
# Have no fear - Invoke-Parallel is here. Hoping that I can help steer you in the right direction. Let me show you some pseudo code that can point you in the right direction.

# You need a list of objects in which you want to perform an operation for
$listOfComputers = @(

# Pipe your array to Invoke-Parallel which takes care of opening threads depending on the amount of objects piped in. 
# If you want to control the amount of simultaneous threads simply help Invoke-Parallel -ShowWindow to see how
$results = $listOfComputers | Invoke-Parallel -ScriptBlock {
function doSomething($inp) 
    return [int]$inp + 10
if ($_ -eq '1') { Start-Sleep -Seconds 15; Write-Verbose 'Waited 15s' } # Adding this so you can see that this script block is running in a multi-threaded fashion
# $_ refers to the values inside of $listOfComputers
return doSomething ($_)
} -Verbose


<# Notice how 11 comes up last 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download