From af9b99a4d38e0cf1e54c4bbd89cbb6a8a8598c4e Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL HE/HIM) (from Dev Box)" Date: Thu, 1 May 2025 16:28:14 -0700 Subject: [PATCH] For WinPS Adapter remove PS7 paths from PSModulePath --- .../psDscAdapter/powershell.resource.ps1 | 5 +++++ .../psDscAdapter/win_psDscAdapter.psm1 | 14 +------------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/powershell-adapter/psDscAdapter/powershell.resource.ps1 b/powershell-adapter/psDscAdapter/powershell.resource.ps1 index 54cf101a6..d5417775e 100644 --- a/powershell-adapter/psDscAdapter/powershell.resource.ps1 +++ b/powershell-adapter/psDscAdapter/powershell.resource.ps1 @@ -45,6 +45,11 @@ if ($Operation -eq 'ClearCache') { exit 0 } +if ($PSVersionTable.PSVersion.Major -le 5) { + # For Windows PowerShell, we want to remove any PowerShell 7 paths from PSModulePath + $env:PSModulePath = ($env:PSModulePath -split ';' | Where-Object { $_ -notlike '*\powershell\*' }) -join ';' +} + if ('Validate' -ne $Operation) { Write-DscTrace -Operation Debug -Message "jsonInput=$jsonInput" diff --git a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 index 3550b0103..693e63395 100644 --- a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 @@ -54,19 +54,7 @@ function Invoke-DscCacheRefresh { $refreshCache = $false - $cacheFilePath = if ($IsWindows) { - # PS 6+ on Windows - Join-Path $env:LocalAppData "dsc\PSAdapterCache.json" - } - else { - # either WinPS or PS 6+ on Linux/Mac - if ($PSVersionTable.PSVersion.Major -le 5) { - Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" - } - else { - Join-Path $env:HOME ".dsc" "PSAdapterCache.json" - } - } + $cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" if (Test-Path $cacheFilePath) { "Reading from Get-DscResource cache file $cacheFilePath" | Write-DscTrace