-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdate-Modules.ps1
More file actions
2720 lines (2717 loc) · 157 KB
/
Update-Modules.ps1
File metadata and controls
2720 lines (2717 loc) · 157 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Requires -Version 7.0
#Requires -modules ThreadJob
#Requires -modules Microsoft.PowerShell.ThreadJob
#region Check-PSResourceRepository
<#
Author: Harze2k
Date: 2026-02-25
Version: 4.0 (Parallel module installation!)
-Update-Modules now installs modules in parallel using ForEach-Object -Parallel.
-3-phase architecture: pre-processing (sequential) -> parallel install -> post-processing/cleaning (sequential).
-ShouldProcess checks evaluated on main thread before dispatching to parallel runspaces.
-Throttle limit scales with CPU cores: Min(moduleCount, Max(4, ProcessorCount * 2)).
-Progress reporting with ETA during parallel installation.
-Cleaning still runs sequentially to avoid filesystem conflicts.
---Older ---
Version 3.7 (2025-05-21):
-Fixed a loop issue that caused the Find-Module to slow down. (10x speed increase)
-Fixed PreRelease logic in several functions.
-Now we try to parse the PreRelease version also from .XML files.
-Fixed output from several functions to be relevant and less spammy.
-Fixed a typo that made the -MatchAutor not work.
-Added more Parallel Processing
-Added ThreadJobs
-Over 150% faster
-Finds basically all modules that can be found.
-Included help and guidelines.
-Progress reporting while parallel jobs.
#>
function Check-PSResourceRepository {
[CmdletBinding()]
param (
[switch]$ImportDependencies, # Force re-import of PSResourceGet module even if commands exist
[switch]$ForceInstall, # Force reinstall of PSResourceGet (PS5.1 only)
[int]$TimeoutSeconds = 30
)
$isPSCore = $PSVersionTable.PSVersion.Major -ge 6
$hasPSResourceGet = [bool](Get-Command -Name 'Get-PSResourceRepository' -ErrorAction SilentlyContinue)
New-Log "PowerShell version: $($PSVersionTable.PSVersion) | PSCore: $isPSCore | PSResourceGet available: $hasPSResourceGet"
function Invoke-WithTimeout {
[CmdletBinding()]
param (
[Parameter(Mandatory)][scriptblock]$ScriptBlock,
[int]$Timeout = 30,
[string]$OperationName = 'Operation'
)
$runspace = $null
$powershell = $null
try {
$runspace = [runspacefactory]::CreateRunspace()
$runspace.Open()
$powershell = [powershell]::Create()
$powershell.Runspace = $runspace
[void]$powershell.AddScript($ScriptBlock)
$handle = $powershell.BeginInvoke()
$completed = $handle.AsyncWaitHandle.WaitOne($Timeout * 1000)
if (-not $completed) {
New-Log "$OperationName timed out after $Timeout seconds." -Level WARNING
$powershell.Stop()
return $null
}
if ($powershell.HadErrors) {
$errorMsg = $powershell.Streams.Error | ForEach-Object { $_.ToString() } | Join-String -Separator '; '
New-Log "$OperationName had errors: $errorMsg" -Level WARNING
}
return $powershell.EndInvoke($handle)
}
catch {
New-Log "$OperationName failed: $($_.Exception.Message)" -Level ERROR
return $null
}
finally {
if ($powershell) { $powershell.Dispose() }
if ($runspace) { $runspace.Close(); $runspace.Dispose() }
}
}
function Set-TlsProtocol {
try {
$existingProtocols = [Net.ServicePointManager]::SecurityProtocol
$tls12Enum = [Net.SecurityProtocolType]::Tls12
if (-not ($existingProtocols -band $tls12Enum)) {
[Net.ServicePointManager]::SecurityProtocol = $existingProtocols -bor $tls12Enum
New-Log "TLS 1.2 security protocol enabled."
}
else {
New-Log "TLS 1.2 already enabled."
}
return $true
}
catch {
New-Log "Unable to set TLS 1.2: $($_.Exception.Message)" -Level ERROR
return $false
}
}
function Install-PSResourceGetForPS5 {
[CmdletBinding()]
param (
[int]$Timeout = 30,
[switch]$Force
)
New-Log "Attempting to install Microsoft.PowerShell.PSResourceGet for PS 5.1$(if ($Force) { ' (Force)' })..."
try {
$psGalleryScript = { Get-PSRepository -Name 'PSGallery' -ErrorAction SilentlyContinue }
$psGallery = Invoke-WithTimeout -ScriptBlock $psGalleryScript -Timeout $Timeout -OperationName "Get-PSRepository PSGallery"
if ($null -eq $psGallery) {
New-Log "Could not query PSGallery repository - may need manual registration." -Level WARNING
}
elseif (-not $psGallery.Trusted) {
New-Log "Setting PSGallery to Trusted..."
$setRepoScript = { Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted -ErrorAction Stop }
Invoke-WithTimeout -ScriptBlock $setRepoScript -Timeout $Timeout -OperationName "Set-PSRepository Trusted" | Out-Null
New-Log "PSGallery set to Trusted." -Level SUCCESS
}
else {
New-Log "PSGallery is already trusted."
}
}
catch {
New-Log "Error configuring PSGallery: $($_.Exception.Message)" -Level WARNING
}
$forceFlag = $Force.IsPresent
$installScript = [scriptblock]::Create(@"
`$ErrorActionPreference = 'Stop'
Install-Module -Name 'Microsoft.PowerShell.PSResourceGet' -Repository 'PSGallery' -Scope AllUsers -Force:$forceFlag -AllowClobber -AcceptLicense -SkipPublisherCheck -Confirm:`$false
"@)
New-Log "Installing Microsoft.PowerShell.PSResourceGet via Install-Module..."
Invoke-WithTimeout -ScriptBlock $installScript -Timeout ($Timeout * 2) -OperationName "Install-Module PSResourceGet" | Out-Null
try {
Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -Force -ErrorAction Stop
New-Log "Successfully imported Microsoft.PowerShell.PSResourceGet." -Level SUCCESS
return $true
}
catch {
New-Log "Failed to import Microsoft.PowerShell.PSResourceGet: $($_.Exception.Message)" -Level ERROR
return $false
}
}
function Import-PSResourceGetModule {
[CmdletBinding()]
param ([switch]$Force)
$action = if ($Force) { "Force importing" } else { "Importing" }
New-Log "$action Microsoft.PowerShell.PSResourceGet module..."
try {
Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -Force:$Force -ErrorAction Stop
New-Log "Successfully imported PSResourceGet." -Level SUCCESS
return $true
}
catch {
New-Log "Failed to import PSResourceGet: $($_.Exception.Message)" -Level ERROR
return $false
}
}
function Register-RepositoryPSResourceGet {
[CmdletBinding()]
param (
[Parameter(Mandatory)][string]$Name,
[string]$Uri,
[Parameter(Mandatory)][int]$Priority,
[string]$ApiVersion = 'v3',
[switch]$IsPSGallery
)
try {
$repository = Get-PSResourceRepository -Name $Name -ErrorAction SilentlyContinue
$needsUpdate = ($null -eq $repository) -or ($repository.Priority -ne $Priority) -or (-not $repository.Trusted)
if (-not $IsPSGallery -and $Uri -and $repository) {
$currentUri = if ($repository.Uri) { $repository.Uri.AbsoluteUri } else { $null }
$needsUpdate = $needsUpdate -or ($currentUri -ne $Uri)
}
if ($needsUpdate) {
if ($IsPSGallery) {
New-Log "Configuring PSGallery (Priority: $Priority, Trusted: True)."
Set-PSResourceRepository -Name $Name -Priority $Priority -Trusted -ErrorAction Stop
}
else {
New-Log "Registering repository '$Name' (Uri: $Uri, Priority: $Priority)."
$registerParams = @{
Name = $Name
Uri = $Uri
Priority = $Priority
Trusted = $true
Force = $true
PassThru = $false
ErrorAction = 'Stop'
}
if ($ApiVersion -eq 'v2') {
$registerParams.ApiVersion = 'v2'
New-Log "Using API Version V2 for '$Name'."
}
Register-PSResourceRepository @registerParams
}
New-Log "Successfully configured '$Name' repository." -Level SUCCESS
}
else {
New-Log "'$Name' repository already configured correctly."
}
return $true
}
catch {
New-Log "Failed to configure '$Name': $($_.Exception.Message)" -Level ERROR
return $false
}
}
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
New-Log "Administrator privileges required. Aborting." -Level WARNING
return $false
}
Set-TlsProtocol | Out-Null
$needsDependencyWork = (-not $hasPSResourceGet) -or $ImportDependencies.IsPresent
if ($needsDependencyWork) {
if ($ImportDependencies.IsPresent -and $hasPSResourceGet) {
New-Log "-ImportDependencies specified. Re-importing PSResourceGet module..."
}
if ($isPSCore) {
if (-not (Import-PSResourceGetModule -Force:$ImportDependencies.IsPresent)) {
New-Log "Could not import PSResourceGet in PS7." -Level ERROR
return $false
}
}
else {
$existingModule = Get-Module -Name 'Microsoft.PowerShell.PSResourceGet' -ListAvailable -ErrorAction SilentlyContinue
if ($ForceInstall.IsPresent -or -not $existingModule) {
if ($ForceInstall.IsPresent -and $existingModule) {
New-Log "-ForceInstall specified. Reinstalling PSResourceGet..."
}
if (-not (Install-PSResourceGetForPS5 -Timeout $TimeoutSeconds -Force:$ForceInstall.IsPresent)) {
New-Log "Could not install PSResourceGet. Cannot continue." -Level ERROR
return $false
}
}
else {
if (-not (Import-PSResourceGetModule -Force:$ImportDependencies.IsPresent)) {
New-Log "Could not import existing PSResourceGet module." -Level ERROR
return $false
}
}
}
$hasPSResourceGet = [bool](Get-Command -Name 'Get-PSResourceRepository' -ErrorAction SilentlyContinue)
}
if (-not $hasPSResourceGet) {
New-Log "PSResourceGet cmdlets still not available. Aborting." -Level ERROR
return $false
}
New-Log "PSResourceGet cmdlets are available. Configuring repositories..."
$repositories = @(
@{ Name = 'PSGallery'; Uri = $null; Priority = 30; IsPSGallery = $true }
@{ Name = 'NuGetGallery'; Uri = 'https://api.nuget.org/v3/index.json'; Priority = 40 }
@{ Name = 'NuGet'; Uri = 'https://www.nuget.org/api/v2'; Priority = 50; ApiVersion = 'v2' }
)
$overallSuccess = $true
foreach ($repo in $repositories) {
$splatParams = @{
Name = $repo.Name
Priority = $repo.Priority
IsPSGallery = [bool]$repo.IsPSGallery
}
if ($repo.Uri) { $splatParams.Uri = $repo.Uri }
if ($repo.ApiVersion) { $splatParams.ApiVersion = $repo.ApiVersion }
if (-not (Register-RepositoryPSResourceGet @splatParams)) {
$overallSuccess = $false
}
}
if ($overallSuccess) {
New-Log "All repositories configured successfully." -Level SUCCESS
}
else {
New-Log "Some repositories could not be configured." -Level WARNING
}
}
#endregion Check-PSResourceRepository
#region Get-ModuleInfo
function Get-ModuleInfo {
<#
.SYNOPSIS
Scans specified paths for PowerShell module manifest files (.psd1) and PSGetModuleInfo.xml files, processing them in parallel to gather module metadata.
.DESCRIPTION
This function discovers and processes PowerShell module files to create a comprehensive inventory. It operates in three main phases:
1. *Helper Function Preparation:** Collects definitions of internal helper functions (like `Parse-ModuleVersion`, `Get-ManifestVersionInfo`, etc.) as strings to make them available within parallel processing scopes.
2. *Phase 1: File Discovery:** Recursively scans the provided `-Paths` for all files ending in `.psd1` (module manifests) and files named `PSGetModuleInfo.xml` (often created by `Save-PSResource -IncludeXml`).
3. *Phase 2: Parallel File Processing:** Each discovered file is processed in parallel using `ForEach-Object -Parallel`.
Inside the parallel task, helper functions are restored using `Invoke-Expression`.
Likely resource files (e.g., localization files) are skipped using the `Test-IsResourceFile` helper.
For `.psd1` files:
`Test-ModuleManifest` is called to validate and get basic manifest data.
The output of `Test-ModuleManifest` is preferably processed by `Get-ManifestVersionInfo -Quick`.
If `Test-ModuleManifest` fails or its output is insufficient, `Get-ManifestVersionInfo -ModuleFilePath` is used as a fallback, attempting to infer details from the file path.
Extracted metadata (ModuleName, ModuleVersion, BasePath, Author, pre-release info) is added to a thread-safe `ConcurrentBag`.
For `PSGetModuleInfo.xml` files:
`Get-ModuleInfoFromXml` is called to parse the XML and extract metadata.
Extracted metadata is added to the `ConcurrentBag`.
4. *Phase 3: Aggregation and Normalization:**
The collected module entries from the `ConcurrentBag` are converted to an array.
Initial deduplication is performed by grouping entries by ModuleName, BasePath, and ModuleVersionString.
Further processing groups modules by name. For each name group:
Module base paths are normalized. This involves logic to identify the true root directory of a module, especially when version numbers are part of the directory structure (e.g., ensuring 'C:\Modules\MyModule\1.0' and 'C:\Modules\MyModule\1.1' both resolve 'C:\Modules\MyModule' as the base path).
Modules are then grouped by these normalized base paths.
Within each base path group, versions are grouped to select a single representative entry (preferring entries with `[System.Version]` objects over strings if both exist for the same version identifier).
The final, aggregated module data is stored in an ordered hashtable, where each key is a module name and the value is an array of `PSCustomObject`s, each representing a unique installation location and version of that module.
Modules specified in the `-IgnoredModules` parameter are filtered out from the final result.
The function returns this ordered hashtable, providing a structured inventory of all discovered modules.
.PARAMETER Paths
[Mandatory] An array of strings, where each string is a path to a directory to be scanned recursively for module files. Typically, this would be paths from `$env:PSModulePath`.
.PARAMETER IgnoredModules
An array of strings containing module names to be excluded from the final results. Defaults to an empty array.
.PARAMETER ThrottleLimit
The maximum number of parallel threads to use for processing files in Phase 2. Defaults to `([System.Environment]::ProcessorCount * 2)`.
.INPUTS
None. This function does not accept pipeline input.
.OUTPUTS
System.Collections.Specialized.OrderedDictionary
Returns an ordered hashtable where:
- Each key is a [string] representing a unique module name found.
- Each value is an array of [PSCustomObject]s. Each PSCustomObject represents a distinct installation of that module and contains properties like:
- ModuleName ([string])
- ModuleVersion ([System.Version])
- ModuleVersionString ([string])
- BasePath ([string]): The normalized root directory of this module installation.
- IsPreRelease ([bool])
- PreReleaseLabel ([string])
- Author ([string])
If no module files are found, an empty ordered hashtable is returned.
.EXAMPLE
PS C:\> $moduleInventory = Get-ModuleInfo -Paths ($env:PSModulePath -split ';') -IgnoredModules 'Pester','MyCustomDevModule'
Scans all standard module paths, processes found module files in parallel, and returns an inventory excluding 'Pester' and 'MyCustomDevModule'.
.NOTES
- Requires PowerShell 7.0 or later due to the use of `ForEach-Object -Parallel`.
- Relies on several internal helper functions (e.g., `Parse-ModuleVersion`, `Get-ManifestVersionInfo`, `Get-ModuleformPath`, `Test-IsResourceFile`, `Get-ModuleInfoFromXml`) which must be defined in the same scope.
- Depends on an external `New-Log` function for logging operations.
- The accuracy of `BasePath` normalization depends on common module directory structures.
.LINK
Test-ModuleManifest
Get-ChildItem
ForEach-Object -Parallel
Invoke-Expression
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)][string[]]$Paths,
[string[]]$IgnoredModules = @(),
[int]$ThrottleLimit = ([System.Environment]::ProcessorCount * 2) # Increased default, file processing is often quick
)
# --- Define Helper Functions as Strings to Pass to Parallel Scope ---
$helperFunctionDefinitions = @{
"New-Log" = ${function:New-Log}.ToString()
"Parse-ModuleVersion" = ${function:Parse-ModuleVersion}.ToString()
"Get-ManifestVersionInfo" = ${function:Get-ManifestVersionInfo}.ToString()
"Resolve-ModuleVersion" = ${function:Resolve-ModuleVersion}.ToString()
"Get-ModuleformPath" = ${function:Get-ModuleformPath}.ToString()
"Get-ModuleInfoFromXml" = ${function:Get-ModuleInfoFromXml}.ToString()
"Test-IsResourceFile" = ${function:Test-IsResourceFile}.ToString()
}
foreach ($funcName in $helperFunctionDefinitions.Keys) {
if ([string]::IsNullOrWhiteSpace($helperFunctionDefinitions[$funcName])) {
Write-Error "Helper function '$funcName' could not be found. It must be loaded."
return
}
}
New-Log "Phase 1: Gathering all .psd1 and PSGetModuleInfo.xml file paths..."
$fileDiscoveryStartTime = Get-Date
$allPotentialFiles = [System.Collections.Generic.List[System.IO.FileInfo]]::new()
foreach ($dir in $paths) {
try {
$psd1Files = @(Get-ChildItem -Path $dir -Recurse -Filter "*.psd1" -ErrorAction SilentlyContinue)
$xmlFiles = @(Get-ChildItem -Path $dir -Recurse -Filter "PSGetModuleInfo.xml" -ErrorAction SilentlyContinue)
foreach ($file in $psd1Files) { $allPotentialFiles.Add($file) }
foreach ($file in $xmlFiles) { $allPotentialFiles.Add($file) }
}
catch {
Write-Host "Error processing directory $($dir.FullName): $_" -ForegroundColor Red
}
}
$fileDiscoveryDuration = (Get-Date) - $fileDiscoveryStartTime
New-Log "Phase 1 complete. Found $($allPotentialFiles.Count) potential module files in $($fileDiscoveryDuration.ToString("g"))."
if ($allPotentialFiles.Count -eq 0) {
New-Log "No potential module files found to process." -Level WARNING
return [ordered]@{}
}
# --- PHASE 2: Process individual files in parallel ---
New-Log "Phase 2: Starting parallel processing of $($allPotentialFiles.Count) files (Throttle: $ThrottleLimit)..."
$parallelProcessingStartTime = Get-Date
$allFoundModulesFromParallel = [System.Collections.Concurrent.ConcurrentBag[object]]::new()
$sortedModules = [ordered]@{}
$sortedModules = $allPotentialFiles | ForEach-Object -ThrottleLimit $ThrottleLimit -Parallel {
# --- BEGIN PARALLEL SCRIPT BLOCK ---
$fileInfo = $_ # Current System.IO.FileInfo object
$filePath = $fileInfo.FullName
$fileExtension = $fileInfo.Extension # .psd1 or .xml
Import-Module -Name 'Microsoft.PowerShell.PSResourceGet' -Global -Force
# $currentKernelTime = (Get-Process -Id $pid).KernelTime # Example, can be removed if not used
$VerbosePreference = $using:VerbosePreference
$allFoundModulesFromParallel = $using:allFoundModulesFromParallel
# --- Restore Helper Functions ---
# Bring the whole hashtable into the parallel scope
$localHelperFunctionDefinitions = $using:helperFunctionDefinitions
foreach ($funcName in $localHelperFunctionDefinitions.Keys) {
# Ensure the definition string is not null or empty before invoking
$funcDefinition = $localHelperFunctionDefinitions[$funcName]
if (-not [string]::IsNullOrWhiteSpace($funcDefinition)) {
Invoke-Expression -Command "function global:$funcName { $funcDefinition }"
}
}
if (Test-IsResourceFile -Path $filePath) {
New-Log "Skipping likely resource file in parallel: $filePath" -Level VERBOSE
return
}
if ($fileExtension -eq '.psd1') {
$manifestInfoObj = $null
$testManifestOutput = $null
try {
$testManifestOutput = Test-ModuleManifest -Path $filePath -ErrorAction Stop -WarningAction SilentlyContinue -Verbose:$false
}
catch {
$testManifestOutput = $null
New-Log "Test-ModuleManifest crashed while using path $filePath" -Level VERBOSE
}
if ($testManifestOutput) {
try {
$manifestInfoObj = Get-ManifestVersionInfo -ResData $testManifestOutput -Quick -ErrorAction Stop -WarningAction SilentlyContinue
}
catch {
$manifestInfoObj = $null
New-Log "Get-ManifestVersionInfo crashed." -Level VERBOSE
}
}
if (-not $manifestInfoObj) {
try {
$manifestInfoObj = Get-ManifestVersionInfo -ModuleFilePath $filePath -ErrorAction Stop -WarningAction SilentlyContinue
}
catch {
$manifestInfoObj = $null
New-Log "Get-ManifestVersionInfo while using path $filePath" -Level VERBOSE
}
}
$manifestInfosToProcess = @()
if ($manifestInfoObj) {
if ($manifestInfoObj -is [array] -or $manifestInfoObj -is [System.Collections.IList]) {
$manifestInfosToProcess = $manifestInfoObj
}
else {
$manifestInfosToProcess = @($manifestInfoObj)
}
}
foreach ($mInfo in $manifestInfosToProcess) {
if ($mInfo -and $mInfo.ModuleVersion -and $mInfo.ModuleName) {
New-Log "[$($mInfo.ModuleName)] Successfully found module info from the [.PSD1] file. Version [$($mInfo.ModuleVersionString)]" -Level SUCCESS
$allFoundModulesFromParallel.Add([PSCustomObject]@{
ModuleName = $mInfo.ModuleName
ModuleVersion = $mInfo.ModuleVersion
ModuleVersionString = $mInfo.ModuleVersionString
BasePath = $mInfo.BasePath
isPreRelease = $mInfo.isPreRelease
PreReleaseLabel = $mInfo.PreReleaseLabel
Author = $mInfo.Author
})
}
else {
New-Log "psd1 info from '$mInfo' missing required parameters (ModuleName/ModuleVersion)." -Level VERBOSE
}
}
}
elseif ($fileExtension -eq '.xml') {
$xmlInfo = Get-ModuleInfoFromXml -XmlFilePath $filePath -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if ($xmlInfo -and $xmlInfo.ModuleName -and $xmlInfo.ModuleVersion) {
New-Log "[$($xmlInfo.ModuleName)] Successfully found module info from the [.XML] file. Version [$($xmlInfo.ModuleVersionString)]" -Level SUCCESS
$allFoundModulesFromParallel.Add([PSCustomObject]@{
ModuleName = $xmlInfo.ModuleName
ModuleVersion = $xmlInfo.ModuleVersion
ModuleVersionString = $xmlInfo.ModuleVersionString
BasePath = $xmlInfo.BasePath
isPreRelease = $xmlInfo.isPreRelease
PreReleaseLabel = $xmlInfo.PreReleaseLabel
Author = $xmlInfo.Author
})
}
else {
New-Log "xml info from '$xmlInfo' missing required parameters (ModuleName/ModuleVersion)." -Level VERBOSE
}
}
# --- END PARALLEL SCRIPT BLOCK ---
} # End ForEach-Object -Parallel
$parallelProcessingDuration = (Get-Date) - $parallelProcessingStartTime
New-Log "Phase 2 complete. Parallel processing took $($parallelProcessingDuration.ToString("g")). Collected $($allFoundModulesFromParallel.Count) raw entries."
# --- PHASE 3: Post-processing and Aggregation ---
New-Log "Phase 3: Starting post-processing and aggregation..."
$aggregationStartTime = Get-Date
$allFoundModulesArray = $allFoundModulesFromParallel.ToArray()
if ($allFoundModulesArray.Count -eq 0) {
New-Log "No valid module data collected after parallel processing." -Level WARNING
return [ordered]@{}
}
# Deduplicate, Normalize, Group (This part remains the same as your latest version)
$uniqueModules = $allFoundModulesArray | Group-Object -Property ModuleName, BasePath, ModuleVersionString | ForEach-Object { $_.Group[0] }
New-Log "Reduced to $($uniqueModules.Count) unique entries after initial grouping."
$resultModules = [ordered]@{}
$modulesGroupedByName = $uniqueModules | Where-Object { $null -ne $_.ModuleName -and $_.ModuleName -notmatch '^\d+(\.\d+)+$' } | Group-Object ModuleName
foreach ($nameGroup in $modulesGroupedByName) {
$moduleName = $nameGroup.Name
# New-Log "Post-processing ModuleName: $moduleName" -Level VERBOSE
$groupWithNormalizedPaths = $nameGroup.Group | Where-Object { $null -ne $_ } | ForEach-Object {
$newObject = $_ | Select-Object *
if ($null -ne $newObject.BasePath -and -not ([string]::IsNullOrWhiteSpace($moduleName))) {
$currentBasePath = $newObject.BasePath
$normalizedCurrentPath = $currentBasePath.TrimEnd('\', '/') -replace '/', '\'
$expectedEnding = "\$moduleName"
if (-not $normalizedCurrentPath.EndsWith($expectedEnding, [System.StringComparison]::OrdinalIgnoreCase)) {
$leafName = Split-Path $normalizedCurrentPath -Leaf
$parentOfCurrent = Split-Path $normalizedCurrentPath -Parent -ErrorAction SilentlyContinue
$parentLeafName = if ($parentOfCurrent) { Split-Path $parentOfCurrent -Leaf -ErrorAction SilentlyContinue } else { $null }
if ($parentLeafName -eq $moduleName -and $leafName -match '^\d+(\.\d+){1,3}(-.+)?$') {
$newObject.BasePath = $parentOfCurrent
}
elseif ($leafName -eq 'Modules' -or $leafName -eq 'Documents') {
$newObject.BasePath = Join-Path -Path $normalizedCurrentPath -ChildPath $moduleName -ErrorAction SilentlyContinue
}
else {
$newObject.BasePath = $normalizedCurrentPath
}
}
else {
$newObject.BasePath = $normalizedCurrentPath
}
}
$newObject
}
$modulesGroupedByBasePath = $groupWithNormalizedPaths | Where-Object { $null -ne $_.BasePath } | Group-Object -Property BasePath
$finalModuleLocations = [System.Collections.Generic.List[object]]::new()
foreach ($basePathGroup in $modulesGroupedByBasePath) {
$currentBasePath = $basePathGroup.Name
$versionsInPathGroup = $basePathGroup.Group | Group-Object -Property @{ Expression = { if ($_.ModuleVersion -is [version]) { $_.ModuleVersion } else { $_.ModuleVersionString } } }
foreach ($versionGroup in $versionsInPathGroup) {
$representativeEntry = $versionGroup.Group | Sort-Object -Property @{Expression = { $_.ModuleVersion -is [version] }; Descending = $true }, ModuleVersionString | Select-Object -First 1
if ($representativeEntry) {
$outputObject = [PSCustomObject]@{
ModuleName = $moduleName
ModuleVersion = $representativeEntry.ModuleVersion
ModuleVersionString = $representativeEntry.ModuleVersionString
BasePath = $currentBasePath
IsPreRelease = $representativeEntry.IsPreRelease
PreReleaseLabel = $representativeEntry.PreReleaseLabel
Author = $representativeEntry.Author
}
$finalModuleLocations.Add($outputObject)
}
}
}
if ($finalModuleLocations.Count -gt 0) {
$sortedLocations = $finalModuleLocations | Sort-Object BasePath, @{Expression = { $_.ModuleVersion }; Ascending = $true }
$resultModules[$moduleName] = $sortedLocations
}
}
$finalSortedModules = [ordered]@{}
foreach ($key in ($resultModules.Keys | Sort-Object)) {
if ($IgnoredModules -notcontains $key) {
# $using: not needed, main thread
$finalSortedModules[$key] = $resultModules[$key]
}
else {
New-Log "Skipping module '$key' as it is in the IgnoredModules list (final filter)." -Level VERBOSE
}
}
$aggregationDuration = (Get-Date) - $aggregationStartTime
New-Log "Phase 3 (Aggregation) complete in $($aggregationDuration.ToString("g"))."
$totalFunctionDuration = (Get-Date) - $fileDiscoveryStartTime # Start from very beginning of Phase 1
New-Log "Get-ModuleInfo completed. Total duration: $($totalFunctionDuration.ToString("g")). Found $($finalSortedModules.Keys.Count) modules." -Level SUCCESS
return $finalSortedModules
}
#endregion Get-ModuleInfo
#region Get-ModuleUpdateStatus
function Get-ModuleUpdateStatus {
<#
.SYNOPSIS
Checks online repositories for updates to locally installed modules based on provided inventory data, using parallel processing for efficiency.
.DESCRIPTION
This function determines if updates are available for modules listed in a local inventory. It operates in two main stages:
1. *Stage 1: Online Version Pre-fetching:**
For each unique module name derived from the input `-ModuleInventory`:
a. Applies blacklist rules: If a module is blacklisted for all repositories (entry like `ModuleName = '*'`) or for all specified repositories, it's skipped.
b. For non-blacklisted modules, it launches a parallel thread job (`Start-ThreadJob`) using a script block that calls `Find-Module` against the specified `-Repositories`. This is done twice for each module: once to find the latest stable version and once (with `-AllowPrerelease`) to find the latest pre-release version.
c. The results (latest stable and latest pre-release found online, or any errors) are collected from these jobs and stored in a thread-safe cache. This stage includes timeout handling for each `Find-Module` job.
2. *Stage 2: Parallel Comparison:**
For each module from the prepared local inventory:
a. It retrieves the pre-fetched online version data (stable and pre-release) from the cache.
b. It determines the single "latest" available online version by comparing the pre-fetched stable and pre-release versions using the `Compare-ModuleVersion` helper function (which generally prefers a pre-release if its base version is the same as or newer than the stable version).
c. This "latest" online version is then compared against the highest locally installed version of the module (also determined using `Compare-ModuleVersion`).
d. If an update is indicated (`LatestOnlineVersion` > `HighestLocalVersion`) and the `-MatchAuthor` switch is specified, it further checks if the author of the online module matches the author of the local module. An update is only reported if authors match or if `-MatchAuthor` is not used.
e. If an update is confirmed, it identifies which specific local installation paths of the module do not yet have this latest online version.
f. Information about modules needing updates is collected into a thread-safe bag.
Progress is logged throughout the process. The function returns an array of PSCustomObjects, each detailing a module for which an update is available.
.PARAMETER ModuleInventory
[Mandatory] A hashtable, typically the output of `Get-ModuleInfo`.
The keys are module names [string].
Each value is an array of PSCustomObjects, where each object represents an installed instance of that module and must contain at least:
- ModuleVersion ([System.Version] or a string parsable to a version)
- ModuleVersionString ([string])
- BasePath ([string])
- IsPrerelease ([bool], optional)
- PreReleaseLabel ([string], optional)
- Author ([string], optional)
.PARAMETER Repositories
An array of strings specifying the names of the registered PSResource repositories to check for updates. Defaults to `@('PSGallery', 'NuGet')`. Ensure these repositories are registered and accessible.
.PARAMETER ThrottleLimit
The maximum number of parallel jobs to run simultaneously. This applies to both the pre-fetching stage (`Start-ThreadJob`'s internal throttle) and the comparison stage (`ForEach-Object -Parallel`). Defaults to `([System.Environment]::ProcessorCount * 2)`.
.PARAMETER TimeoutSeconds
The maximum time in seconds that each individual `Find-Module` job in the pre-fetching stage is allowed to run before being timed out. Defaults to 30 seconds.
.PARAMETER FindModuleTimeoutSeconds
The maximum time in seconds that each `Find-Module` command within a job is allowed to run before being timed out. Defaults to 10 seconds.
.PARAMETER BlackList
A hashtable used to exclude specific modules from update checks or to exclude them from being checked against certain repositories.
- To completely exclude a module: `@{ 'ModuleName' = '*' }`
- To exclude a module from specific repositories: `@{ 'ModuleName' = @('Repo1', 'Repo2') }` or `@{ 'ModuleName' = 'Repo1' }`
Defaults to an empty hashtable (no blacklisting).
.PARAMETER MatchAuthor
If specified, an update for a module will only be reported if the author of the latest online version matches the author of the locally installed version. Author matching is case-insensitive and ignores non-alphanumeric characters.
.INPUTS
None. This function does not accept direct pipeline input for its main parameters but relies on the `-ModuleInventory` parameter.
.OUTPUTS
System.Management.Automation.PSCustomObject[]
An array of PSCustomObjects, where each object represents a module that has an available update. Each object includes:
- ModuleName ([string]): The name of the module.
- Repository ([string]): The name of the repository where the latest version was found.
- IsPreview ([bool]): True if the latest available online version is a pre-release.
- PreReleaseVersion ([string]): The pre-release tag of the latest online version (e.g., "beta1"), if applicable.
- HighestLocalVersion ([System.Version]): The [System.Version] object of the highest version currently installed locally.
- LatestVersion ([System.Version]): The [System.Version] object of the latest version available online.
- LatestVersionString ([string]): The full string representation of the latest online version (e.g., "2.1.0" or "2.1.0-beta1").
- OutdatedModules ([PSCustomObject[]]): An array of objects, each detailing a local installation path that is outdated. Each sub-object has:
- Path ([string]): The base path of the outdated local installation.
- InstalledVersion ([string]): The version string of the outdated local installation at that path.
- Author ([string]): Author of the local module.
- GalleryAuthor ([string]): Author of the online module.
Returns an empty array if no updates are found or if the input inventory is empty.
.EXAMPLE
PS C:\> $inventory = Get-ModuleInfo -Paths $env:PSModulePath
PS C:\> $updates = Get-ModuleUpdateStatus -ModuleInventory $inventory -Repositories 'PSGallery' -MatchAuthor -BlackList @{ 'SomeModule' = '*' }
Checks PSGallery for updates to modules in `$inventory`, requiring author match, and skipping 'SomeModule'.
.NOTES
- Requires PowerShell 7.0 or later due to the use of `Start-ThreadJob` and `ForEach-Object -Parallel`.
- Relies on `Find-Module` cmdlet (from `PowerShellGet` or `Microsoft.PowerShell.PSResourceGet` module). Ensure one of these is installed and functional.
- Depends on internal helper functions `Compare-ModuleVersion` and an external `New-Log` function.
- Network connectivity to the specified repositories is required for the online version pre-fetching stage.
- The accuracy of update detection depends on the quality of the input `$ModuleInventory`.
.LINK
Get-ModuleInfo
Find-Module
Start-ThreadJob
ForEach-Object -Parallel
Compare-ModuleVersion
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)][hashtable]$ModuleInventory,
[string[]]$Repositories = @('PSGallery', 'NuGet'),
[int]$ThrottleLimit = ([Environment]::ProcessorCount * 2),
[ValidateRange(1, 3600)][int]$TimeoutSeconds = 30, # Job timeout
[ValidateRange(1, 60)][int]$FindModuleTimeoutSeconds = 10, # Internal timeout for Find-Module
[hashtable]$BlackList = @{},
[switch]$MatchAuthor
)
# Quick environment check
if ($PSVersionTable.PSVersion.Major -lt 7) {
New-Log "This function requires PowerShell 7 or later. Current version: $($PSVersionTable.PSVersion)" -Level ERROR
return
}
$allModuleNames = $ModuleInventory.Keys | Where-Object { $_ -and $_.Trim() } | Sort-Object -Unique
if ($allModuleNames.Count -eq 0) {
New-Log "Module inventory is empty. Nothing to check."
return @()
}
# --- Prepare Local Module Data ---
$moduleDataArray = @()
foreach ($moduleNameInLoop in $allModuleNames) {
# Renamed to avoid conflict with $moduleName later
$localModulesInput = $ModuleInventory[$moduleNameInLoop]
if ($localModulesInput -is [PSCustomObject]) {
$localModulesInput = @($localModulesInput)
}
$parsedVersions = $localModulesInput | Where-Object { $_ -and ($_.PSObject.Properties.Name -contains 'ModuleVersion' -or $_.PSObject.Properties.Name -contains 'ModuleVersionString') -and $_.PSObject.Properties.Name -contains 'BasePath' } | ForEach-Object {
[PSCustomObject]@{
ModuleVersion = $_.ModuleVersion
ModuleVersionString = $_.ModuleVersionString
PreReleaseLabel = $_.PreReleaseLabel
BasePath = $_.BasePath
IsPreRelease = $_.IsPrerelease
Author = $_.Author
}
}
if ($parsedVersions.Count -gt 0) {
$highestLocalVersionInstall = $parsedVersions | Sort-Object -Property @{E = { $_.ModuleVersion }; Descending = $true }, @{E = { $_.IsPreRelease }; Ascending = $true } | Select-Object -First 1
$moduleDataArray += [PSCustomObject]@{
ModuleName = $moduleNameInLoop
HighestLocalInstall = $highestLocalVersionInstall
AllParsedVersions = $parsedVersions
}
}
}
$validModuleCountForProcessing = $moduleDataArray.Count
if ($validModuleCountForProcessing -eq 0) {
New-Log "No valid modules remaining after pre-processing local inventory." -Level WARNING
return @()
}
New-Log "Prepared $validModuleCountForProcessing modules from local inventory." -Level VERBOSE
# --- STAGE 1: Pre-fetch Online Module Versions (Optimized) ---
New-Log "Starting online version pre-fetching for up to $($moduleDataArray.Count) modules..."
$overallOperationStartTime = Get-Date
$onlineModuleVersionsCache = [System.Collections.Concurrent.ConcurrentDictionary[string, object]]::new() # Thread-safe for direct assignment
# Script block to find module versions - search repositories one at a time
$findModuleScriptBlock = {
param(
$moduleNameToFetch,
$repositoriesForJob,
$findModuleTimeoutSeconds
)
# Ensure PSResourceGet cmdlets are available in the thread
# Import-Module Microsoft.PowerShell.PSResourceGet -ErrorAction SilentlyContinue -Force
# Import-Module PowerShellGet -ErrorAction SilentlyContinue -Force # For older systems if needed
$ErrorActionPreference = 'SilentlyContinue' # Let Find-Module try its best for each repo
$stableResult = $null
$prereleaseResult = $null
$fetchError = $null
# Check each repository one at a time to stop once we find a match
foreach ($repo in $repositoriesForJob) {
try {
$stableModuleFromRepo = Find-Module -Name $moduleNameToFetch -Repository $repo -ErrorAction SilentlyContinue -Verbose:$false
if ($stableModuleFromRepo) {
# Found in this repo, take the highest version
$stableResult = $stableModuleFromRepo | Sort-Object -Property Version -Descending | Select-Object -First 1
break # Stop checking other repos
}
}
catch {
$fetchError = "Error finding stable for $moduleNameToFetch in $repo : $($_.Exception.Message)"
# Continue to next repo
}
}
# If we didn't find a stable version, $stableResult will remain $null
# Now check for prerelease versions (only if we need to)
if (-not $stableResult) {
foreach ($repo in $repositoriesForJob) {
try {
$prereleaseModuleFromRepo = Find-Module -Name $moduleNameToFetch -AllowPrerelease -Repository $repo -ErrorAction SilentlyContinue -Verbose:$false |
Where-Object { ($_.PSObject.Properties['IsPrerelease'] -and $_.PSObject.Properties['IsPrerelease'].Value) -or ($_.Version.ToString() -match '-') }
if ($prereleaseModuleFromRepo) {
# Found in this repo, take the highest version
$prereleaseResult = $prereleaseModuleFromRepo | Sort-Object -Property Version -Descending | Select-Object -First 1
break # Stop checking other repos
}
}
catch {
$prereleaseError = "Error finding prerelease for $moduleNameToFetch in $repo : $($_.Exception.Message)"
$fetchError = ($fetchError + "; " + $prereleaseError).TrimStart('; ')
# Continue to next repo
}
}
}
# Even if we find a stable version, we might want to check for a newer prerelease version in the same repo
if ($stableResult) {
try {
$stableRepo = $stableResult.Repository
$prereleaseModuleFromSameRepo = Find-Module -Name $moduleNameToFetch -AllowPrerelease -Repository $stableRepo -ErrorAction SilentlyContinue -Verbose:$false |
Where-Object { ($_.PSObject.Properties['IsPrerelease'] -and $_.PSObject.Properties['IsPrerelease'].Value) -or ($_.Version.ToString() -match '-') }
if ($prereleaseModuleFromSameRepo) {
$prereleaseResult = $prereleaseModuleFromSameRepo | Sort-Object -Property Version -Descending | Select-Object -First 1
}
}
catch {
# Non-critical, just log it
$fetchError = ($fetchError + "; Error checking for prerelease in same repo as stable for $moduleNameToFetch").TrimStart('; ')
}
}
[pscustomobject]@{
ModuleName = $moduleNameToFetch
Stable = $stableResult
PreRelease = $prereleaseResult
ErrorFetching = $fetchError # Store any caught error messages
Skipped = $false
}
}
# Start jobs for each module
$preFetchJobs = @()
foreach ($moduleEntry in $moduleDataArray) {
$moduleNameToFetch = $moduleEntry.ModuleName
$currentRepositories = $Repositories # Start with all specified function repos
# Apply blacklist logic for repositories for this module
if ($BlackList -and $BlackList.ContainsKey($moduleNameToFetch)) {
$blacklistedReposSetting = $BlackList[$moduleNameToFetch]
if ($blacklistedReposSetting -eq '*') {
New-Log "[$moduleNameToFetch] Pre-fetch: Blacklisted ('*'). Skipping online check." -Level DEBUG
$onlineModuleVersionsCache[$moduleNameToFetch] = [pscustomobject]@{
ModuleName = $moduleNameToFetch
Stable = $null
PreRelease = $null
ErrorFetching = $null
Skipped = $true
}
continue
}
if ($blacklistedReposSetting -is [array]) {
$currentRepositories = $Repositories | Where-Object { $blacklistedReposSetting -notcontains $_ }
}
elseif ($blacklistedReposSetting -is [string]) {
$currentRepositories = $Repositories | Where-Object { $_ -ne $blacklistedReposSetting }
}
if ($currentRepositories.Count -eq 0) {
New-Log "[$moduleNameToFetch] Pre-fetch: Blacklisted due to repository exclusion for all specified repos. Skipping." -Level DEBUG
$onlineModuleVersionsCache[$moduleNameToFetch] = [pscustomobject]@{
ModuleName = $moduleNameToFetch
Stable = $null
PreRelease = $null
ErrorFetching = $null
Skipped = $true
}
continue
}
}
if ($currentRepositories.Count -eq 0) {
# If $Repositories was empty to begin with
New-Log "[$moduleNameToFetch] Pre-fetch: No repositories specified to check. Skipping." -Level DEBUG
$onlineModuleVersionsCache[$moduleNameToFetch] = [pscustomobject]@{
ModuleName = $moduleNameToFetch
Stable = $null
PreRelease = $null
ErrorFetching = $null
Skipped = $true
}
continue
}
$job = Start-ThreadJob -ScriptBlock $findModuleScriptBlock -ThrottleLimit $ThrottleLimit -ArgumentList @($moduleNameToFetch, $currentRepositories, $FindModuleTimeoutSeconds)
$job.PSObject.Properties.Add([psnoteproperty]::new("ModuleNameForJob", $moduleNameToFetch)) # Tag job with module name
$preFetchJobs += $job
}
# Single log statement for waiting
New-Log "Waiting for $($preFetchJobs.Count) pre-fetch jobs to complete (Timeout per job: ${TimeoutSeconds}s)..."
# Initialize counters for job monitoring
$totalJobs = $preFetchJobs.Count
$prefetchSync = [System.Collections.Hashtable]::Synchronized(@{
timeouts = 0
completed = 0
total = $totalJobs
})
# For tracking job completion progress
$lastCompletedCount = 0
$completionThreshold = [Math]::Max(1, [Math]::Ceiling($totalJobs / 10)) # Show progress every ~10% completion
$lastProgressUpdate = Get-Date
$progressInterval = 5 # seconds
# Much simpler approach - check every second, report progress on thresholds
$waitStart = Get-Date
while ($true) {
# Sleep briefly to avoid high CPU usage
Start-Sleep -Milliseconds 250
# Get current job states
$runningJobs = @($preFetchJobs | Where-Object State -EQ 'Running')
$completedJobs = @($preFetchJobs | Where-Object State -EQ 'Completed')
$runningCount = $runningJobs.Count
$completedCount = $completedJobs.Count
# Check if we should update progress
$timeToUpdate = ((Get-Date) - $lastProgressUpdate).TotalSeconds -ge $progressInterval
$completionCountChanged = ($completedCount - $lastCompletedCount) -ge $completionThreshold
if ($timeToUpdate -or $completionCountChanged -or ($lastCompletedCount -eq 0 -and $completedCount -gt 0)) {
$percentComplete = [math]::Round(($completedCount / $totalJobs) * 100, 1)
New-Log "Pre-fetch progress: $completedCount/$totalJobs completed ($percentComplete%), $runningCount still running" -Level INFO
$lastProgressUpdate = Get-Date
$lastCompletedCount = $completedCount
}
# Check for timed-out jobs
$longRunningJobs = @($runningJobs | Where-Object { ((Get-Date) - $_.PSBeginTime).TotalSeconds -gt $TimeoutSeconds })
if ($longRunningJobs.Count -gt 0) {
# Process timed out jobs
New-Log "Stopping $($longRunningJobs.Count) jobs that exceeded timeout of ${TimeoutSeconds}s" -Level WARNING
$prefetchSync.timeouts += $longRunningJobs.Count
foreach ($timeoutJob in $longRunningJobs) {
$jobModuleName = $timeoutJob.PSObject.Properties["ModuleNameForJob"].Value
try {
$partialResults = $timeoutJob | Receive-Job -ErrorAction SilentlyContinue
if ($partialResults -and $partialResults.ModuleName) {
$onlineModuleVersionsCache[$partialResults.ModuleName] = $partialResults
}
else {
$onlineModuleVersionsCache[$jobModuleName] = [pscustomobject]@{
ModuleName = $jobModuleName; Stable = $null; PreRelease = $null
ErrorFetching = "Pre-fetch job timed out after ${TimeoutSeconds}s"; Skipped = $false
}
}
}
catch {
$onlineModuleVersionsCache[$jobModuleName] = [pscustomobject]@{
ModuleName = $jobModuleName; Stable = $null; PreRelease = $null
ErrorFetching = "Pre-fetch job timed out after ${TimeoutSeconds}s"; Skipped = $false
}
}
$timeoutJob | Stop-Job -ErrorAction SilentlyContinue
}
}
# Check for aggressive termination criteria
$elapsedSeconds = ((Get-Date) - $waitStart).TotalSeconds
$percentCompleted = $completedCount / $totalJobs
if ($elapsedSeconds -gt 30 -and $percentCompleted -gt 0.90 -and $runningCount -gt 0 -and $runningCount -lt 10) {
New-Log "Aggressively terminating remaining $runningCount jobs after $([int]$elapsedSeconds)s as they're taking too long" -Level WARNING
foreach ($slowJob in $runningJobs) {
$jobModuleName = $slowJob.PSObject.Properties["ModuleNameForJob"].Value
try {
$partialResults = $slowJob | Receive-Job -ErrorAction SilentlyContinue
if ($partialResults -and $partialResults.ModuleName) {
$onlineModuleVersionsCache[$partialResults.ModuleName] = $partialResults
}
else {
$onlineModuleVersionsCache[$jobModuleName] = [pscustomobject]@{
ModuleName = $jobModuleName; Stable = $null; PreRelease = $null
ErrorFetching = "Job terminated for taking too long"; Skipped = $false
}
}
}
catch {
$onlineModuleVersionsCache[$jobModuleName] = [pscustomobject]@{
ModuleName = $jobModuleName; Stable = $null; PreRelease = $null
ErrorFetching = "Job terminated for taking too long"; Skipped = $false
}
}
$slowJob | Stop-Job -ErrorAction SilentlyContinue
}
}
# EXIT CONDITIONS - in order of importance
# 1. All jobs have finished - clean exit
if ($runningCount -eq 0) {
# Final progress report if needed
if (((Get-Date) - $lastProgressUpdate).TotalSeconds -gt 0.5) {
$percentComplete = [math]::Round(($completedCount / $totalJobs) * 100, 1)
New-Log "Pre-fetch progress: $completedCount/$totalJobs completed ($percentComplete%), 0 still running" -Level INFO
}
break
}
# 2. Hard timeout - if we've waited too long, just exit regardless
if ($elapsedSeconds -gt 45) {
New-Log "Hard timeout after 45 seconds - exiting job monitoring loop" -Level WARNING
# Stop any remaining running jobs
foreach ($job in $runningJobs) {
$job | Stop-Job -ErrorAction SilentlyContinue
}
break
}
}
# Receive results from all completed jobs (not already processed)
$remainingCompletedJobs = @($preFetchJobs | Where-Object { $_.State -eq 'Completed' })
if ($remainingCompletedJobs.Count -gt 0) {
New-Log "Processing $($remainingCompletedJobs.Count) remaining completed jobs" -Level VERBOSE
foreach ($job in $remainingCompletedJobs) {
$jobModuleName = $job.PSObject.Properties["ModuleNameForJob"].Value
try {
$jobOutput = $job | Receive-Job -ErrorAction SilentlyContinue -Wait
if ($jobOutput -and $jobOutput.ModuleName) {
$onlineModuleVersionsCache[$jobOutput.ModuleName] = $jobOutput
$prefetchSync.completed++
}
}
catch {
# Already logged elsewhere
}
}
}
# Clean up all jobs
$preFetchJobs | Remove-Job -Force -ErrorAction SilentlyContinue
New-Log "Pre-fetch complete: $($prefetchSync.completed)/$($prefetchSync.total) processed, $($prefetchSync.timeouts) timeouts" -Level INFO
New-Log "Online version pre-fetching complete. Cached data for $($onlineModuleVersionsCache.Count) modules. Timeouts: $($prefetchSync.timeouts)"
$preFetchDuration = (Get-Date) - $overallOperationStartTime
New-Log "Pre-fetching (Stage 1) took: $($preFetchDuration.ToString("g"))"
# Ensure all modules in $moduleDataArray have an entry in $onlineModuleVersionsCache
foreach ($moduleEntry in $moduleDataArray) {
if (-not $onlineModuleVersionsCache.ContainsKey($moduleEntry.ModuleName)) {
New-Log "[$($moduleEntry.ModuleName)] No pre-fetched data found post-job processing. Marking as error/skipped." -Level WARNING
$onlineModuleVersionsCache[$moduleEntry.ModuleName] = [pscustomobject]@{
ModuleName = $moduleEntry.ModuleName
Stable = $null
PreRelease = $null
ErrorFetching = "Data not found in pre-fetch cache."
Skipped = $true
}
}
}
# --- STAGE 2: Parallel Processing with Pre-fetched Data ---
$results = [System.Collections.Concurrent.ConcurrentBag[object]]::new()
$sync = [System.Collections.Hashtable]::Synchronized(@{
processed = 0; updates = 0; errors = 0
total = $validModuleCountForProcessing; startTime = Get-Date
})
$NewLogDef = ${function:New-Log}.ToString()
$CompareModuleVersionDef = ${function:Compare-ModuleVersion}.ToString()
New-Log "Starting parallel update comparison for $($moduleDataArray.Count) modules (Throttle: $ThrottleLimit)..." -Level SUCCESS
$moduleDataArray | ForEach-Object -ThrottleLimit $ThrottleLimit -Parallel {
$script:ErrorActionPreference = 'Continue' # Set for this parallel runspace
$moduleData = $_
$moduleName = $moduleData.ModuleName
$sync = $using:sync
$VerbosePreference = $using:VerbosePreference
${function:New-Log} = $using:NewLogDef
${function:Compare-ModuleVersion} = $using:CompareModuleVersionDef
$results = $using:results
$matchAuthor = $using:MatchAuthor.IsPresent
$onlineCache = $using:onlineModuleVersionsCache # Access the main cache
try {
$highestLocalInstall = $moduleData.HighestLocalInstall
if (-not $highestLocalInstall) {
New-Log "[$moduleName] Pre-processed HighestLocalInstall object missing. Skipping." -Level WARNING
$sync.errors++