0

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?

Adam
  • 3,891
  • 3
  • 19
  • 42
  • I guess that your object is already enumerated by the `.Add` method prior you `Write-Output`. (check with a `Write-Verbose $threads_all.GetType().Name` just before the `Write-Output`). Anyways, you might rebuild the array list by adding a comma in front of it. In other words try: `Write-Output ,$threads_all` or `Write-Output -NoEnumerate ,$threads_all`. – iRon Jun 14 '18 at 07:22
  • Trying `Write-Output ,$threads_all` generated an exception `Missing argument in parameter list.` You sure about that? – Adam Jun 14 '18 at 14:03
  • If I throw a `$threads_all.GetType()` before the `Write-Output...`, I can see that `$threads_all` is a ListArray. – Adam Jun 14 '18 at 14:04
  • Sorry, the my suggestion wasn't quiet correct (I am usually not using `Write-Output` myself but just drop the output on the pipeline. In other words just try only `,$threads_all` without `Write-Output`. – iRon Jun 14 '18 at 16:06
  • If I just leave `$threads_all`, it will enumerate the collection, and I'll end up with an immutable list on the otherside. mklement0 shared that yesterday in the following https://stackoverflow.com/questions/50843357/returning-arraylist-from-function-script/50843688#50843688 – Adam Jun 14 '18 at 18:14

0 Answers0