Skip to content

Commit 91a6293

Browse files
(GH-1153) Fix Powershell adapter PSModulePath normalization for WinPS
Prior to this change, the adapter munged the PSModulePath by removing any path items containing the segment `\powershell\` to ensure no PowerShell 7 modules are used for discovery when invoked with the Windows PowerShell adapter. This implementation was highlighted as a bug after moving and renaming the `powershell-adapter` folder to `adapters/powershell`. The tests showed that none of the test modules in the `Tests` subfolder were discovered by the adapter. The prior implementation removed not only the actual PowerShell 7 module paths, but _any_ module path with `powershell` anywhere in the ancestor folder path. Not only does this cause the tests to fail, but it also prevents users from discovering any modules they might have added in custom locations like `~\dev\powershell` or `D:\Tools\PowerShell`. This change updates the implementation to specifically search for and remove the default module paths for PowerShell _if_ PowerShell is installed. It no longer uses a simple match for any path that contains `powershell` as an ancester folder.
1 parent a457d55 commit 91a6293

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

adapters/powershell/psDscAdapter/powershell.resource.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ if ($Operation -eq 'ClearCache') {
5050

5151
if ($PSVersionTable.PSVersion.Major -le 5) {
5252
# For Windows PowerShell, we want to remove any PowerShell 7 paths from PSModulePath
53-
$env:PSModulePath = ($env:PSModulePath -split ';' | Where-Object { $_ -notlike '*\powershell\*' }) -join ';'
53+
if ($pwshPath = Get-Command 'pwsh' -ErrorAction Ignore | Select-Obect -ExpandProperty Source) {
54+
$pwshDefaultModulePaths = @(
55+
"$HOME\Documents\PowerShell\Modules" # CurrentUser
56+
"$Env:ProgramFiles\PowerShell\Modules" # AllUsers
57+
Join-Path $(Split-Path $pwshPath -Parent) 'Modules' # Builtin
58+
)
59+
$env:PSModulePath = ($env:PSModulePath -split ';' | Where-Object { $_ -notin $pwshDefaultModulePaths }) -join ';'
60+
}
5461
}
5562

5663
if ('Validate' -ne $Operation) {

0 commit comments

Comments
 (0)