Skip to content

The creation of an instance of the Powershell class by specifying an InitialSessionState, do not update the 'IsRunspaceOwner' property. #6422

Closed

Description

When I create an instance of the Powershell class by specifying an InitialSessionState, the 'IsRunspaceOwner' property is not updated.

Steps to reproduce

try {
  $InitialSessionState=  [System.Management.Automation.Runspaces.InitialSessionState]::Create()
  
 $SessionStateCmdletEntry = New-Object System.Management.Automation.Runspaces.SessionStateCmdletEntry 'Get-ChildItem','Microsoft.PowerShell.Commands.GetChildItemCommand',$null
 $InitialSessionState.Commands.Add($SessionStateCmdletEntry)

 $Provider = New-Object System.Management.Automation.Runspaces.SessionStateProviderEntry 'FileSystem',([Microsoft.PowerShell.Commands.FileSystemProvider]),$null
 $InitialSessionState.Providers.Add($Provider)

  $PS = [PowerShell]::Create($InitialSessionState)
  Write-Warning "Automatic release : $($ps.IsRunspaceOwner)"
   $PS.AddCommand("Get-ChildItem") > $null
   $Results = $PS.Invoke()                        
  Write-Warning "Automatic release : $($ps.IsRunspaceOwner)"

}
finally {
 $PS.Dispose()
}
$PS.Runspace

Expected behavior

WARNING: Automatic release : False
WARNING: Automatic release : True

 Id Name            ComputerName    Type          State         Availability
 -- ----            ------------    ----          -----         ------------
  2 Runspace2       localhost       Local         Closed        None

Actual behavior

#WARNING: Automatic release : False
#WARNING: Automatic release : False

$PS.Runspace

#  Id Name            ComputerName    Type          State         Availability
#  -- ----            ------------    ----          -----         ------------
#   2 Runspace2       localhost       Local         Opened        Available

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.2
PSEdition                      Core
GitCommitId                    v6.0.2
OS                             Microsoft Windows 6.1.7601 S
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.0.2}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Same error with Windows Powershell :

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.14409.1012
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1012}
BuildVersion                   10.0.14409.1012
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Mar 19, 2018
  2. iSazonov commented on Mar 19, 2018

    @iSazonov
    Collaborator

    IsRunspaceOwner = true means that a runspace is created by the PowerShell object.
    IsRunspaceOwner = false means that a runspace is created out of the PowerShell object and assigned to the PowerShell object.


    SetRunspace(RunspaceFactory.CreateRunspace(), true);

  3. SteveL-MSFT commented on Mar 19, 2018

    @SteveL-MSFT
    Member
  4. iSazonov commented on Mar 20, 2018

    @iSazonov
    Collaborator

    I updated my comment with code references.

  5. LaurentDardenne commented on Apr 10, 2018

    @LaurentDardenne
    Author

    Ilya (@iSazonov) Is your comment correct ?
    Is not it ?

    IsRunspaceOwner = false means that a runspace is created out of the PowerShell object and assigned to the PowerShell object.
    

    In my example who should release the created runspace?

  6. iSazonov commented on Apr 10, 2018

    @iSazonov
    Collaborator

    My comment fixed.

  7. iSazonov commented on Apr 10, 2018

    @iSazonov
    Collaborator

    If you created you should release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions