Forum Discussion

Alan2022's avatar
Alan2022
Iron Contributor
Nov 07, 2022

Powershell Multithreading using THREADJOBS.

Dear Community,

 

Just for sharing 🙂

 

 

# Install-Module -Name ThreadJob -Scope CurrentUser

Cls
$startScript = get-date

# Parameters
$ThreadsProcessed = 0
$customObject = $null

$someJobs = { 
    param($ThreadsProcessed)

    # Create Start and End Date Time
    $start = get-date
    Sleep -s 1
    $end = get-date

    # Create a Custom Object
    $customObject = [PSCustomObject]@{
        ThreadID = $ThreadsProcessed
        Start = $start
        End = $end
        Result = "OK"
    }

    Return $customObject
}

1..10 | % { 
    # Start some jobs
    Start-ThreadJob -ScriptBlock $someJobs -ArgumentList $ThreadsProcessed
    $ThreadsProcessed++
}

# Get Jobs and Remove Jobs
Get-Job | Wait-Job | ForEach {
    Receive-Job -Name $_.Name 
    Remove-Job -Name $_.Name
}

$endScript = get-date
Write-Host "$startScript to $endScript"

 

No RepliesBe the first to reply

Resources