@@ -50,9 +50,10 @@ function Invoke-DscCacheRefresh {
50
50
$trace = @ {' Debug' = " Reading from Get-DscResource cache file $cacheFilePath " } | ConvertTo-Json - Compress
51
51
$host.ui.WriteErrorLine ($trace )
52
52
53
- $dscResourceCache = Get-Content - Raw $cacheFilePath | ConvertFrom-Json
53
+ $cache = Get-Content - Raw $cacheFilePath | ConvertFrom-Json
54
+ $dscResourceCacheEntries = $cache.ResourceCache
54
55
55
- if ($dscResourceCache .Count -eq 0 ) {
56
+ if ($dscResourceCacheEntries .Count -eq 0 ) {
56
57
# if there is nothing in the cache file - refresh cache
57
58
$refreshCache = $true
58
59
@@ -64,7 +65,7 @@ function Invoke-DscCacheRefresh {
64
65
$trace = @ {' Debug' = " Checking cache for stale entries" } | ConvertTo-Json - Compress
65
66
$host.ui.WriteErrorLine ($trace )
66
67
67
- foreach ($cacheEntry in $dscResourceCache ) {
68
+ foreach ($cacheEntry in $dscResourceCacheEntries ) {
68
69
$trace = @ {' Trace' = " Checking cache entry '$ ( $cacheEntry.Type ) $ ( $cacheEntry.LastWriteTimes ) '" } | ConvertTo-Json - Compress
69
70
$host.ui.WriteErrorLine ($trace )
70
71
@@ -82,6 +83,23 @@ function Invoke-DscCacheRefresh {
82
83
83
84
if ($refreshCache ) {break }
84
85
}
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
+ }
85
103
}
86
104
}
87
105
else {
@@ -96,7 +114,7 @@ function Invoke-DscCacheRefresh {
96
114
$host.ui.WriteErrorLine ($trace )
97
115
98
116
# 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()
100
118
101
119
# improve by performance by having the option to only get details for named modules
102
120
# workaround for File and SignatureValidation resources that ship in Windows
@@ -188,21 +206,26 @@ function Invoke-DscCacheRefresh {
188
206
$lastWriteTimes.Add ($_.FullName , $_.LastWriteTime )
189
207
}
190
208
191
- $dscResourceCache += [dscResourceCacheEntry ]@ {
209
+ $dscResourceCacheEntries += [dscResourceCacheEntry ]@ {
192
210
Type = " $moduleName /$ ( $dscResource.Name ) "
193
211
DscResourceInfo = $DscResourceInfo
194
212
LastWriteTimes = $lastWriteTimes
195
213
}
196
214
}
197
215
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
+
198
221
# save cache for future use
199
222
# TODO: replace this with a high-performance serializer
200
223
$trace = @ {' Debug' = " Saving Get-DscResource cache to '$cacheFilePath '" } | ConvertTo-Json - Compress
201
224
$host.ui.WriteErrorLine ($trace )
202
- $dscResourceCache | ConvertTo-Json - Depth 90 | Out-File $cacheFilePath
225
+ $cache | ConvertTo-Json - Depth 90 | Out-File $cacheFilePath
203
226
}
204
227
205
- return $dscResourceCache
228
+ return $dscResourceCacheEntries
206
229
}
207
230
208
231
# Convert the INPUT to a dscResourceObject object so configuration and resource are standardized as much as possible
@@ -455,6 +478,11 @@ class dscResourceCacheEntry {
455
478
[PSCustomObject ] $LastWriteTimes
456
479
}
457
480
481
+ class dscResourceCache {
482
+ [string []] $PSModulePaths
483
+ [dscResourceCacheEntry []] $ResourceCache
484
+ }
485
+
458
486
# format expected for configuration and resource output
459
487
class dscResourceObject {
460
488
[string ] $name
0 commit comments