-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPSWizget.ps1
1193 lines (1055 loc) · 45 KB
/
PSWizget.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
<#PSScriptInfo
.VERSION 1.0.15
.GUID acb8f443-01c8-40b3-9369-c5e6e28548d1
.AUTHOR [email protected]
.COMPANYNAME
.COPYRIGHT GPL-3.0
.TAGS PSEdition_Core PSEdition_Desktop windows winget batch upgrade wizard update all queue
.LICENSEURI https://opensource.org/licenses/GPL-3.0
.PROJECTURI https://github.com/zakret/PSwizget
.ICONURI
.EXTERNALMODULEDEPENDENCIES PowerShellGet
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
- once a week, the script checks if there is a new version available on powershellgallery.com
and if so, the information is added to the table;
- the package's manifest file is downloaded if there is an update for it.
Information about the release notes and the URL for them are retrieved from the manifest file
and can be presented to the user under the [ R ] option;
- manifests older than 3 months are deleted at the end of the script;
- additional files to the script are now placed in the folder;
- testing the Internet connection can be omitted with the omitNetTest parameter;
- reformating the table, separators and menu;
- better parameter validation, error handling and easier debugging;
- adjusted version recognition mechanism;
- the script can now play an idle animation while waiting for the user's response
( idleAnimation parameter ).
.PRIVATEDATA
#>
<#
.SYNOPSIS
PowerShell script that allow you to manage the upgrade process with winget.
.DESCRIPTION
PowerShell script that allow you to manage the upgrade process with winget.
It adds a few more options than 'winget upgrade --all':
- create a file with the packages you would like to omit;
- add or remove packages from the blacklist file directly from the script;
- automatically omit packages with "unknown" installed version, or when the formats of
the installed version and the available version formats does not match;
- it tries to guess the correct installed version by reading the pattern
from the available version;
- manually edit the upgrade queue;
- quick mode (it's similar to 'winget upgrade --all' but with a blacklist applied);
- wingetParam <string> option with custom parameters to pass to winget. '-h' is set by default;
- you can preselect one of the options available from the menu by adding the -option parameter
with A, C or S argument;
- you can read the package release notes if this information is provided in the manifest file.
Known issue with Windows Powershell ver. <= 5.1 (desktop):
Due to the ascii encoding, packages with longer names than 30 chars may corrupt
the 'winget upgrade' result, i.e. info about the long name package
and the packages listed after it.
Please use this script with PowerShell ver. > 5.1 (core) if you can
or avoid installing long name packages with winget.
.NOTES
This is my first powershell script for educational purposes.
.EXAMPLE
PS> .PSWizget.ps1
.INPUTS
None. You cannot pipe objects to PSWizget.ps1.
.OUTPUTS
None. PSWizget.ps1 does not generate any output.
.LINK
Project page https://github.com/Zakret/PSwizget
.FUNCTIONALITY
winget batch upgrade wizard update all queue
.PARAMETER quick
The Script won't ask for any unessentialy input. It will exclude packages blacklisted
or with an unrecognized version and then the update will start.
.PARAMETER wizgetFolderPath
Non-standard location of the local Wizget folder. Default is "~\Wizget\". It will contain all
files required by the script. It doesn't need to be created beforehand.
.PARAMETER wingetParam
custom parameters to pass to winget. '-h' is set by default.
.PARAMETER omitNetTest
With this parameter setting, the script will assume that you are connected.
.PARAMETER idleAnimation
Add an ASCII animation in the bottom-right corner of the menu. Put the animation in a folder
and save all its frames as txt files.
.PARAMETER idleSpeed
Specify the animation playback speed. By default it is 1.
.PARAMETER option
Start with the pre-selected option:
C custom queue creation.
A package addition.
S package omission.
.PARAMETER waitingTime
Wait x seconds and then proceed with the update.
#>
[CmdletBinding(DefaultParameterSetName = 'set0')]
param (
[Parameter(Position=2)]
[string]$wizgetFolderPath = "$($HOME)\Wizget\",
[Parameter(Position=3)]
[string]$wingetParam = "-h",
[switch]$omitNetTest,
[Parameter(Position=4)]
[string[]]$idleAnimation,
[ Parameter(ParameterSetName="set1") ]
[switch]$quick,
[ Parameter(ParameterSetName="set2", Position=1) ]
[ ValidateSet( 'a', 's', 'c' ) ]
[char]$option,
[ Parameter(ParameterSetName="set2", Position=1) ]
[int32]$waitingTime
)
DynamicParam {
if ($idleAnimation) {
$parameterAttribute = [System.Management.Automation.ParameterAttribute]@{
Mandatory = $false
ParameterSetName = '__AllParameterSets'
Position = 5
}
$attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
$attributeCollection.Add($parameterAttribute)
$attributeCollection.Add((New-Object System.Management.Automation.ValidateRangeAttribute(1,100)))
$dynParam1 = [System.Management.Automation.RuntimeDefinedParameter]::new(
"idleSpeed", [Int32], $attributeCollection
)
$PSBoundParameters["idleSpeed"] = 1
$paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$paramDictionary.Add('idleSpeed', $dynParam1)
return $paramDictionary
}
}
Process {
#region FUNCTIONS
#region UNIVERSAL FUNCTIONS
function Write-Art {
param (
[Parameter(Mandatory, Position=1)]
[ValidateCount(2,2)]
[int[]]$startPoint,
[Parameter(Mandatory, Position=2)]
[string]$artPath,
[Parameter(Position=3)]
[ValidateCount(0,2)]
[string[]]$chosenColors
)
$currentPosition = $Host.UI.RawUI.CursorPosition
$Colors = [enum]::GetValues([System.ConsoleColor])
foreach ($color in $chosenColors) {
if ($color -eq "Random") {
$chosenColors[$chosenColors.IndexOf($color)] = Get-Random -InputObject $Colors
} elseif ($Colors -notcontains $color) {
[Console]::SetCursorPosition($startPoint[0],$startPoint[1]-1)
Write-Error "'$($color)' is not a color supported by Powershell" -ErrorAction SilentlyContinue
$Host.UI.RawUI.CursorPosition = $currentPosition
if ($color -eq $chosenColors[0]) {
$chosenColors[$chosenColors.IndexOf($color)] = $host.UI.RawUI.ForegroundColor
} else {
$chosenColors[$chosenColors.IndexOf($color)] = $host.UI.RawUI.BackgroundColor
}
}
}
if (!$chosenColors[0]) {
$chosenColors =@()
$chosenColors += $host.UI.RawUI.ForegroundColor
}
if(!$chosenColors[1]) {$chosenColors += $host.UI.RawUI.BackgroundColor}
if (!(Test-Path -Path $artPath)) {
$art = $artPath.Split("`n")
} else {
$art = Get-Content -Path $artPath
}
for ($l=0; $l -lt $art.Count; $l++) {
[Console]::SetCursorPosition($startPoint[0],$startPoint[1]+$l)
Write-Host $art[$l] -ForegroundColor $chosenColors[0] -BackgroundColor $chosenColors[1]
}
$Host.UI.RawUI.CursorPosition = $currentPosition
}
function Clear-HostRange {
<#
.DESCRIPTION
The function clears the specified range of console lines. After that, the cursor
will return to its last location. If no parameters are specified, clears the line
before the cursor position and moves the cursor to that position.
If only a start point is specified, the end point will be the index of the line before
the cursor position. If only the end point is specified, the start point will be set to 0.
.PARAMETER Startpoint
Index of the line at which line clear up will begin.
.PARAMETER Endpoint
Index of the line at which line clear up will end.
#>
Param (
[Parameter(Position=1)]
[int32]$Startpoint,
[Parameter(Position=2)]
[int32]$Endtpoint
)
$CurrentLine = $Host.UI.RawUI.CursorPosition.Y
$ConsoleWidth = $Host.UI.RawUI.BufferSize.Width
if ( !$Startpoint -and !$Endtpoint ) {
$Range = @($($CurrentLine-1),$($CurrentLine))
} else{
$Range = @(0,$($CurrentLine-1))
if ( $Startpoint ) { $Range[0] = $Startpoint }
if ( $Endtpoint ) { $Range[1] = $Endtpoint }
}
$Range | ForEach-Object {
if ( $_ -lt 0 -or $_ -ge ($host.UI.RawUI.BufferSize.Height)) {Write-Error "'$_' is out of the buffer size."}
}
$Range = $Range | Sort-Object
$Count = $Range[1]-$Range[0]
$i = 0
for ($i; $i -le $Count; $i++) {
[Console]::SetCursorPosition(0,($Range[0] + $i))
[Console]::Write("{0,$ConsoleWidth}" -f " ")
[Console]::SetCursorPosition(0,($Range[0] + $i))
}
if ( !$Startpoint -and !$Endtpoint ) {
[Console]::SetCursorPosition(0,($Currentline-1))
} else {
[Console]::SetCursorPosition(0,($Currentline-1))
[Console]::Write("{0,$ConsoleWidth}" -f " ")
[Console]::SetCursorPosition(0,($Currentline-1))
}
}
function Write-Separator {
<#
.DESCRIPTION
Write-Host a colored line made of multipled string.
.PARAMETER type
The string to be multiplied.
.PARAMETER length
Line length.
.PARAMETER color
Line color.
.PARAMETER allowMultiLines
The line won't be trimmed to the window size.
.PARAMETER noNewLine
Cursor position will be set to the end of the line.
.PARAMETER header
Header will be print on the central position of the line.
#>
param (
[string]$type = '-',
[string]$color = 'Green',
[string]$header,
[string]$headerColor,
[switch]$allowMultiLines,
[int]$length = $host.UI.RawUI.BufferSize.Width,
[switch]$noNewLine,
$position
)
$currentPosition = $Host.UI.RawUI.CursorPosition.Y
if ($color -notin ([enum]::GetValues([System.ConsoleColor]))) {$color = 'Green'}
$line = ''
if ($header) { $start = [int32]$($length/2-$header.Length/2-2)}
for ( $i = 0; $i -lt $length; $i ++ ) {
$line = $line + $type
}
if ( !$allowMultiLines -and $line -gt $host.UI.RawUI.BufferSize.Width) {
$line = $line.Substring(0,$host.UI.RawUI.BufferSize.Width)
}
if ($null -ne $position) {
if ($position -isnot 'int' -or $position -lt 0) {
Write-Error '$position is not an valid integer'
}
[Console]::SetCursorPosition(0,($position))
}
if ($header) {
if (!$headerColor) { $headerColor = $color }
if ($headerColor -notin ([enum]::GetValues([System.ConsoleColor]))) {$color = 'Green'}
[int32]$start = [int32]$($line.Length/2 - ($header.Length+4)/2)
$shortLine = $line.Substring(0,$start)
Write-host "$($shortLine)[ " -ForegroundColor $color -NoNewline
Write-Host $header -ForegroundColor $headerColor -NoNewline
Write-host " ]$($shortLine)" -ForegroundColor $color
} else {
Write-Host $line -ForegroundColor $color
}
if ($noNewLine){
if ( $null -eq $position ) { [Console]::SetCursorPosition($line.Length, `
($Host.UI.RawUI.CursorPosition.Y-1))
} else { [Console]::SetCursorPosition($line.Length,$position) }
}
if ( $null -ne $position -and !$noNewLine ) { [Console]::SetCursorPosition(0, ($currentPosition)) }
}
function Get-Answer {
<#
.SYNOPSIS
Wait for user input for specified amount of time.
.DESCRIPTION
Wait for user input for specified amount of time. The delay is an integer and amounts to 0.1s.
By default it's 50, so ~5s. It only takes one key ready. After the delay, function
returns False.
.EXAMPLE
Get-Answer -Delay 100
wait 10 seconds for user input, return single key object or false if no input has been entered
.PARAMETER Delay
Waiting time for the function to return False expressed in 0.1s.
.NOTES
Modified code by Every Villain Is Lemons
https://stackoverflow.com/a/16965646
#>
param( [ Parameter(Position = 1) ] [int]$Delay = 0 )
$counter = 0
$host.UI.RawUI.FlushInputBuffer()
if ($Delay -eq 0) {Write-Host "`rPress anything else to continue. " -NoNewline}
while( ($counter++ -lt $Delay) -or $Delay -eq 0) {
if ( $Host.UI.RawUI.KeyAvailable ) {
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp")
break
}
else{
#start extra useless segment for the script (not universal)
if (($i=($counter-1)/$Script:PSBoundParameters.idlespeed) -is "int" -and ($Delay -gt 50 -or $Delay -eq 0) -and $frames) {
$i = $i - [int]($i/($frames.Count-1))*($frames.Count-1)
Write-Art 56,($host.UI.RawUI.CursorPosition.Y-7) $frames[$i].FullName $idleAnimation[1],$idleAnimation[2]
}
#end extra useless segment for the script (not universal)
if (($k=($counter-1)/10) -is "int" -and $Delay -ne 0) {
Write-Host "`rWait $($Delay/10-[int32]$k) sec or press anything else to continue. " -NoNewline
}
Start-Sleep -Milliseconds 100
$key = $false
}
}
Write-Host "`r " -NoNewline
Write-Host "`r" -NoNewline
return ( $key )
}
function Get-IntAnswer {
<#
.SYNOPSIS
Get selected numbers from the options presented to the user. User input must be integers separated by
spaces.
.DESCRIPTION
Get selected numbers from the options presented to the user.
User input must be integers separated by spaces, any other input will be rejected
by function.
.PARAMETER options
an array of item names from which the user can choose
#>
param (
[ Parameter(Mandatory) ]
[ string[] ]$options
)
$options | ForEach-Object {$i = 0} {
$i ++
Write-Host "[ $($i) ] $($_)"
}
Write-Separator -length 55
$hostSelection = Read-Host @"
Select the package indexes to which you want to apply the changes (eg. 1 4).
Press [ Q ] to return to the menu
"@
do {
$hostArray = $hostSelection.Split(" ")
$wrongInput = $false
if ( !$hostArray -or $hostArray -eq 'q') {
break
}
try {
$hostArray = foreach ( $i in $hostArray ) { [Convert]::ToInt32($i) }
} catch { $wrongInput = $true }
if ( $wrongInput -eq $false ) {
$wrongInput = $hostArray.Where({ $_ -le 0 -or $_ -gt $options.Count},
'SkipUntil', 1)
$wrongInput = [boolean]$wrongInput
}
if ( $wrongInput -eq $true ) {
$hostSelection = Read-Host "Don't be silly! Only mentioned numbers separated $(
)by a space! Please select"
}
} while ( $wrongInput -eq $true )
return $hostArray
}
function Write-PackagesStatus {
<#
.SYNOPSIS
Show packages from array and message about their status
.DESCRIPTION
Show the status of packages, that means an array to which the package has been moved by
Split-Result function.
It's displayed as follows: "Package/s <array> is/are <message>" or "<message>"
if list is empty.
.EXAMPLE
Write-PackagesStatus -namesArray @(Package1, Package2) -secondHalf "fantastic!"
"Packages Package1, Package2 are fantastic!"
.PARAMETER namesArray
Array with list of packages names of the same status.
.PARAMETER secondHalf
The string displayed after the package list. It's a status description.
.PARAMETER emptyMessage
The string displayed if the package array is empty
#>
param(
[ Parameter(Mandatory) ]
[ AllowNull() ]
[string[]]$namesArray,
[ Parameter(Mandatory) ]
[string]$secondHalf,
[ Parameter() ]
[string]$emptyMessage = ""
)
if ( $namesArray.Count -eq 1 ) {
Write-Host "The " -NoNewline
Write-Host $namesArray[0] -ForegroundColor Yellow -NoNewline
Write-Host " package is $($secondHalf)"
}
elseif ( $namesArray.Count -ne 0) {
Write-Host "Packages " -NoNewline
for ( $i=0; $i -lt $namesArray.count; $i++){
Write-Host $namesArray[$i] -ForegroundColor Yellow -NoNewline
if ( $i -ne $namesArray.count-1 ) {Write-Host ', ' -NoNewline}
}
Write-Host " are $($secondHalf)"
}
elseif ( $emptyMessage -ne "" ) {
Write-Host $emptyMessage -ForegroundColor Yellow
}
}
#endregion UNIVERSAL FUNCTIONS
#region FUNCTIONS SPECIFIED FOR THE SCRIPT
function Reset-Setup {
<#
.DESCRIPTION
Reset the values to their past state before the script was initialized.
#>
$host.UI.RawUI.WindowTitle = $initialSetup.WindowTitle
$VerbosePreference = $initialSetup.VerbosePreference
$InformationPreference = $initialSetup.InformationPreference
}
function Split-Result {
<#
.SYNOPSIS
Split the 'winget upgrade' result into three arrays: okList, noList, unknownVer
.DESCRIPTION
Split the 'winget upgrade' result into three groups: skippable (noList),
unrecognizable (unknownList) and upgradeable (okList) ones.
Also tries to correctly recognize packages with an incorrect format of the installed version.
These objects must be defined in the script before calling this function:
$upgradeList collection, with Package object with Name, ID, Version, AvailableVersion
$blacklistPath with location to the blacklist file
#>
[OutputType([Object[]])]
$Script:upgradeList | ForEach-Object -Begin {
$okList = @()
$noList = @()
$unknownVer = @()
} -Process {
if ($Script:toSkip -contains $_.Id) { $noList += $_ }
elseif ($_.Version -contains "Unknown") { $unknownVer += $_ }
elseif (($_.Version.Length -gt $_.AvailableVersion.Length) -and !$quick) {
$versionPattern = $_.AvailableVersion -replace "[a-z]",'[a-z]'
$versionPattern = $versionPattern -replace "[0-9]",'[0-9]'
$versionPattern = $versionPattern -replace "\.",'\.'
$_.Version -match "(?<Version>$($versionPattern))" | Out-Null
$hostResponse = Read-Host "Is version '$($Matches.Version)' the correct version$(
) of $($_.Name) installed? Available version is '$(
$_.AvailableVersion)'. [ Y ] for Yes, anything else to skip$(
) this package"
if ( $hostResponse -like "y" ) {
$_.Version = $Matches.Version
$versionsComparison = @($_.Version, $_.AvailableVersion) |
Sort-Object -Descending
if ( $_.Version -eq $_.AvailableVersion -or `
$versionsComparison[0] -eq $_.Version) {
Write-Information "There is no need to update $($_.Name). Omitting the package."
$dummyAnswer = Get-Answer 30
if ( $dummyAnswer ){}
} else { $okList += $_ }
} else { $unknownVer += $_ }
} elseif ($_.Version.Length -lt $_.AvailableVersion.Length) {
$_.AvailableVersion -match '(^.*(?<numberToCompare>[0-9])\.[0-9]+$)' | Out-Null
$availableNumber = $Matches.numberToCompare
$_.Version -match '(^.*(?<numberToCompare>[0-9])$)' | Out-Null
$currentNumber = $Matches.numberToCompare
if ( $availableNumber -and $currentNumber) {
if ( $availableNumber -ge $currentNumber ) {
$okList += $_
} else {
Write-Information "There is no need to update $($_.Name). Omitting the package."
$dummyAnswer = Get-Answer 30
if ( $dummyAnswer ){}
}
} else {
$unknownVer += $_
Write-Information "Installed ($($_.Version)) and available version ($(
$_.AvailableVersion)) formats for package $(
$_.Name) are different."
$dummyAnswer = Get-Answer 30
if ( $dummyAnswer ){}
}
} else { $okList += $_ }
} -End {
return $okList, $noList, $unknownVer
}
}
function Show-Result {
<#
.DESCRIPTION
It uses the Write-PackagesStatus function to return all the messages needed in this script
at once.
#>
Write-PackagesStatus -namesArray $unknownVer.Name -secondHalf $Script:unknownVerMessage
Write-PackagesStatus -namesArray $noList.Name -secondHalf $Script:noListMessage
Write-PackagesStatus -namesArray $okList.Name -secondHalf $Script:okListMessage `
-emptyMessage $Script:emptyListMessage
}
function Show-UI {
<#
.SYNOPSIS
It shows the user all the available options that he can choose to manipulate the blacklist file
or the update queue.
.DESCRIPTION
It shows the user all the available options that he can choose to manipulate the blacklist file
or the update queue. Options available:
- Add the package Id to the blacklist file
- Remove package Id from the blacklist
- Add the excluded packages to the update queue
- Only this time, skip the package in the queue
- Create a new custom upgrade queue
The function determines which packages are relevant to which option and shows them to the user
as an indexed list. The user select packages by entering their indexes separated
by spaces.
The function has all the mechanisms needed for manipulation.
.PARAMETER okList
an object with packages to be updated
.PARAMETER noList
an object with blacklisted packages to be omitted
.PARAMETER unknownVer
an object with packages to be omitted due to unrecognized version info
.EXAMPLE
Show-UI -okList $okList -noList $noList -unknownVer $unknownVer
<Table with upgradable packages>
---
Packages A, B are recognized incorrectly (installed version info).
Package C is listed in the blacklist file.
Package D id going to be updated.
---
<a list with options indexed with single letters>
[A] Option A
---
Wait 10 sec or press anything else to continue
<User input - only one char read>
A
<indexed list of packages relevant to selected option>
[1] Package B
[2] Package D
Select the package indexes to which you want to apply the changes (eg. 1 4):
<User input - integers seperated by spaces>
1 2
<Packages B and D are manipulated as indicated by the option>
<Clear-Host and run function again>
<User unput Enter>
<Function ends>
#>
param (
[ Parameter(Mandatory) ]$okList,
[ Parameter(Mandatory) ]$noList,
[ Parameter(Mandatory) ]$unknownVer
)
Clear-Host
if ( !$Script:extraLine ) {Write-Host} # BUG 01 fix
#region CUSTOM FORMAT-TABLE
$columnsDivision = @(0.30,0.30,0.20,0.20)
$List = $Script:upgradeList
if ($Script:timeForUpdate) { $List += $Script:WizgetInfo}
$formatedList = $List | Select-Object -Property * `
-ExcludeProperty ManifestPath,ReleaseNotes,ReleaseNotesURL
if ( $($columnsDivision | Measure-Object -Sum).sum -ne 1 ) {Write-Error "Wrong division"}
$columnsTitles = $formatedList[0].psobject.properties | Select-Object name
$columnsTitles = $columnsTitles.Name
$columnsTitles | ForEach-Object -Begin {$columnsWidth = ''; $i =0} -Process {
$columnsWidth += '@{ e="'+$_+'"; Width = '+
$([Math]::Truncate($columnsDivision[$i]*$maxWidth))+' }, '
$i++
} -End {$columnsWidth = $columnsWidth.TrimEnd(', ')}
$codeString = [scriptblock]::Create($('$formatedList | Format-Table -Property '+$columnsWidth))
Invoke-Command -ScriptBlock $codeString
Clear-HostRange
Write-Separator -position 3 -length $maxWidth
#endregion CUSTOM FORMAT-TABLE
# Headline
$halfWidth = [math]::Truncate($maxWidth/2)
$currentLine = $host.UI.RawUI.CursorPosition.Y
Write-Separator -position 0 -header ($Script:scriptInfo.Name+", ver. "+$Script:scriptInfo.Version) `
-headerColor $host.UI.RawUI.ForegroundColor -length $halfWidth -noNewLine
Write-Separator -header $Script:scriptInfo.ProjectUri `
-headerColor $host.UI.RawUI.ForegroundColor -length $halfWidth
[Console]::SetCursorPosition(0,$currentLine)
# Menu
Write-Separator -length $maxWidth
Show-Result
Write-Separator -length $maxWidth
Write-Host @"
[ B ] Add the package Id to the blacklist file
[ W ] Remove package Id from the blacklist
[ A ] Add the excluded packages to the update queue
[ S ] Only this time, skip the package in the queue
[ C ] Create a new custom upgrade queue
[ R ] Read the release notes for the package
"@
Write-Separator -length 55
if ($idleAnimation -and !$frames) {
Write-Art 56,($host.UI.RawUI.CursorPosition.Y-8) $idleAnimation[0] $idleAnimation[1],$idleAnimation[2]
}
$hostResponse = New-Object -TypeName System.Management.Automation.Host.KeyInfo
if ( $option -eq 'a' ) {
Write-Host "Do you want to add one of these packages to the update queue?"
$hostResponse.Character = "a"
$option = $false
} elseif ( $option -eq 'c' ) {
Write-Host "Please define the update queue:"
$hostResponse.Character = "c"
$option = $false
} elseif ( $option -eq 's' ) {
Write-Host "Do you want to omit any of these packages?"
$hostResponse.Character = "s"
$option = $false
} else {
$hostResponse = Get-Answer -Delay $($waitingTime*10)
}
$optionsList = @()
switch ( $hostResponse.Character ) {
'b' {
$optionsList = $Script:upgradeList | Where-Object {$_ -NotIn $noList}
if ( !$optionsList ) {
Write-Verbose "`rThere are no packages that could be added to the blacklist file."
} else {
$hostArray = Get-IntAnswer -options $optionsList.Name
if ( $hostArray -notin @('q', $false ) ) {
foreach ( $k in $hostArray ) {
Add-Content -Path $Script:blacklistPath -Value $optionsList[$k - 1].Id
$toSkip, $noList = @(), @()
$toSkip = Get-Content $Script:blacklistPath
foreach ($package in $Script:upgradeList) {
if ($toSkip -contains $package.Id)
{ $noList += $package }
}
$okList = $oklist | Where-Object {$_ -notIn @($optionsList[$k - 1])}
if ( $null -eq $okList) { $okList = @() }
Write-Verbose "`rAdded $($optionsList[$k - 1].Name) to the blacklist file."
}
}
}
}
'w' {
$optionsList = $noList
if ( !$optionsList ) {
Write-Verbose "`rFor now, the blacklist file is empty"
} else {
$hostArray = Get-IntAnswer -options $optionsList.Name
if ( $hostArray -notin @('q', $false ) ) {
foreach ( $k in $hostArray ) {
Set-Content -Path $Script:blacklistPath -Value (get-content `
-Path $Script:blacklistPath | Where-Object {$_.trim() -ne ""} | Select-String `
-SimpleMatch $optionsList[$k - 1].Id -NotMatch)
$noList = $nolist | Where-Object {$_ -notIn @($optionsList[$k - 1])}
if ( $null -eq $noList) { $noList = @() }
if ( $optionsList[$k - 1].Version -ne 'Unknown' ){
$okList += @($optionsList[$k - 1])
}
Write-Verbose "`rRemoved $($optionsList[$k - 1].Name) from the blacklist file."
}
}
}
}
'a' {
$optionsList = $unknownVer + $noList
if ( !$optionsList ) {
Write-Verbose "`rThere are no excluded packages"
} else {
$hostArray = Get-IntAnswer -options $optionsList.Name
if ( $hostArray -notin @('q', $false ) ) {
foreach ( $k in $hostArray ) {
$okList += @($optionsList[$k - 1])
Write-Verbose "`rAdded $($optionsList[$k - 1].Name) to this upgrade queue."
}
}
}
}
's' {
$optionsList = $okList
if ( !$optionsList ) {
Write-Verbose "`rThere are no upgradeable packages"
} else {
$hostArray = Get-IntAnswer -options $optionsList.Name
if ( $hostArray -notin @('q', $false ) ) {
foreach ( $k in $hostArray ) {
$okList = $oklist | Where-Object {$_ -notin @($optionsList[$k - 1])}
if ( $null -eq $okList) { $okList = @() }
Write-Verbose "`rRemoved $($optionsList[$k - 1].Name) from this upgrade queue."
}
}
}
}
'c' {
$optionsList = $Script:upgradeList
$hostArray = Get-IntAnswer -options $optionsList.Name
if ( $hostArray -notin @('q', $false ) ) {
$okList = @()
foreach ($k in $hostArray) {
$okList += $optionsList[$k - 1]
}
$Script:okListS = $okList
break
}
}
'r' {
$optionsList = $Script:upgradeList
if ($Script:timeForUpdate) { $optionsList += $Script:WizgetInfo}
$hostArray = Get-IntAnswer -options $optionsList.Name
if ( $hostArray -notin @('q', $false ) ) {
foreach ($k in $hostArray) {
Clear-Host
Write-Separator -header `
($optionsList[$k - 1].Name+" - "+$optionsList[$k - 1].Id+" - "+
$optionsList[$k - 1].AvailableVersion) `
-headerColor "White" -length $maxWidth
Write-Host $("file://$($optionsList[$k - 1].ManifestPath)" -replace "\\", "/")
If ( !($optionsList[$k - 1].ReleaseNotes -or $optionsList[$k - 1].ReleaseNotesURL)) {
Write-Information "No information about available release."
} else {
foreach ($line in $optionsList[$k - 1].ReleaseNotesURL) {Write-Host $line}
foreach ($line in $optionsList[$k - 1].ReleaseNotes) {Write-Host $line}
}
Write-Separator -length 55
Write-Host "Next " -NoNewline
If ( $k -eq $hostArray[-1] ) {Read-Host}
else { Read-Host $optionsList[$k].Name }
}
}
}
{ $_ -in 'b', 'w', 'a', 's', 'c', 'r' } {
if ( $hostArray -notin @('q', $false ) ) { Get-Answer -Delay 20 | Out-Null }
$Script:okListS = $okList
Show-UI -okList $okList -noList $noList -unknownVer $unknownVer
}
}
}
#endregion FUNCTIONS SPECIFIED FOR THE SCRIPT
#endregion FUNCTIONS
#region STARTUP
$scriptInfo = Test-ScriptFileInfo -LiteralPath $MyInvocation.MyCommand.Path
$powershellgalleryName = $scriptInfo.Name
$scriptInfo.Name = 'PS Wizget'
$maxWidth = 100
$host.UI.RawUI.BufferSize.Width = $maxWidth; $host.UI.RawUI.WindowSize.Width = $maxWidth
if ($host.UI.RawUI.BufferSize.Width -lt $maxWidth) {
$maxWidth = $host.UI.RawUI.BufferSize.Width
}
$initialSetup = [PSCustomObject]@{
WindowTitle = $host.UI.RawUI.WindowTitle
VerbosePreference = $VerbosePreference
InformationPreference = $InformationPreference
}
$host.UI.RawUI.WindowTitle = $scriptInfo.Name
if (!$quick) {
$VerbosePreference = 'Continue'
$InformationPreference = 'Continue'
}
#endregion STARTUP
#region PARAMETERS VALIDATION
if ( $waitingTime -lt 0 ) { Write-Error "Waiting time lesser than 0" }
# idleAnimation test
if ($idleAnimation){
try {
$frames = Get-ChildItem -Path $idleAnimation[0] -ErrorAction SilentlyContinue
}
catch {
Write-Information 'Provided string as idleAnimation' -InformationAction SilentlyContinue
}
$Colors = [enum]::GetValues([System.ConsoleColor])
if ($idleAnimation[1] -eq "Random") {
$idleAnimation[1] = Get-Random -InputObject $Colors
}
if ($idleAnimation[2] -eq "Random") {
$idleAnimation[2] = Get-Random -InputObject $Colors
}
}
if ( !$PSBoundParameters.idleSpeed ) { $PSBoundParameters.idleSpeed = 1 }
# create and read files from profile directory
$invalidChar = '[^"<>*|?]'
$test = "^"+$invalidChar+"+$"
if ( $wizgetFolderPath -notmatch $test ) {
Write-Error 'The passed string is not a location!'
Reset-Setup
Exit 102
}
if ( $wizgetFolderPath[-1] -notin @( "\", "/" )) {
$wizgetFolderPath = "$($wizgetFolderPath)\"
}
$wizgetFolderPath = $wizgetFolderPath -replace '/', "\"
if ( !(Test-Path -Path $wizgetFolderPath) ) {
try {
Write-Information "A wizget folder has been created in:" -InformationAction Continue
New-Item -ItemType Directory -Path "$($wizgetFolderPath)Manifests" -ErrorAction Stop
}
catch {
Write-Error "The wizget folder could not be created. Make sure that you $(
)have a read/write permissions to the target path"
Reset-Setup
Exit 105
}
# BUG 01 an additional line is created after the Clear-Host call.
# This is somehow related to the New-Item function.
$extraLine = $true
}
$blacklistPath = "$($wizgetFolderPath)blacklist.txt"
if ( !(Test-Path -Path $blacklistPath) ) {
try {
"" | Out-File -LiteralPath $blacklistPath -ErrorAction Stop
Write-Information "A blacklist file has been created in:" -InformationAction Continue
Write-Information $blacklistPath
} catch {
Write-Error "The blacklist file could not be created. Make sure that you $(
)have a read/write permissions to the target folder"
Reset-Setup
Exit 106
}
Write-Information "$(Resolve-Path -path $blacklistPath)" -InformationAction SilentlyContinue
Write-Information ""
Write-Information "Add the IDs of packages with a different format for 'version' and $(
)'available version' to this file."
If (!$quick) {Read-Host | Out-Null}
} else {
#test blacklist file
try{
[string[]]$toSkip = Get-Content $blacklistPath -TotalCount 1 -ErrorAction Stop
Add-Content -path $blacklistPath -Value "" -ErrorAction Stop
} catch {
Write-Error "The blacklist file could not be accessed. Make sure that it $(
)is a valid text file with read/write permissions and is not being used by any process."
Reset-Setup
Exit 107
}
if ( $null -ne $toSkip ) {
$invalidChar = $invalidChar.TrimEnd("]")+"\\/.]"
$test = '^'+$invalidChar+'+\.'+$invalidChar+'+$'
if ( $toSkip[0] -match $test -or $toSkip[0] -eq "") {
[string[]]$toSkip = Get-Content $Script:blacklistPath
$toSkip | ForEach-Object -Begin {$i=0} -Process{
if ( $_ -notmatch $test -and $_ -ne "" ) {
Write-Error "The blacklist have an invalid record (No. $($i+1))."
Reset-Setup
Exit 108
}
$i++
}
} else {
Write-Error "It seems the blacklist contains invalid records."
Reset-Setup
Exit 109
}
[string[]]$toSkip = Get-Content $blacklistPath
}
}
#endregion PARAMETERS VALIDATION
#region REQUIREMENTS TEST
# winget test
$wingetExist = test-path -path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\winget.exe"
if ( !$wingetExist ) {
Write-Verbose "`rFirst please install winget from msstore"
Reset-Setup
Exit 101
}
# encoding test
if( $OutputEncoding.WindowsCodePage -ne 1200 ) {
Write-Warning "`rYour Powershell encoding is not utf8. Packages with longer names than 30 chars $(
)may corrupt the 'winget upgrade' result."
Write-Information "Powershell encoding : $($OutputEncoding.HeaderName)"
}
Write-Information 'Please wait...'
#internet connection test
$oldProgressPreference = $Global:ProgressPreference
$Global:ProgressPreference = 'SilentlyContinue'
if (!$omitNetTest){
Write-Verbose "`rTesting connection... "
if (!$quick) {[Console]::SetCursorPosition(0,($host.UI.RawUI.CursorPosition.Y-1))}
if ( !( Test-NetConnection -InformationLevel Quiet)) {
Write-Verbose "`rNo internet connection. "
Reset-Setup
Exit 110
}
}
$Global:ProgressPreference = $oldProgressPreference
#endregion REQUIREMENTS TEST
Write-Verbose "`rConnected. Fetching from 'winget upgrade'..."
if (!$quick) {[Console]::SetCursorPosition(0,($host.UI.RawUI.CursorPosition.Y-1))}
#region FETCHING UPGRADEABLE PACKAGES
# FROM WINGET TO COLLECTION OF OBJECT
<#
It's not mine code in this region. I found it on stackoverflow some time ago but can't find it now.
I modified it a bit to use PSCustomObject instead of a class
and domnload manifest files from the winget github.
Temporory no author, no link
#>