Powershell: New-Object System.Collections.ArrayList gets a Runspace attribute automatically

Viewed 45

I am new to Powershell and am debugging a powershell script in which I am creating runspaces and storing the objects holding these runspaces in an ArrayList.
While debugging, the ArrayList storing these objects automatically gets "Runspace" (while ($Jobs.Runspace.IsCompleted -contains $false){}) attribute on which I can call IsCompleted. I am not sure how this is happening; Is this something Powershell does internally or can I actually use this Runspace attribute to keep track of all the runspaces in that arraylist? Is there any documentation mentioning this and would it be recommended to use this.
I am following this blog while writing the script - https://xkln.net/blog/multithreading-in-powershell--running-a-specific-number-of-threads/

$Jobs = New-Object System.Collections.ArrayList
foreach ($File in $Filenames) {
    Write-Host "Creating runspace for $File"
    $PowerShell = [powershell]::Create()
    $PowerShell.RunspacePool = $RunspacePool
    $PowerShell.AddScript($Worker).AddArgument($File) | Out-Null
    
    $JobObj = New-Object -TypeName PSObject -Property @{
        Runspace = $PowerShell.BeginInvoke()
        PowerShell = $PowerShell  
    }

    $Jobs.Add($JobObj) | Out-Null
}

while ($Jobs.Runspace.IsCompleted -contains $false) {
    Write-Host (Get-date).Tostring() "Still running..."
    Start-Sleep 1
}
0 Answers
Related