Skip to content

Commit ec8fd50

Browse files
authored
Merge pull request #1097 from Gijsreyn/gh-1095/main/fix-empty-winpspath-handling
Fix empty path handling for Windows PowerShell adapter
2 parents db7aff0 + e49334d commit ec8fd50

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

powershell-adapter/Tests/win_powershellgroup.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class PSClassResource {
118118
New-Item -Path $modulePath -ItemType File -Value $module -Force | Out-Null
119119
}
120120

121-
$env:PSModulePath = $windowsPowerShellPath + [System.IO.Path]::PathSeparator + $env:PSModulePath
121+
$env:PSModulePath = $windowsPowerShellPath + [System.IO.Path]::PathSeparator + $env:PSModulePath + [System.IO.Path]::PathSeparator
122122
}
123123

124124
AfterAll {

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ function Invoke-DscCacheRefresh {
6060
$namedModules = [System.Collections.Generic.List[Object]]::new()
6161
$cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
6262

63+
# Repair PSModulePath to not contain empty entries
64+
Repair-ValidPSModulePath
65+
6366
if (Test-Path $cacheFilePath) {
6467
"Reading from Get-DscResource cache file $cacheFilePath" | Write-DscTrace
6568

@@ -701,6 +704,19 @@ function GetClassBasedCapabilities {
701704
}
702705
}
703706

707+
function Repair-ValidPSModulePath {
708+
[CmdletBinding()]
709+
param()
710+
711+
end {
712+
if (($env:PSModulePath -split [System.IO.Path]::PathSeparator) -contains '') {
713+
"Removing empty entry from PSModulePath: '$env:PSModulePath'" | Write-DscTrace -Operation Debug
714+
$env:PSModulePath = [String]::Join([System.IO.Path]::PathSeparator, ($env:PSModulePath.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries))).TrimEnd([System.IO.Path]::PathSeparator)
715+
}
716+
}
717+
}
718+
719+
704720
# cached resource
705721
class dscResourceCacheEntry {
706722
[string] $Type

0 commit comments

Comments
 (0)