interactive icon indicating copy to clipboard operation
interactive copied to clipboard

PWSH kernel cannot create or enter WinPSCompatSession

Open iamthecage opened this issue 3 years ago • 2 comments
trafficstars

It doesn't seem that the PWSH kernel can create or access the WinPSCompatSession when trying to import a module into a background PS 5.1 process. I have a module that will not import directly in .NET Interactive PWSH due to the following error: Import-Module: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.

Due to System.Windows.Forms not being available in the interactive PWSH session, I'm trying to load that module in compatibility mode. The expected behavior when importing the module with "-UseWindowsPowerShell" should be that the module is loaded in a background compatibility session to PS 5.1.
Windows_PowerShell_Compatibility

All examples below work on the desktop when running PWSH directly, but not in Interactive PWSH.

Using -UseWindowsPowerShell doesnt import any commands but throws no error (empty command list)

Import-Module powershelljmc -UseWindowsPowerShell
Get-Command -Module PowershellJMC
(blank)

Trying to enter the compatibility session after importing returns an error that no session exists

Import-Module powershelljmc -UseWindowsPowerShell  
$s = Get-PSSession -Name WinPSCompatSession  
Get-PSSession:   
Line |  
   2 |  $s = Get-PSSession -Name WinPSCompatSession  
     |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The remote session with the name WinPSCompatSession  is not available.

Opening a New-PSSession with -UseWindowsPowerShell returns a general error

$session = New-PSSession -UseWindowsPowerShell
New-PSSession: [localhost] An error has occurred which PowerShell cannot handle. A remote session might have ended.

Which version of .NET Interactive are you using? (In a notebook, run the #!about magic command. ): Version: 1.0.317502+9c2a225c543085a04516cc4fc490919d5925ba87

  • OS
    • [ ] Windows 11
    • [x] Windows 10
    • [ ] macOS
    • [ ] Linux (Please specify distro)
    • [ ] iOS
    • [ ] Android
  • Browser
    • [ ] Chrome
    • [ ] Edge
    • [ ] Firefox
    • [ ] Safari
  • Frontend
    • [ ] Jupyter Notebook
    • [ ] Jupyter Lab
    • [ ] nteract
    • [x] Visual Studio Code
    • [ ] Visual Studio Code Insiders
    • [ ] Visual Studio
    • [ ] Other (please specify)

Screenshots

If applicable, add screenshots to help explain your problem.

iamthecage avatar Apr 20 '22 19:04 iamthecage

I have currently encountered the same problem in an ASP.NET Core application. The error behavior described above was identical. In my search for a solution I came across the following issue. After I implemented the solution there, everything went smoothly again.

Maybe this will help you with your problem.

FSharpCSharp avatar Jun 10 '22 11:06 FSharpCSharp

Related: https://github.com/dotnet/interactive/issues/2041#issuecomment-1149324115

jonsequitur avatar Jun 21 '22 17:06 jonsequitur