PowerShell / Win32-OpenSSH

Win32 port of OpenSSH
7.44k stars 762 forks source link

Subsystem for 5.1 #1938

Open Hvid opened 2 years ago

Hvid commented 2 years ago

"OpenSSH for Windows" version 8.9.1.0

Server OperatingSystem Windows Server 2016 Standard

Client OperatingSystem Windows Server 2022 Standard

Is it possible to have a subsystem for Powershell 5.1? I currently have this working: Subsystem powershell c:/progra~1/powershell/7/pwsh.exe -sshs -NoLogo

I also have these set in registry:

PS C:\Windows\system32> Get-Item HKLM:\SOFTWARE\OpenSSH\

    Hive: HKEY_LOCAL_MACHINE\SOFTWARE

Name                           Property
----                           --------
OpenSSH                        DefaultShell              : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
                               DefaultShellCommandOption : /c

PS C:\Windows\system32>

When i ssh into the machine with ssh.exe i get Powershell 5.1. When i Invoke-Command from Powershell 7.2.3 on my client, i get into Powershell 7.2.3 on the server as well. Is it possible to enter Powershell 5.1 from 7.2.3?

I was thinking a subsystem for 5.1, but powershell.exe doesn't have the -sshs switch and this doesn't work: Subsystem powershell5 c:/Windows/System32/WINDOW~1/v1.0/powershell.exe -NoLogo

jborean93 commented 2 years ago

It's not in any way supported by MS or the PowerShell team but I came up with a wrapper that can give you what you want https://gist.github.com/jborean93/7d4cb107fa06251b080fa10ec844893e. You would place this script on your host and set the subsystem to Subsystem ... C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File C:/path/to/script.ps1.

The reason why this is needed is due to some weird problem with how OpenSSH creates an overlapped stdin pipe for processes and .NET Framework's console handles unable to read from such inputs. The script works around this by using a StreamReader over the stdin handle and just exchanging the PSRP input and output as per usual.

Hvid commented 2 years ago

Thanks, that works! I hope MS will eventually fix it, but for now i'll use your script.