-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorchestrator.ps1
1510 lines (923 loc) · 40.4 KB
/
orchestrator.ps1
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
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
# Import script for Topological Sort
. (Join-Path $PSScriptRoot sort.ps1)
#region: Classes & Variables
class Managers {
[bool] $psrget = $False
[bool] $winget = $False
[bool] $scoop = $False
}
class Package {
[string]$Manager=""
[string]$Id=""
[string]$Source=""
}
class Software {
[string]$Name=""
[string]$Alias=""
[Package[]]$Package=@([Package]::new())
[int]$Selected=0
[string]$Version=""
[string]$Available=""
[string[]]$Dependencies=@()
[string[]]$Urls
[bool]$Prerequisite=$false
[int]$Sort=0
[action]$Action=[action]::None
[bool]$Installed=$false
[bool]$InManifest=$false
}
#TODO: Future use?
class Collection {
[Software[]]$Software=@()
# Method to print the Packages array as a table
[void]PrintCollection() {
if ($this.Software.Count -eq 0) {
Write-Host "No software available."
return
}
Write-Host($this.Software | Format-Table -AutoSize | Out-String)
}
}
#TODO: Future use?
class Install : Software {
[Package]$Package
}
enum action
{
None = 0
Install = 1
Update = 2
Remove = 3
}
# Global variables
$managerAvailable = [Managers]::new()
$managers = @("psrget", "winget", "scoop")
$inventory = @()
$manifest = @()
$debug = $True
#endregion
#region: Helper Functions
# Helper function to log/write messages
function Write-Message {
param (
[string]$Message,
[string]$Type = 'INFO'
)
# Initialize values
$timestamp = $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")
$label = $Type.ToUpper()
# Calculate padding based on the longest label
$maxLabelLength = 7
$paddingLength = $maxLabelLength - $label.Length
if ( $paddingLength -lt 0 ) { $padding = '' } else { $padding = (' ' * $paddingLength) }
# Construct the formatted message with padding after the label
$formattedMessage = "[$timestamp] [$label] $padding $Message"
# Set color based on the type and output message
switch ($label) {
'INFO' {
Write-Host $formattedMessage -ForegroundColor Green
}
'ERROR' {
Write-Host $formattedMessage -ForegroundColor Red
}
'WARNING' {
Write-Host $formattedMessage -ForegroundColor Yellow
}
'DEBUG' {
if ( $debug ) { Write-Host $formattedMessage -ForegroundColor Cyan }
}
default {
Write-Host $formattedMessage -ForegroundColor White
}
}
# Path to the log file
$logFilePath = (Join-Path $PSScriptRoot "log.txt")
# Append the formatted message to the log file
$formattedMessage | Out-File -FilePath $logFilePath -Append
}
# Helper function to print collection as table
function Show-Collection {
param (
[Software[]] $collection,
[string[]] $Exclude = @()
)
# Get all property names of the Software class and exclude specified properties
$visibleProperties = [Software]::new().PSObject.Properties.Name | Where-Object { $_ -notin $Exclude }
# Display the collection as a table with specified properties, outputting only to the console
$collection | Select-Object -Property $visibleProperties | Format-Table -AutoSize | Out-Host
}
#endregion
#region Main Function
function Start-PackageOrchestrator {
Write-Message "Starting package orchestrator..." "INFO"
# PREPARATIONS
# Check if NuGet is installed
$nugetProvider = Get-PackageProvider -Name NuGet -Force -ErrorAction SilentlyContinue
# Install NuGet if it is not already installed
if (-not $nugetProvider) {
Write-Message "NuGet provider not found. Installing..." "DEBUG"
[void](Install-PackageProvider -Name NuGet -Force -ForceBootstrap -Scope CurrentUser)
} else {
Write-Message "NuGet provider is already installed." "DEBUG"
}
# IMPORT MANIFEST
Write-Message "Importing user manifest from file..." "INFO"
# Get manifest from file
Import-FromJsonFile "manifest" | ForEach-Object {
# Convert JSON object from manifest file to Software a instance
$manifestSoftware = ConvertTo-Class -SourceObject $_ -TargetType ([Software])
# Set the manifest property to True as it is coming from the manifest
$manifestSoftware.InManifest = $true
# Append the modified instance directly to the SoftwareList array
$global:manifest += @($manifestSoftware)
}
# Print imported manifest
Show-Collection $global:manifest -Exclude @("Sort", "InManifest", "Selected", "Version", "Available", "Action", "Installed")
# INSTALL & CHECK PACKAGE MANAGERS
Write-Message "Checking available package managers and installing if required..." "INFO"
# Get data and build a package list from the manifest
$global:manifest| Where-Object { $_.prerequisite } | ForEach-Object {
# Get package details for "prerequisite" software
Get-Package $_
# Check if there is an action on the package
if ( $_.Action -ne [action]::None ) {
# Build action method name from package data and execute
& "$([action].GetEnumName($_.Action))-Package" $_ | Out-Null
}
}
# Set managers available based on inventory status
$managers | ForEach-Object {
# Check if manager software is in inventory
if ( -not (Test-Dependency @($_ ) ) ) {
# Set manager as available
$global:managerAvailable.$($_) = $true
} else {
Write-Message "Could not confirm manager available ($($_)). Package handling for this manager will be ignored." "WARNING"
}
}
# BUILD INVENTORY
Write-Message "Checking system for installed software..." "INFO"
# Get installed applications for all package managers
$managers | ForEach-Object {
# Find installed software foe manager if available
if ( $global:managerAvailable.$($_) ) { Find-Packages $_ }
}
# TODO: Need to include package manager as a dependency on each object when not custom?
# CHECK MANIFEST PACKAGES
Write-Message "Checking software from manifest, updating details and action..." "INFO"
# Check packages in manifest and update details and inventory
$global:manifest | ForEach-Object { Get-Package $_ }
# Print updated manifest
Show-Collection $global:manifest -Exclude @("InManifest", "Sort")
#TODO: Cross-reference with inventory to confirm manager/id??
## CREATE PLAN
Write-Message "Creating plan for manifest software with sorted, actioned items..." "INFO"
# Filter out packages from manifest with an action
$actionedPackages = $global:manifest | Where-Object { $_.Action -ne [action]::None }
# Add sort number to packages based on dependencies and sort them
$plan = Get-TopologicalSort $actionedPackages "Alias" "Dependencies" | Sort-Object -Property Sort
# Print updated manifest
Show-Collection $plan -Exclude @("InManifest", "Urls", "Installed", "Prerequisite")
## EXECUTE ACTIONS
Write-Message "Executing manifest plan (installing/updating software)..." "INFO"
# Run appropriate manager action method for each package
$plan | ForEach-Object {
# Build action method name from package data and execute
& "$([action].GetEnumName($_.Action))-Package" $_ | Out-Null
# TODO: Find more generic solution.....
if ( "$($_.Alias)" -eq "powershell") {
Test-PowerShellVersion
}
}
## SAVE INVENTORY
# Print updated manifest
Show-Collection $inventory -Exclude @("Urls", "Sort", "Prerequisite", "Dependencies", "Action", "Selected", "Installed")
# Prepare inventory list for export to file
$inventoryFile = $global:inventory | Select-Object -ExcludeProperty Installed,Urls,Dependencies,Action,Sort,Selected | Sort-Object -Property Name
# Save inventory to file
Export-ToJsonFile $inventoryFile "inventory"
Write-Message "Package orchestrator finished." "INFO"
}
#endregion
#region Version Handling
function Get-Version {
param (
[string] $rawVersion
)
if ( ( $rawVersion -match '\d+(\.\d+){0,2}' ) ) {
return $matches[0]
} else {
Write-Warning "Could not extract a valid version number (${rawVersion})."
return $null
}
}
function Compare-Versions {
param (
[string] $versionOne,
[string] $versionTwo
)
# Helper function to parse version numbers
function Search-Version {
param ([string]$version)
$parsedVersion = New-Object int[] 3
$versionParts = $version -split '\.'
for ($i = 0; $i -lt 3; $i++) {
$parsedVersion[$i] = if ($i -lt $versionParts.Count) { [int]$versionParts[$i] } else { 0 }
}
return $parsedVersion
}
$parsedVersionOne = Search-Version -version $versionOne
$parsedVersionTwo = Search-Version -version $versionTwo
for ($i = 0; $i -lt 3; $i++) {
if ($parsedVersionOne[$i] -gt $parsedVersionTwo[$i]) {
return $true
} elseif ($parsedVersionOne[$i] -lt $parsedVersionTwo[$i]) {
return $false
}
}
return $false
}
#endregion
#region: Import / Export
function Export-ToJsonFile {
param (
[PSObject] $objectList,
[string] $name
)
try {
# Convert the object list to JSON format
$jsonData = $objectList | ConvertTo-Json
} catch {
# Handle any errors that occur during the conversion to JSON
Write-Message "Could not save content. Failed to convert object list to JSON -> ($($_.Exception.Message))" "WARNING"
return
}
# Specify the path for the JSON file
$jsonFilePath = (Join-Path $PSScriptRoot "${name}.json")
# Log the attempt to save the JSON data
Write-Message "Saving JSON content to '$jsonFilePath'..." "DEBUG"
try {
# Save the JSON data to the file
$jsonData | Set-Content -Path $jsonFilePath -Force
} catch {
# Handle any errors that occur during the file writing process
Write-Message "Failed to save JSON content to '$jsonFilePath'. $($_.Exception.Message)" "ERROR"
return
}
}
function Import-FromJsonFile {
param (
[string] $name
)
# Construct the full path to the JSON file
$jsonFilePath = (Join-Path $PSScriptRoot "${name}.json")
# Log the attempt to import the file
Write-Message "Importing content from '$jsonFilePath'..." "DEBUG"
# Check if the file exists before proceeding
if (-not (Test-Path $jsonFilePath)) {
# If the file doesn't exist, log an error and exit the function
Write-Message "The file '$jsonFilePath' does not exist." "ERROR"
return $null
}
try {
# Read the content of the file and convert it from JSON format
$content = Get-Content -Path $jsonFilePath -Raw | ConvertFrom-Json
} catch {
# Handle any errors that occur during JSON conversion
Write-Message "Failed to parse JSON content from '$jsonFilePath' -> ($($_.Exception.Message)" "ERROR"
return $null
}
# Return the parsed content
return $content
}
#endregion
#region: Inventory Handling
function Update-InventoryAliasById {
param (
[Software] $item,
[int] $index=0
)
# Check for package in inventory and update
$global:inventory | ForEach-Object {
# Compare current inventory entry with package ID
if ($_.package[$index].Id -eq $item.package[$index].Id) {
Write-Message "Updating package entry in inventory with alias from manifest ($($item.Alias))..." "DEBUG"
# Update alias
$_.Alias = $item.Alias
return $True
}
}
Write-Message "Package with ID '$($item.package[$index].Id)' not found in inventory." "DEBUG"
return $False
}
function Add-ToInventory {
param (
[Software] $item,
[int] $index=0
)
Write-Message "Updating package inventory with installed package '$($item.package[$index].Id)'..." "DEBUG"
try {
# Add package to inventory if not already included
if ( -not ( $global:inventory | Where-Object { $_.package[$index].Id -eq $item.package[$index].Id } ) ) {
# Add the package to the inventory list
$global:inventory += @($item)
} else {
Write-Message "Package already present in inventory." "DEBUG"
}
}
catch {
Write-Message "Issues with adding package to inventory -> ($($_.Exception.Message))" "WARNING"
}
}
function Test-Dependency {
param (
[array] $dependencies
)
# Initialize array for missing dependencies
$local:missingDependencies = @()
# Check if any dependencies to check
if ( $item.Dependencies ) {
Write-Message "Checking for dependencies in inventory ($($dependencies))..." "DEBUG"
# Check each dependency package id
ForEach ( $dependency in $dependencies) {
Write-Message "Searching for dependency ($($dependency))..." "DEBUG"
# Find the package with the specified id
$dependencyPackage = $global:inventory | Where-Object { $_.Alias -eq $dependency }
# Check if the dependency was found and flagged as installed
if ( -not $dependencyPackage) {
# Add to missing dependencies
$missingDependencies += $dependency
}
}
}
return $missingDependencies
}
#endregion
#region: Package Handlers
function Find-Packages {
param (
[string]$packageManager
)
$idFilter = @('MSIX', 'HP','km-', 'Intel', 'Printer','{')
$nameFilter = @('Citrix')
Write-Message "Finding installed packages ($($packageManager))..." "INFO"
try {
# Retrieve installed packages based for package manager
switch ($packageManager) {
"psrget" {
$installedPackages = Get-InstalledPSResource
$getAvailableVersion = { (Find-PSResource $installedPackage.Name -ErrorAction SilentlyContinue).Version }
}
"scoop" {
$installedPackages = scoop list 6> $null
$getAvailableVersion = { (scoop info $package.Id 6> $null).Version }
}
"winget" {
$installedPackages = Get-WinGetPackage
$getAvailableVersion = { ($installedPackage.AvailableVersions[0]) }
}
default {
Write-Warning "Unknown package manager: $packageManager"
return @()
}
}
# Apply filtering to exclude packages with certain substrings in Id or Name
$filteredPackages = $installedPackages | Where-Object {
( -not ( $_.Id -match [string]::Join('|', $idFilter) ) ) -and
( -not ( $_.Name -match [string]::Join('|', $nameFilter) ) )
}
# Create package objects for filtered packages
$filteredPackages | ForEach-Object {
$installedSoftware = Build-PackageDetails -packageManager $packageManager -installedPackage $_ -getAvailableVersion $getAvailableVersion
if ( $installedSoftware ) { Add-ToInventory $installedSoftware }
}
}
catch {
Write-Message "Could not get installed packages ($($packageManager)): $($_.Exception.Message)" "ERROR"
}
}
function Get-Package {
param (
[Software] $item,
[int] $index=0
)
Write-Message "Checking software status and updating details ($($item.package[$index].Manager) / $($item.Alias))..." "DEBUG"
# Initialize values
$installedPackage = $null
$packageAvailable = $null
try {
# Switch based on the manager type to handle different package managers
switch ($item.package[$index].Manager) {
"psrget" {
# Handle PSResourceGet package manager
$installedPackage = Get-InstalledPSResource -Name $item.package[$index].Id -ErrorAction SilentlyContinue
$packageAvailable = $(Find-PSResource $item.package[$index].Id).Version
}
"scoop" {
# Handle Scoop package manager
$installedPackage = scoop list $item.package[$index].Id 6> $null
$packageAvailable = $(scoop info $item.package[$index].Id 6> $null).Version
}
"winget" {
# Handle WinGet package manager
$installedPackage = Get-WinGetPackage $item.package[$index].Id
$installedPackage | Add-Member -MemberType NoteProperty -Name Version -Value $installedPackage.InstalledVersion
$packageAvailable = $(Find-WinGetPackage -Id $item.package[$index].Id).Version
}
"custom" {
# Handle Custom package manager
$fullFilePath = (Join-Path $PSScriptRoot "$($item.Alias)/setup.ps1")
# Test file path and run custom methods
if (Test-Path $fullFilePath -PathType Leaf) {
# Run custom script if available
. $fullFilePath
# Check installation status and get version
if ( & "Test-$($item.Name)" ) {
$installedPackage = [PSCustomObject]@{
Version = & "Get-$($item.Name)"
}
}
# Get available version for custom package
$packageAvailable = & "Get-$($item.Name)Available"
} else {
# Handle missing file path
throw "The file '$fullFilePath' does not exist"
}
}
default {
# Handle unknown package manager
throw "Unknown package manager '$($item.package[$index].Manager)'"
}
}
} catch {
# Handle any errors that occur during the process
Write-Message "Error getting package details for '$($item.package[$index].Id)' -> ($($_.Exception.Message))" "WARNING"
return
}
# If the package is installed, update the package details
if ( $installedPackage ) {
# Update details
$item.Version = Get-Version $installedPackage.Version
$item.Installed = $True
$item.Action=[action]::None
# Update inventory if the package is installed but not in inventory
if ( -not (Update-InventoryAliasById $item ) ) {
# Add as new entry to inventory if installed, but not in inventory.
# Should only happen for Custom packages.
Add-ToInventory $item
}
} else {
# If not installed, mark the package for installation
$item.Action = [action]::Install
}
# If available version is found, compare with the installed version
if ( $packageAvailable ) {
$item.Available = Get-Version $packageAvailable
# Check that we have valid version numbers
if ($item.Version -and $item.Available) {
# Check if the installed version is the same as the available one
if (Compare-Versions $item.Available $item.Version -and $item.Installed) {
# If newer version available, mark the package for update
$item.Action = [action]::Update
}
}
}
}
function Install-Package {
param (
[Software]$item
)
$index = $item.Selected
Write-Message "Installing software package ($($item.package[$index].Manager) / $($item.Alias))..." "DEBUG"
try {
# Check for missing dependencies
if ( $missingDependencies = (Test-Dependency $item.Dependencies) ) {
# Handle missing dependencies
throw "Missing dependencies (${missingDependencies})"
}
# Switch based on the manager type to handle different package managers
switch ($item.package[$index].Manager) {
"psrget" {
# Install package with PSResourceGet
$result = $( Install-PSResource -Name $item.package[$index].id -NoClobber -AcceptLicense -PassThru -ErrorAction Stop )
# Check result from install
if ( $result ) { $success = $true } else { $success = $false }
}
"scoop" {
# Install package with Scoop
$result = $( scoop install $item.package[$index].id 6>&1 )
# Check result from install
if ( $( $result | Out-String ).Contains("successfully") ) { $success = $true } else { $success = $false }
}
"winget" {
# Install package with WinGet
$result = $( Install-WinGetPackage $item.package[$index].id )
# Check result from install
if ( $result.Succeeded() ) { $success = $true } else { $success = $false }
}
"custom" {
# Assuming custom packages have specific scripts or executables to install
$installScript = Join-Path $PSScriptRoot "$($item.Alias)/setup.ps1"
# Test file path and run custom methods
if (Test-Path $installScript -PathType Leaf) {
# Dot source the script to bring in the functions
. $installScript
# Install package using custom install script
$result = $( & "Install-$($item.Name)" 2>&1 )
if ("${result}" -eq "Reboot") {
Restart-ComputerAndContinue
Write-Message "Installation of '$($item.alias)' will continue on script startup after reboot." "INFO"
return $false
}
$success = $result
} else {
# Handle missing file path
throw "The file '$fullFilePath' does not exist"
}
}
default {
# Handle unknown package manager
throw "Unknown package manager '$($item.package[$index].Manager)'"
}
}
# Check if install was successful
if ( $success ) {
# Try to get installed package details (updates Installed flag)
Get-Package $item
# Check if actually installed
if ( $item.Installed ) {
# Compare installed version with available
if ( $item.Version -eq $item.Available ) {
Write-Message "Package successfully installed ($($item.Alias) / $($item.package[$index].Id))." "DEBUG"
} else {
Write-Message "Potential package install issue (could not find installed version $($package.Available)). Computer or application might need to be restarted." "WARNING"
}
# Update PATH variable
Update-Path
} else {
# Handle unconfirmed install
throw "Could not get confirm that package was installed"
}
} else {
# Handled failed install
throw "Install method failed"
}
}
catch {
# Handle any errors that occur during the process
Write-Message "Failed to install '$($item.package[$index].Id)' using '$($item.package[$index].Manager)' -> ($($_.Exception.Message))" "WARNING"
return $false
}
return $true
}
function Update-Package {
param (
[Software] $item
)
$index = $item.Selected
Write-Message "Updating software package ($($item.package[$index].Manager) / $($item.Alias))..." "DEBUG"
try {
# Switch based on the manager type to handle different package managers
switch ($item.package[$index].Manager) {
"psrget" {
# Install package with PSResourceGet
$result = $( Update-PSResource -Name $item.package[$index].id -NoClobber -AcceptLicense -PassThru -ErrorAction Stop)
# Check result from install
if ( $result ) { $success = $true } else { $success = $false }
}
"scoop" {
# Install package with Scoop
$result = $( scoop update $item.package[$index].id --quiet 6>&1 )
# Check result from install
if ( $( $result | Out-String ).Contains("ERROR") ) { $success = $true } else { $success = $false }
}
"winget" {
# Check if update is available
if ( $( Get-WinGetPackage $package.id ).IsUpdateAvailable ) {
# Install package with winget
$result = $( Update-WinGetPackage $item.package[$index].id )
# Check result from install
if ( $result.Succeeded() ) { $success = $true } else { $success = $false }
} else {
Write-Message "No update available (check plan requesting update to $($item.available))." "WARNING"
}
}
"custom" {
# Assuming custom packages have specific scripts or executables to install
$installScript = Join-Path $PSScriptRoot "$($item.Alias)/setup.ps1"
# Test file path and run custom methods
if (Test-Path $installScript -PathType Leaf) {
# Dot source the script to bring in the functions
. $installScript
# Install package using custom install script
$result = $( & "Update-$($item.Name)" 2>&1 )
$success = $result
} else {
# Handle missing file path
throw "The file '$fullFilePath' does not exist"
}
}
default {
# Handle unknown package manager