0

I'm playing around with creating and destroying runspaces. Everything seems to work as expected, but when I'm trying to access Runspace.name property I get duplicated name someName someName. Here's doc reference to this property

Expected output

RUNSPACE Boobs CREATED
RUNSPACE Boobs DESTROYED

Actual output

RUNSPACE Boobs CREATED
RUNSPACE Boobs Boobs DESTROYED

Code

function main {
  $runspaceObject = createRunspace "Boobs"
  $output = destroyRunspace $runspaceObject
}
function createRunspace {
  param($name)
  $hash = [hashtable]::Synchronized(@{})
  $runspace = [runspacefactory]::CreateRunspace()
  $runspace.Name = $name
  $runspace.Open()
  $runspace.SessionStateProxy.SetVariable('hash', $hash)
  $powershell = [powershell]::Create()
  $powershell.Runspace = $runspace
  $powershell.AddScript({
    return "RUNSPACE RETURN VALUE"
  })
  $runspaceObject = @{
    handle = $powershell.BeginInvoke()
    runspace = $runspace
    powershell = $powershell
    hash = $hash
  }
  Write-Host "RUNSPACE $name CREATED"
  return $runspaceObject
}

function destroyRunspace {
  param($runspaceObject)
  $output = $runspaceObject.powershell.EndInvoke($runspaceObject.handle)
  $runspaceObject.runspace.Close()
  $runspaceObject.powershell.Dispose()
  $name = $runspaceObject.runspace.name
  Write-Host "RUNSPACE $name DESTROYED"
  return $output
}

main
W. Churchill
  • 346
  • 1
  • 7
  • 28
manidos
  • 3,244
  • 4
  • 29
  • 65

0 Answers0