From 14487bf85d48764239047843f7d324aace55143e Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 13 Apr 2020 23:08:01 -0700 Subject: [PATCH] Make GetWindowsPowerShellModulePath compatible with multiple PS installations (#12280) Add additional check for each component of PSModulePath (that is set for WinPS process) - if it is has pwsh.exe in the parent directory, then it is considered another PS Core installation and this location is also filtered out. --- .../engine/Modules/ModuleIntrinsics.cs | 21 ++++++++++++++++--- .../CompatiblePSEditions.Module.Tests.ps1 | 4 ++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs b/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs index 3d03276613..a4d343131f 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs @@ -1234,7 +1234,7 @@ namespace System.Management.Automation #if !UNIX /// - /// Returns a PSModulePath suiteable for Windows PowerShell by removing this PowerShell's specific + /// Returns a PSModulePath suiteable for Windows PowerShell by removing PowerShell's specific /// paths from current PSModulePath. /// /// @@ -1261,9 +1261,24 @@ namespace System.Management.Automation var modulePathList = new List(); foreach (var path in currentModulePath.Split(';')) { - if (!excludeModulePaths.Contains(path)) + var trimmedPath = path.Trim(); + if (!excludeModulePaths.Contains(trimmedPath)) { - modulePathList.Add(path); + // make sure this module path is Not part of other PS Core installation + var possiblePwshDir = Path.GetDirectoryName(trimmedPath); + + if (string.IsNullOrEmpty(possiblePwshDir)) + { + // i.e. module dir is in the drive root + modulePathList.Add(trimmedPath); + } + else + { + if (!File.Exists(Path.Combine(possiblePwshDir, "pwsh.dll"))) + { + modulePathList.Add(trimmedPath); + } + } } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/CompatiblePSEditions.Module.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/CompatiblePSEditions.Module.Tests.ps1 index 582dfa0bf3..921b7af379 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/CompatiblePSEditions.Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/CompatiblePSEditions.Module.Tests.ps1 @@ -646,6 +646,10 @@ Describe "PSModulePath changes interacting with other PowerShell processes" -Tag $errors | Should -Be $null } + It "Allows Windows PowerShell subprocesses to load WinPS version of `$PSHOME modules" { + powershell.exe -Command "Get-ChildItem | Out-Null;(Get-Module Microsoft.PowerShell.Management).Path" | Should -BeLike "*system32*" + } + It "Allows PowerShell subprocesses to call core modules" { $errors = & $pwsh -Command "Get-ChildItem" 2>&1 | Where-Object { $_ -is [System.Management.Automation.ErrorRecord] } $errors | Should -Be $null