Update Invoke-PSThread.ps1

This commit is contained in:
freedbygrace
2023-10-20 14:03:20 -04:00
committed by GitHub
parent d237b27b9a
commit dfaf87e29a
+81 -41
View File
@@ -1,4 +1,4 @@
## Microsoft Function Naming Convention: http://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx ## Microsoft Function Naming Convention: http://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx
#region Function Invoke-PSThread #region Function Invoke-PSThread
Function Invoke-PSThread Function Invoke-PSThread
@@ -11,52 +11,61 @@ Function Invoke-PSThread
This function aims to remove the enormous complexity out of multithreading within powershell. See the fully working examples below. This function aims to remove the enormous complexity out of multithreading within powershell. See the fully working examples below.
.PARAMETER Runspace .PARAMETER Runspace
Your parameter description Strictly places this function into "Runspace" mode.
.PARAMETER RunspacePool .PARAMETER RunspacePool
Your parameter description Strictly places this function into "RunspacePool" mode.
.PARAMETER Await .PARAMETER Await
Your parameter description Strictly places this function into "Await" mode.
.PARAMETER RunspaceDefinition .PARAMETER RunspaceDefinition
Your parameter description A valid scriptblock that will be executed by either a single thread within the runspace, or by each thread within the runspace pool.
.PARAMETER RunspaceParameters .PARAMETER RunspaceParameters
Your parameter description A valid hashtable containing the key-value pairs for the additional parameters to be passed to each thread. If the order of the parameters needs to be preserved, use an ordered hashtable instead.
.PARAMETER InputObjectList .PARAMETER InputObjectList
Your parameter description A list of one or more objects to submit to the runspace pool. The runspace pool will create a job for each item and process them in a queue fashion (As new runspaces become available) until the list has been completed.
.PARAMETER SynchronizedHashtable .PARAMETER SynchronizedHashtable
Your parameter description A valid variable object that contains the synchronized hashtable. This is primarily used to share data between the main thread, and any additional threads that are created.
.PARAMETER AssemblyList .PARAMETER AssemblyList
Your parameter description A list of one or more additional assemblies that will be loaded into the inital session state, and made available to each thread.
.PARAMETER FunctionList .PARAMETER FunctionList
Your parameter description A list of one or more additional functions that will be loaded into the inital session state, and made available to each thread.
.PARAMETER ModuleList .PARAMETER ModuleList
Your parameter description A list of one or more additional modules that will be loaded into the inital session state, and made available to each thread.
.PARAMETER VariableList .PARAMETER VariableList
Your parameter description A list of one or more additional variables that will be loaded into the inital session state, and made available to each thread.
.PARAMETER ThreadList .PARAMETER ApartmentState
Your parameter description A valid apartment state that will be used to create each thread within the runspace.
.PARAMETER LoopTimeout .PARAMETER ThreadOption
Your parameter description A valid thread option that will be used to create each thread within the runspace.
.PARAMETER LoopDuration .PARAMETER MaximumRunspaces
Your parameter description The maximum runspaces available to the runspace pool. By default, the value will be set to double the amount of logical processors available to the current device.
.PARAMETER WaitForAvailableRunspace .PARAMETER WaitForAvailableRunspace
Your parameter description If there are no more available runspaces, the function will wait to submit any additional jobs to teh runspace pool as runspaces become available. This will drastically increase the time it takes to process the runspace pool.
.PARAMETER ThreadList
A list of one ore more objects that were returned in the output from a previous call of this function. The runspaces in this list will be monitored until they have completed.
.PARAMETER LoopTimeout
A valid timespan that will be used for designating the maximum time allowed to monitor running threads.
.PARAMETER LoopDuration
A valid timespan that will be used for designating how often to recheck the status of running threads.
.PARAMETER ContinueOnError .PARAMETER ContinueOnError
Your parameter description Ignore errors.
.EXAMPLE .EXAMPLE
Create a runspace Create a runspace
@@ -93,7 +102,7 @@ Function Invoke-PSThread
$InvokePSThreadParameters = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary' $InvokePSThreadParameters = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
$InvokePSThreadParameters.Runspace = $True $InvokePSThreadParameters.Runspace = $True
$InvokePSThreadParameters.RunspaceDefinition = $RunspaceDefinition $InvokePSThreadParameters.RunspaceDefinition = $RunspaceDefinition
$InvokePSThreadParameters.RunspaceParameters = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary' $InvokePSThreadParameters.RunspaceParameters = [Ordered]@{}
$InvokePSThreadParameters.RunspaceParameters.Title = "This is my UI title" $InvokePSThreadParameters.RunspaceParameters.Title = "This is my UI title"
$InvokePSThreadParameters.RunspaceParameters.Message = "This is my UI message.`r`nThis is the second line of my UI message.`r`nThis is the third line of my UI message." $InvokePSThreadParameters.RunspaceParameters.Message = "This is my UI message.`r`nThis is the second line of my UI message.`r`nThis is the third line of my UI message."
$InvokePSThreadParameters.SynchronizedHashtable = Get-Variable -Name 'SynchronizedHashtable' -ErrorAction SilentlyContinue $InvokePSThreadParameters.SynchronizedHashtable = Get-Variable -Name 'SynchronizedHashtable' -ErrorAction SilentlyContinue
@@ -148,7 +157,7 @@ Function Invoke-PSThread
$InvokePSThreadParameters.RunspacePool = $True $InvokePSThreadParameters.RunspacePool = $True
$InvokePSThreadParameters.InputObjectList = 1..15 $InvokePSThreadParameters.InputObjectList = 1..15
$InvokePSThreadParameters.RunspaceDefinition = $RunspaceDefinition $InvokePSThreadParameters.RunspaceDefinition = $RunspaceDefinition
$InvokePSThreadParameters.RunspaceParameters = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary' $InvokePSThreadParameters.RunspaceParameters = [Ordered]@{}
$InvokePSThreadParameters.RunspaceParameters.AdditionalParameter1 = Get-Random -Minimum 0 -Maximum 10000 $InvokePSThreadParameters.RunspaceParameters.AdditionalParameter1 = Get-Random -Minimum 0 -Maximum 10000
$InvokePSThreadParameters.RunspaceParameters.AdditionalParameter2 = Get-Random -Minimum 0 -Maximum 10000 $InvokePSThreadParameters.RunspaceParameters.AdditionalParameter2 = Get-Random -Minimum 0 -Maximum 10000
$InvokePSThreadParameters.SynchronizedHashtable = Get-Variable -Name 'SynchronizedHashtable' -ErrorAction SilentlyContinue $InvokePSThreadParameters.SynchronizedHashtable = Get-Variable -Name 'SynchronizedHashtable' -ErrorAction SilentlyContinue
@@ -203,7 +212,7 @@ Function Invoke-PSThread
$InvokePSThreadParameters.RunspacePool = $True $InvokePSThreadParameters.RunspacePool = $True
$InvokePSThreadParameters.InputObjectList = Get-Process | Get-Random -Count 15 $InvokePSThreadParameters.InputObjectList = Get-Process | Get-Random -Count 15
$InvokePSThreadParameters.RunspaceDefinition = $RunspaceDefinition $InvokePSThreadParameters.RunspaceDefinition = $RunspaceDefinition
$InvokePSThreadParameters.RunspaceParameters = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary' $InvokePSThreadParameters.RunspaceParameters = [Ordered]@{}
$InvokePSThreadParameters.RunspaceParameters.AdditionalParameter1 = Get-Random -Minimum 0 -Maximum 10000 $InvokePSThreadParameters.RunspaceParameters.AdditionalParameter1 = Get-Random -Minimum 0 -Maximum 10000
$InvokePSThreadParameters.RunspaceParameters.AdditionalParameter2 = Get-Random -Minimum 0 -Maximum 10000 $InvokePSThreadParameters.RunspaceParameters.AdditionalParameter2 = Get-Random -Minimum 0 -Maximum 10000
$InvokePSThreadParameters.SynchronizedHashtable = Get-Variable -Name 'SynchronizedHashtable' -ErrorAction SilentlyContinue $InvokePSThreadParameters.SynchronizedHashtable = Get-Variable -Name 'SynchronizedHashtable' -ErrorAction SilentlyContinue
@@ -277,7 +286,7 @@ Function Invoke-PSThread
[Parameter(Mandatory=$False, ParameterSetName = 'RunspacePool')] [Parameter(Mandatory=$False, ParameterSetName = 'RunspacePool')]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[Alias('RSP')] [Alias('RSP')]
[System.Collections.Specialized.OrderedDictionary]$RunspaceParameters, [System.Collections.HashTable]$RunspaceParameters,
[Parameter(Mandatory=$True, ParameterSetName = 'RunspacePool')] [Parameter(Mandatory=$True, ParameterSetName = 'RunspacePool')]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
@@ -310,6 +319,25 @@ Function Invoke-PSThread
[Alias('VL')] [Alias('VL')]
[System.Management.Automation.PSVariable[]]$VariableList, [System.Management.Automation.PSVariable[]]$VariableList,
[Parameter(Mandatory=$False, ParameterSetName = 'Runspace')]
[Parameter(Mandatory=$False, ParameterSetName = 'RunspacePool')]
[Alias('AS')]
[System.Threading.ApartmentState]$ApartmentState,
[Parameter(Mandatory=$False, ParameterSetName = 'Runspace')]
[Parameter(Mandatory=$False, ParameterSetName = 'RunspacePool')]
[Alias('TO')]
[System.Management.Automation.Runspaces.PSThreadOptions]$ThreadOption,
[Parameter(Mandatory=$False, ParameterSetName = 'RunspacePool')]
[ValidateScript({$_ -le (((Get-CIMInstance -Namespace 'Root\CIMv2' -ClassName 'Win32_Processor' -Property 'NumberOfLogicalProcessors' -Verbose:$False).NumberOfLogicalProcessors | Measure-Object -Sum).Sum * 2)})]
[Alias('MR')]
[UInt32]$MaximumRunspaces,
[Parameter(Mandatory=$False)]
[Alias('WFAR')]
[Switch]$WaitForAvailableRunspace,
[Parameter(Mandatory=$True, ParameterSetName = 'Await')] [Parameter(Mandatory=$True, ParameterSetName = 'Await')]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[Alias('TL')] [Alias('TL')]
@@ -317,7 +345,7 @@ Function Invoke-PSThread
[Parameter(Mandatory=$False, ParameterSetName = 'Await')] [Parameter(Mandatory=$False, ParameterSetName = 'Await')]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[Alias('TO')] [Alias('LT')]
[System.TimeSpan]$LoopTimeout, [System.TimeSpan]$LoopTimeout,
[Parameter(Mandatory=$False, ParameterSetName = 'Await')] [Parameter(Mandatory=$False, ParameterSetName = 'Await')]
@@ -325,10 +353,6 @@ Function Invoke-PSThread
[Alias('LD')] [Alias('LD')]
[System.TimeSpan]$LoopDuration, [System.TimeSpan]$LoopDuration,
[Parameter(Mandatory=$False)]
[Alias('WFAR')]
[Switch]$WaitForAvailableRunspace,
[Parameter(Mandatory=$False)] [Parameter(Mandatory=$False)]
[Alias('COE')] [Alias('COE')]
[Switch]$ContinueOnError [Switch]$ContinueOnError
@@ -433,6 +457,25 @@ Function Invoke-PSThread
$LoggingDetails.LogMessage = "$($GetCurrentDateTimeMessageFormat.Invoke()) - Parameter Set Name: $($ParameterSetName)" $LoggingDetails.LogMessage = "$($GetCurrentDateTimeMessageFormat.Invoke()) - Parameter Set Name: $($ParameterSetName)"
Write-Verbose -Message ($LoggingDetails.LogMessage) Write-Verbose -Message ($LoggingDetails.LogMessage)
#Define default parameter values
Switch ($True)
{
{($Null -ieq $ApartmentState)}
{
[System.Threading.ApartmentState]$ApartmentState = [System.Threading.ApartmentState]::MTA
}
{($Null -ieq $ThreadOption)}
{
[System.Management.Automation.Runspaces.PSThreadOptions]$ThreadOption = [System.Management.Automation.Runspaces.PSThreadOptions]::Default
}
{($Null -ieq $MaximumRunspaces) -or ($MaximumRunspaces -eq 0)}
{
$MaximumRunspaces = ((Get-CIMInstance -Namespace 'Root\CIMv2' -ClassName 'Win32_Processor' -Property 'NumberOfLogicalProcessors' -Verbose:$False).NumberOfLogicalProcessors | Measure-Object -Sum).Sum * 2
}
}
} }
Catch Catch
{ {
@@ -568,8 +611,8 @@ Function Invoke-PSThread
{ {
$ConfigurationTable = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary' $ConfigurationTable = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
$ConfigurationTable.Runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($Host, $InitialSessionState) $ConfigurationTable.Runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($Host, $InitialSessionState)
$ConfigurationTable.Runspace.ThreadOptions = [System.Management.Automation.Runspaces.PSThreadOptions]::ReuseThread $ConfigurationTable.Runspace.ThreadOptions = $ThreadOption
$ConfigurationTable.Runspace.ApartmentState = [System.Threading.ApartmentState]::MTA $ConfigurationTable.Runspace.ApartmentState = $ApartmentState
$ConfigurationTable.Thread = [System.Management.Automation.PowerShell]::Create() $ConfigurationTable.Thread = [System.Management.Automation.PowerShell]::Create()
@@ -596,8 +639,8 @@ Function Invoke-PSThread
{ {
ForEach ($RunspaceParameter In $RunspaceParameters.GetEnumerator()) ForEach ($RunspaceParameter In $RunspaceParameters.GetEnumerator())
{ {
#$LoggingDetails.LogMessage = "$($GetCurrentDateTimeMessageFormat.Invoke()) - Attempting to add the value for parameter `"$($RunspaceParameter.Key)`" into the runspace. Please Wait..." $LoggingDetails.LogMessage = "$($GetCurrentDateTimeMessageFormat.Invoke()) - Attempting to add the value for parameter `"$($RunspaceParameter.Key)`" into the runspace. Please Wait..."
#Write-Verbose -Message ($LoggingDetails.LogMessage) Write-Verbose -Message ($LoggingDetails.LogMessage)
$Null = $ConfigurationTable.Thread.AddParameter($RunspaceParameter.Key, $RunspaceParameter.Value) $Null = $ConfigurationTable.Thread.AddParameter($RunspaceParameter.Key, $RunspaceParameter.Value)
} }
@@ -618,16 +661,12 @@ Function Invoke-PSThread
{($_ -iin @('RunspacePool'))} {($_ -iin @('RunspacePool'))}
{ {
$ProcessorInfoPropertyName = 'NumberOfLogicalProcessors'
$ProcessorInfo = Get-CIMInstance -Namespace 'Root\CIMv2' -ClassName 'Win32_Processor' -Property ($ProcessorInfoPropertyName) -Verbose:$False
$ConfigurationTable = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary' $ConfigurationTable = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
$ConfigurationTable.MinimumRunspaces = 1 $ConfigurationTable.MinimumRunspaces = 1
$ConfigurationTable.MaximumRunspaces = ($ProcessorInfo | Measure-Object -Property ($ProcessorInfoPropertyName) -Sum).Sum $ConfigurationTable.MaximumRunspaces = $MaximumRunspaces
$ConfigurationTable.RunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool($ConfigurationTable.MinimumRunspaces, $ConfigurationTable.MaximumRunspaces, $InitialSessionState, $Host) $ConfigurationTable.RunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool($ConfigurationTable.MinimumRunspaces, $ConfigurationTable.MaximumRunspaces, $InitialSessionState, $Host)
$ConfigurationTable.RunspacePool.ThreadOptions = [System.Management.Automation.Runspaces.PSThreadOptions]::ReuseThread $ConfigurationTable.RunspacePool.ThreadOptions = $ThreadOption
$ConfigurationTable.RunspacePool.ApartmentState = [System.Threading.ApartmentState]::MTA $ConfigurationTable.RunspacePool.ApartmentState = $ApartmentState
$Null = $ConfigurationTable.RunspacePool.Open() $Null = $ConfigurationTable.RunspacePool.Open()
@@ -714,7 +753,8 @@ Function Invoke-PSThread
Default Default
{ {
###Invalid/Null input object list item. Skip. $LoggingDetails.WarningMessage = "$($GetCurrentDateTimeMessageFormat.Invoke()) - An invalid input object item was detected. Skipping..."
Write-Warning -Message ($LoggingDetails.WarningMessage)
} }
} }
} }