Skip to content

Runspace.Open() sometimes completes without the specified ImportPSModules becoming available #7377

@alx9r

Description

@alx9r

Usually when a module is specified using InitialSessionState.ImportPSModule() and Runspace.Open() is invoked that module is imported into the runspace. It is then output when Get-Module is invoked in the runspace. Sometimes, however, the specified module is not imported (or at least is not available using Get-Module) despite that Runspace.Open() succeeded.

There seems to be variation from run-to-run of this phenomenon:

  • some modules never import successfully
  • some modules always import successfully
  • some modules import successfully only sometimes

Steps to reproduce

foreach ( $module in Get-Module -ListAvailable )
{
    "Testing Module $($module.Name)"
    $initialSessionState = [initialsessionstate]::CreateDefault()
    $initialSessionState.ImportPSModule($module.Name)
    $runspace = [runspacefactory]::CreateRunspace($initialSessionState)
    $runspace.Open()
    $powershell = [powershell]::Create().AddScript("Get-Module $($module.Name)")
    $powershell.Runspace = $runspace
    if ( -not $powershell.Invoke() )
    {
        Write-Error 'Module not imported.'
    }
    else {
        'success'
    }
}

Expected behavior

Testing Module CimCmdlets
success
Testing Module Microsoft.PowerShell.Archive
success
Testing Module Microsoft.PowerShell.Diagnostics
success
Testing Module Microsoft.PowerShell.Host
success
Testing Module Microsoft.PowerShell.Management
success
Testing Module Microsoft.PowerShell.Security
success
Testing Module Microsoft.PowerShell.Utility
success
Testing Module Microsoft.WSMan.Management
success
Testing Module PackageManagement
success
Testing Module PowerShellGet
success
Testing Module PSDesiredStateConfiguration
success
Testing Module PSDiagnostics
success
Testing Module PSReadLine
success

Actual behavior

Testing Module CimCmdlets
success
Testing Module Microsoft.PowerShell.Archive
success
Testing Module Microsoft.PowerShell.Diagnostics
C:\users\un1\Desktop\test2.ps1 : Module not imported.
+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test
2.ps1

Testing Module Microsoft.PowerShell.Host
C:\users\un1\Desktop\test2.ps1 : Module not imported.
+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test
2.ps1

Testing Module Microsoft.PowerShell.Management
C:\users\un1\Desktop\test2.ps1 : Module not imported.
+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test
2.ps1

Testing Module Microsoft.PowerShell.Security
C:\users\un1\Desktop\test2.ps1 : Module not imported.
+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test
2.ps1

Testing Module Microsoft.PowerShell.Utility
C:\users\un1\Desktop\test2.ps1 : Module not imported.
+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test
2.ps1

Testing Module Microsoft.WSMan.Management
C:\users\un1\Desktop\test2.ps1 : Module not imported.
+ CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test
2.ps1

Testing Module PackageManagement
success
Testing Module PowerShellGet
success
Testing Module PSDesiredStateConfiguration
success
Testing Module PSDiagnostics
success
Testing Module PSReadLine
success

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.0-preview.4
PSEdition                      Core
GitCommitId                    6.1.0-preview.4
OS                             Microsoft Windows 6.3.9600 
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Corecmdlets in the Microsoft.PowerShell.Core moduleWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions