Asked this question yesterday...
Returning ArrayList from Function/Script
Purpose of this question was to help me with this function...
function Initialize-ThreadPool {
Param(
[Parameter(Mandatory=$true,Position=1)][scriptblock] $ScriptBlock
, [Parameter(Mandatory=$false,Position=2)][Hashtable] $Parameters
, [Parameter(Mandatory=$true,Position=3)][int64] $Occurrences
, [Parameter(Mandatory=$false,Position=4)][int] $Throttle = 3
)
$threads_all = New-Object 'System.Collections.ArrayList'
[runspacefactory]::CreateRunspacePool() | Out-Null
$session_state = `
[System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$runspace_pool = [runspacefactory]::CreateRunspacePool(1, $Throttle)
$runspace_pool.ApartmentState = "STA"
$runspace_pool.Open()
for ($iterator = 1; $iterator -le $Occurrences; $iterator++) {
$session = [Powershell]::Create()
$session.AddScript($ScriptBlock)
if ( $Parameters ) {
$session.AddParameters($Parameters) | Out-Null
}
$session.RunspacePool = $runspace_pool
$thread_created = @{
'session' = $session
'thread_handle' = $session.BeginInvoke()
}
$threads_all.Add($thread_created) | Out-Null
}
Write-Output -NoEnumerate $threads_all
}
When I call this function, I get back an object array and not an ArrayList, and I don't understand why. I'm doing more or less the same thing here...
function f {
[CmdletBinding()]Param()
Write-Verbose 'f: Start'
$t = New-Object 'System.Collections.ArrayList'
Write-Verbose $t.GetType().Name
Write-Output -NoEnumerate $t
}
$things = New-Object 'System.Collections.ArrayList'
$things.GetType().Name
$things = f -verbose
$things.GetType().Name
And, I get an ArrayList back in that case. WTF?