0

I am trying to create some file using runspacepool to parallelize the operation but I am invoking this runspacepool to itself get invoked from inside a runspace (lets say a parent runspace).

Below is the code and the problem is that files never get created. If I execute the runspace pool without the parent runspace then runspacepool code works fine. Could someone please suggest what am I missing here.

function RunTestpool {

    param()
   
    ** Worker thread to create file invoked by worker2 thread**
    $Worker = {
        param($Filename)
     
        Write-Output  "Inside the script tag
        $Item = New-Item -Name $Filename -Path 'D:\Test'
        Write-Output "$($Item.FullName)"
    }

    ** Worker thread to loop over filenames and call worker thread to create file (above)**

    $Worker2 = {
        param()
       
        $MaxRunspaces = 5
        $RunspacePool = [runspacefactory]::CreateRunspacePool(1, $MaxRunspaces)
        $RunspacePool.Open()
        $Jobs = New-Object System.Collections.ArrayList

        $Filenames = @("file1.txt", "file2.txt", "file3.txt", "file4.txt", "file5.txt", 
        "file6.txt", "file7.txt", "file8.txt", "file9.txt", "file10.txt", "file11.txt")

        foreach ($File in $Filenames) {
            Write-Host "Creating runspace for $File"
            $p1 = [powershell]::Create()
            $p1.RunspacePool = $RunspacePool
            $inputparams =@{
                 Filename=$File
            }

            $p1.AddScript($Worker).AddParameters($inputparams) | Out-Null

            $JobObj = New-Object -TypeName PSObject -Property @{
                Job = $p1.BeginInvoke()
                Instance = $p1
            }

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

 

        while ($Jobs.Job.IsCompleted -contains $false) {
            Write-Output "$($(Get-date).Tostring()) Still running..." 
            Start-Sleep 1
        }

        foreach($j in $Jobs) {
          $j.Instance.EndInvoke($j.Job)
        }
    }

 

    if ($global:TestpoolJob)
    {
                
                $global:TestpoolJob.Instance.EndInvoke($global:TestpoolJob.Job) | Out-Null
                $global:TestpoolJob.Job.AsyncWaitHandle.Dispose()
                $global:TestpoolJob.Instance.Runspace.Close()
                $global:TestpoolJob.Instance.Runspace.Dispose()
                $global:TestpoolJob.Instance.Dispose()
                $global:TestpoolJob = $null
                $job=$null
    }


    **Parent Runspace**

    $path = Convert-Path .
    $k = [PowerShell]::Create()
    $k.Runspace = [RunspaceFactory]::CreateRunspace()
    $k.Runspace.Open()
    $inputparams =@{
        #Filename="file1.txt"
        path=$path
    }
    $k.AddScript($Worker2).AddParameters($inputparams)| Out-Null
    $job = $k.BeginInvoke()

    $global:TestpoolJob = New-Object PSObject -Property @{
                Instance = $k
                Job = $job
            }
}

 

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
Rudrani Angira
  • 956
  • 2
  • 14
  • 28

0 Answers0