< Back

Invoke-MadMultithread

Sat Jan 18, 2020 9:53 am

NAME Invoke-MadMultithread



SYNOPSIS

Run input against function in multiple threads





SYNTAX

Invoke-MadMultithread [[-Function] <String>] [[-Parameters] <Hashtable>] [[-Threads] <Int32>] [-NoSort]

[[-InputObject] <Array>] [<CommonParameters>]





DESCRIPTION





PARAMETERS

-Function <String>

String - Name of the defined function to run



Required? false

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Parameters <Hashtable>

Hashtable - Parameters (other than pipeline input) for Function



Required? false

Position? 2

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Threads <Int32>

Int - Number of parallel threads to use

Defaults to 2



Required? false

Position? 3

Default value 2

Accept pipeline input? false

Accept wildcard characters? false



-NoSort [<SwitchParameter>]

Switch - If present, results are returned in the order they complete processing.

If not present, results are return in the same order as their

respective inputs.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-InputObject <Array>

Object[] - Array of objects or pipeline input to be processed by Scriptblock



Required? false

Position? 4

Default value

Accept pipeline input? true (ByValue)

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





v1.0 3/16/18 Tim Curwick Created

v1.1 8/ 2/18 Tim Curwick Modifed to supress output of Null values



-------------------------- EXAMPLE 1 --------------------------



PS C:\\># Single thread



$Groups | Get-ADGroupMember

# Default 2 threads

$Groups | Invoke-MadMultithread Get-ADGroupMember









-------------------------- EXAMPLE 2 --------------------------



PS C:\\># Single thread



$Groups | Get-ADGroupMember -Server DC01

# 4 threads

$Groups | Invoke-MadMultithread Get-ADGroupMember -Parameters @{ Server = 'DC01' } -Threads 4









-------------------------- EXAMPLE 3 --------------------------



PS C:\\># Single thread



$Groups | Get-ADGroupMember -Server DC01 -Recursive

# 8 threads

$Groups | Invoke-MadMultithread Get-ADGroupMember -Parameters @{ Server = 'DC01'; Recursive = $True } -Threads 8









-------------------------- EXAMPLE 4 --------------------------



PS C:\\>function YourFunction {



Param ( [parameter( ValueFromPipeline = $True )]$InputParam, $Thing1, $Thing2 )

Process

{

# Your pipeline code

}

}

# Single thread

$InputObjects | YourFunction -Thing1 'Alpha' -Thing2 27

# Default 2 threads

$InputObjects | Invoke-MadMultithread YourFunction -Parameters @{ Thing1 = 'Alpha'; Thing2 = 27 }









-------------------------- EXAMPLE 5 --------------------------



PS C:\\>function Start-RandomSleep {



Param ( [parameter( ValueFromPipeline = $True )]$Thing1 )

Process

{

Start-Sleep -Seconds ( Get-Random 5 )

$Thing1

}

}

# Default sorting - Output is in same order as input

1..10 | Invoke-MadMultithread Start-RandomSleep -Threads 10



# returns 1, 2, 3, 4, 5, 6, 7, 8, 9, 10





# NoSort option - Results returned when ready

1..10 | Invoke-MadMultithread Start-RandomSleep -Threads 10 -NoSort



# returns 7, 4, 5, 1, 6, 8, 10, 2, 3, 9











RELATED LINKS