Skip to content

Using PowerShell Core RC-1 as an OpenSSH subsystem on windows breaks core module loading #7679

Closed

Description

Steps to reproduce

  • Install OpenSSH on Windows Server 2016

  • Configure PowerShell as a subsystem
    NOTE: Due to path issues, create a symlink or copy PowerShell Core to a directory without spaces
    Subsystem powershell c:\powershell\pwsh.exe -sshs -NoLogo -NoProfile

  • Connect from a linux or windows system

$session = New-PSSession -HostName hostname -UserName username
Enter-PSSession $session

Expected behavior

The session is entered.

Actual behavior

The command fails

PS /home/psrptest> enter-pssession $session
enter-pssession : The 'Measure-Object' command was found in the module 'Microsoft.PowerShell.Utility', but the module could not be loaded. For more information, run 'Import-Module Microsoft.PowerShell.Utility'.
At line:1 char:1
+ enter-pssession $session
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Measure-Object:String) [Enter-PSSession], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

Using Invoke-Command to invoke Test-Path on the session also produces an error due to failing to load 'Microsoft.PowerShell.Management'

PS /home/psrptest> Invoke-Command -Session $session -ScriptBlock {Test-Path -Path C:\Windows}
The 'Test-Path' command was found in the module 'Microsoft.PowerShell.Management', but the module could not be loaded. For more information, run 'Import-Module Microsoft.PowerShell.Management'.
+ CategoryInfo          : ObjectNotFound: (Test-Path:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
+ PSComputerName        : psrpwinsrv.fsm0dauomfbubcgw4wlghhwv3d.yx.internal.cloudapp.net

Using Invoke-Command to examine $env:PSModulePath, it appears suspicious in that $PSHOME is the last entry in the apath.

PS /home/psrptest> Invoke-Command -Session $session -ScriptBlock {$PSHOME}
C:\powershell
PS /home/psrptest> Invoke-Command -Session $session -ScriptBlock {$env:PSModulePath}
C:\Users\psrptest\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;c:\powershell\Modules

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSRemotingProtocolVersion      2.3
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
GitCommitId                    6.1.0-rc.1
PSEdition                      Core
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
PSVersion                      6.1.0-rc.1

Activity

  1. added
    Issue-Bug
    WG-Remoting
    on Sep 3, 2018
  2. dantraMSFT commented on Sep 6, 2018

    @dantraMSFT
    ContributorAuthor

    The problem occurs when the PSModulePath from the native environment contains a reference to a SxS installation.

    ModuleIntrinsics.GetModulePath calls RemoveSxSPsHomeModulePath to remove it; which succeeds but the subsequent logic is has a few problems. In the repro case, systemModulePathToUse is populated with the PSHOME\Modules because hklmMachineModulePath is null and it is placed at the end of the PSModulePath. Using the pshome\modules path as the fallback is problem since later logic assumes they are different.

    For the repro case, I was using 6-preview as the default shell for OpenSSH and using RC-1 as the subsystem. OpenSSH adds the path of the default shell to the PSModulePath environment variable which is picked up by the subsystem process.

  3. dantraMSFT commented on Sep 6, 2018

    @dantraMSFT
    ContributorAuthor

    The problem occurs when the environment's path already contains the Windows PowerShell module path (%windir%\system32\WindowsPowerShell\v1.0\Modules).

    The current logic conditionally appends it if it is not present but doesn't consider it when adding $PSHOME\Modules. The result is $PSHOME\Modules is added after Windows PowerShell causing built in modules to be loaded from Windows PowerShell; many of which fail.

    The problem only occurs when SxS versions of PowerShell containing the PSCompatibleEdition changes are installed and one launches the other or if the Windows PowerShell module path is present in the environment's PSModulePath.

    A possible fix is to detect the existence of WP's module path and insert $PSHOME\Modules before it.

  4. SteveL-MSFT commented on Sep 7, 2018

    @SteveL-MSFT
    Member

    Talked to Dan Travison (@dantraMSFT), this seems more of an advanced use case than a common case for most users. If the fix comes in and is low risk, we can consider for 6.1 GA, but it's not a blocker for GA.

  5. dantraMSFT commented on Sep 7, 2018

    @dantraMSFT
    ContributorAuthor

    Steve Lee (@SteveL-MSFT): The proposed fix and test are here: #7727

  6. added a commit that references this issue on Sep 10, 2018
    4683934
  7. added a commit that references this issue on Aug 6, 2026
    ecce145
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-BugResolution-FixedWG-Remoting

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions