From 16823a10345eeebe407197b14bc1bbb3a65c6fac Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Sun, 7 Sep 2025 15:06:32 +0200 Subject: [PATCH 1/4] Fix empty path handling for Windows PowerShell adapter --- .../Tests/win_powershellgroup.tests.ps1 | 2 +- .../psDscAdapter/win_psDscAdapter.psm1 | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/powershell-adapter/Tests/win_powershellgroup.tests.ps1 b/powershell-adapter/Tests/win_powershellgroup.tests.ps1 index 88f991697..f5c505478 100644 --- a/powershell-adapter/Tests/win_powershellgroup.tests.ps1 +++ b/powershell-adapter/Tests/win_powershellgroup.tests.ps1 @@ -118,7 +118,7 @@ class PSClassResource { New-Item -Path $modulePath -ItemType File -Value $module -Force | Out-Null } - $env:PSModulePath = $windowsPowerShellPath + [System.IO.Path]::PathSeparator + $env:PSModulePath + $env:PSModulePath = $windowsPowerShellPath + [System.IO.Path]::PathSeparator + $env:PSModulePath + [System.IO.Path]::PathSeparator } AfterAll { diff --git a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 index 47ea29bee..82dcdb94f 100644 --- a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 @@ -60,6 +60,9 @@ function Invoke-DscCacheRefresh { $namedModules = [System.Collections.Generic.List[Object]]::new() $cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" + # Reset PSModulePath to not contain empty entries + Set-ValidPSModulePath + if (Test-Path $cacheFilePath) { "Reading from Get-DscResource cache file $cacheFilePath" | Write-DscTrace @@ -244,10 +247,10 @@ function Invoke-DscCacheRefresh { } $dscResourceCacheEntries.Add([dscResourceCacheEntry]@{ - Type = "$moduleName/$($dscResource.Name)" - DscResourceInfo = $DscResourceInfo - LastWriteTimes = $lastWriteTimes - }) + Type = "$moduleName/$($dscResource.Name)" + DscResourceInfo = $DscResourceInfo + LastWriteTimes = $lastWriteTimes + }) } if ($namedModules.Count -gt 0) { @@ -701,6 +704,19 @@ function GetClassBasedCapabilities { } } +function Set-ValidPSModulePath { + [CmdletBinding()] + param() + + end { + if (($env:PSModulePath -split [System.IO.Path]::PathSeparator) -contains '') { + "Removing empty entry from PSModulePath: '$env:PSModulePath'" | Write-DscTrace -Operation Debug + $env:PSModulePath = ($env:PSModulePath -split [System.IO.Path]::PathSeparator | Where-Object { $_ -ne '' }) -join [System.IO.Path]::PathSeparator + } + } +} + + # cached resource class dscResourceCacheEntry { [string] $Type From d025084741dd9fd1875d082592579d2c2db69906 Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Sun, 7 Sep 2025 15:07:23 +0200 Subject: [PATCH 2/4] Formatting --- powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 index 82dcdb94f..4584ce455 100644 --- a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 @@ -711,7 +711,8 @@ function Set-ValidPSModulePath { end { if (($env:PSModulePath -split [System.IO.Path]::PathSeparator) -contains '') { "Removing empty entry from PSModulePath: '$env:PSModulePath'" | Write-DscTrace -Operation Debug - $env:PSModulePath = ($env:PSModulePath -split [System.IO.Path]::PathSeparator | Where-Object { $_ -ne '' }) -join [System.IO.Path]::PathSeparator + $env:PSModulePath = ($env:PSModulePath -split [System.IO.Path]::PathSeparator | + Where-Object { $_ -ne '' }) -join [System.IO.Path]::PathSeparator } } } From 4aeae508a0c67653916e8db8e1785c4c9c76da3f Mon Sep 17 00:00:00 2001 From: GijsR Date: Mon, 8 Sep 2025 16:50:27 +0200 Subject: [PATCH 3/4] Resolve remark --- .../psDscAdapter/win_psDscAdapter.psm1 | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 index 4584ce455..b7d2fc945 100644 --- a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 @@ -60,8 +60,8 @@ function Invoke-DscCacheRefresh { $namedModules = [System.Collections.Generic.List[Object]]::new() $cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" - # Reset PSModulePath to not contain empty entries - Set-ValidPSModulePath + # Repair PSModulePath to not contain empty entries + Repair-ValidPSModulePath if (Test-Path $cacheFilePath) { "Reading from Get-DscResource cache file $cacheFilePath" | Write-DscTrace @@ -247,10 +247,10 @@ function Invoke-DscCacheRefresh { } $dscResourceCacheEntries.Add([dscResourceCacheEntry]@{ - Type = "$moduleName/$($dscResource.Name)" - DscResourceInfo = $DscResourceInfo - LastWriteTimes = $lastWriteTimes - }) + Type = "$moduleName/$($dscResource.Name)" + DscResourceInfo = $DscResourceInfo + LastWriteTimes = $lastWriteTimes + }) } if ($namedModules.Count -gt 0) { @@ -704,15 +704,14 @@ function GetClassBasedCapabilities { } } -function Set-ValidPSModulePath { +function Repair-ValidPSModulePath { [CmdletBinding()] param() end { if (($env:PSModulePath -split [System.IO.Path]::PathSeparator) -contains '') { "Removing empty entry from PSModulePath: '$env:PSModulePath'" | Write-DscTrace -Operation Debug - $env:PSModulePath = ($env:PSModulePath -split [System.IO.Path]::PathSeparator | - Where-Object { $_ -ne '' }) -join [System.IO.Path]::PathSeparator + $env:PSModulePath = [String]::Join([System.IO.Path]::PathSeparator, ($env:PSModulePath.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries))) } } } From e49334dc13138070cf8dd5a62eaed097dfe02b1c Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Mon, 8 Sep 2025 17:40:13 +0200 Subject: [PATCH 4/4] Remove seperator at the end --- powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 index b7d2fc945..c206d89d9 100644 --- a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 @@ -711,7 +711,7 @@ function Repair-ValidPSModulePath { end { if (($env:PSModulePath -split [System.IO.Path]::PathSeparator) -contains '') { "Removing empty entry from PSModulePath: '$env:PSModulePath'" | Write-DscTrace -Operation Debug - $env:PSModulePath = [String]::Join([System.IO.Path]::PathSeparator, ($env:PSModulePath.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries))) + $env:PSModulePath = [String]::Join([System.IO.Path]::PathSeparator, ($env:PSModulePath.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries))).TrimEnd([System.IO.Path]::PathSeparator) } } }