Skip to content

Commit 32015c7

Browse files
author
Andrew
committed
Added PSModulePath check
1 parent 9857ad2 commit 32015c7

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ function Invoke-DscCacheRefresh {
5050
$trace = @{'Debug' = "Reading from Get-DscResource cache file $cacheFilePath"} | ConvertTo-Json -Compress
5151
$host.ui.WriteErrorLine($trace)
5252

53-
$dscResourceCache = Get-Content -Raw $cacheFilePath | ConvertFrom-Json
53+
$cache = Get-Content -Raw $cacheFilePath | ConvertFrom-Json
54+
$dscResourceCacheEntries = $cache.ResourceCache
5455

55-
if ($dscResourceCache.Count -eq 0) {
56+
if ($dscResourceCacheEntries.Count -eq 0) {
5657
# if there is nothing in the cache file - refresh cache
5758
$refreshCache = $true
5859

@@ -64,7 +65,7 @@ function Invoke-DscCacheRefresh {
6465
$trace = @{'Debug' = "Checking cache for stale entries"} | ConvertTo-Json -Compress
6566
$host.ui.WriteErrorLine($trace)
6667

67-
foreach ($cacheEntry in $dscResourceCache) {
68+
foreach ($cacheEntry in $dscResourceCacheEntries) {
6869
$trace = @{'Trace' = "Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'"} | ConvertTo-Json -Compress
6970
$host.ui.WriteErrorLine($trace)
7071

@@ -82,6 +83,23 @@ function Invoke-DscCacheRefresh {
8283

8384
if ($refreshCache) {break}
8485
}
86+
87+
$trace = @{'Debug' = "Checking cache for stale PSModulePath"} | ConvertTo-Json -Compress
88+
$host.ui.WriteErrorLine($trace)
89+
90+
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -ea SilentlyContinue}
91+
92+
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
93+
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
94+
$hs_cache.SymmetricExceptWith($hs_live)
95+
$diff = $hs_cache
96+
97+
$trace = @{'Debug' = "PSModulePath diff '$diff'"} | ConvertTo-Json -Compress
98+
$host.ui.WriteErrorLine($trace)
99+
100+
if ($diff.Count -gt 0) {
101+
$refreshCache = $true
102+
}
85103
}
86104
}
87105
else {
@@ -96,7 +114,7 @@ function Invoke-DscCacheRefresh {
96114
$host.ui.WriteErrorLine($trace)
97115

98116
# create a list object to store cache of Get-DscResource
99-
[dscResourceCacheEntry[]]$dscResourceCache = [System.Collections.Generic.List[Object]]::new()
117+
[dscResourceCacheEntry[]]$dscResourceCacheEntries = [System.Collections.Generic.List[Object]]::new()
100118

101119
# improve by performance by having the option to only get details for named modules
102120
# workaround for File and SignatureValidation resources that ship in Windows
@@ -188,21 +206,26 @@ function Invoke-DscCacheRefresh {
188206
$lastWriteTimes.Add($_.FullName, $_.LastWriteTime)
189207
}
190208

191-
$dscResourceCache += [dscResourceCacheEntry]@{
209+
$dscResourceCacheEntries += [dscResourceCacheEntry]@{
192210
Type = "$moduleName/$($dscResource.Name)"
193211
DscResourceInfo = $DscResourceInfo
194212
LastWriteTimes = $lastWriteTimes
195213
}
196214
}
197215

216+
[dscResourceCache]$cache = [dscResourceCache]::new()
217+
$cache.ResourceCache = $dscResourceCacheEntries
218+
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -ea SilentlyContinue}
219+
$cache.PSModulePaths = $m.FullName
220+
198221
# save cache for future use
199222
# TODO: replace this with a high-performance serializer
200223
$trace = @{'Debug' = "Saving Get-DscResource cache to '$cacheFilePath'"} | ConvertTo-Json -Compress
201224
$host.ui.WriteErrorLine($trace)
202-
$dscResourceCache | ConvertTo-Json -Depth 90 | Out-File $cacheFilePath
225+
$cache | ConvertTo-Json -Depth 90 | Out-File $cacheFilePath
203226
}
204227

205-
return $dscResourceCache
228+
return $dscResourceCacheEntries
206229
}
207230

208231
# Convert the INPUT to a dscResourceObject object so configuration and resource are standardized as much as possible
@@ -455,6 +478,11 @@ class dscResourceCacheEntry {
455478
[PSCustomObject] $LastWriteTimes
456479
}
457480

481+
class dscResourceCache {
482+
[string[]] $PSModulePaths
483+
[dscResourceCacheEntry[]] $ResourceCache
484+
}
485+
458486
# format expected for configuration and resource output
459487
class dscResourceObject {
460488
[string] $name

0 commit comments

Comments
 (0)