From 990979705089bff1be48e5eee6f547529c05d6be Mon Sep 17 00:00:00 2001 From: CodingWonders <101426328+CodingWonders@users.noreply.github.com> Date: Fri, 4 Oct 2024 16:42:12 +0200 Subject: [PATCH] Add sorting to error messages Thanks @og-mrk for the suggestion and the patch (even though I applied it myself) --- .../private/Invoke-WinUtilMicroWin-Helper.ps1 | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/functions/private/Invoke-WinUtilMicroWin-Helper.ps1 b/functions/private/Invoke-WinUtilMicroWin-Helper.ps1 index f4918bb82d..75771981f4 100644 --- a/functions/private/Invoke-WinUtilMicroWin-Helper.ps1 +++ b/functions/private/Invoke-WinUtilMicroWin-Helper.ps1 @@ -36,6 +36,30 @@ class ErroredPackage { } } +class ErroredPackageComparer : System.Collections.Generic.IComparer[ErroredPackage] { + [string]$PropertyName + [bool]$Descending = $false + + ErroredPackageComparer([string]$property) { + $this.PropertyName = $property + } + + ErroredPackageComparer([string]$property, [bool]$descending) { + $this.PropertyName = $property + $this.Descending = $descending + } + + [int]Compare([ErroredPackage]$a, [ErroredPackage]$b) { + if ($a.$($this.PropertyName) -eq $b.$($this.PropertyName)) { $res = 0 } + elseif ($a.$($this.PropertyName) -lt $b.$($this.PropertyName)) { $res = -1 } + else { $res = 1 } + + if ($this.Descending) { $res *= -1 } + + return $res + } +} + function Get-FidoLangFromCulture { param ( @@ -200,6 +224,12 @@ function Remove-Packages { if ($erroredPackages.Count -gt 0) { $erroredPackages + $ErrorMessageComparer = [ErroredPackageComparer]::new("ErrorMessage") + $erroredPackages.Sort($ErrorMessageComparer) + foreach ($erroredPackage in $erroredPackages) { + Write-Host "Failed to remove Package $($erroredPackage.PackageName) due to $($erroredPackage.ErrorMessage)" -NoNewline + } + #$erroredPackages } } } catch {