From e3c2d6c0748ff62b24a780637b745a07c261a1e4 Mon Sep 17 00:00:00 2001 From: "shaun.greatbatch" Date: Fri, 19 Sep 2025 16:09:01 +0100 Subject: [PATCH] =?UTF-8?q?fix(dism-progress):=20make=20percent=20parser?= =?UTF-8?q?=20locale-safe=20and=20resilient=20-=20Accept=20=E2=80=9C4.9%?= =?UTF-8?q?=E2=80=9D=20and=20=E2=80=9C4,9%=E2=80=9D;=20strip=20%;=20use=20?= =?UTF-8?q?integer=20part=20only=20-=20Prevent=20conversion=20warnings;=20?= =?UTF-8?q?steady=20progress=20output?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SRGAdminMenu.ps1 | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/SRGAdminMenu.ps1 b/SRGAdminMenu.ps1 index 7a7ddf0..059afd6 100644 --- a/SRGAdminMenu.ps1 +++ b/SRGAdminMenu.ps1 @@ -230,13 +230,11 @@ function Invoke-DismWithProgress { $outf = Join-Path $env:TEMP "dism_out_$ts.txt" $errf = Join-Path $env:TEMP "dism_err_$ts.txt" - # Ensure we get a log file too (helps debugging) if (-not ($Arguments -match '/LogPath:')) { $logFile = Join-Path $env:TEMP "dism_log_$ts.log" $Arguments += "/LogPath:$logFile" } - # Launch DISM and redirect output so we can parse percentages safely $p = Start-Process -FilePath dism.exe ` -ArgumentList ($Arguments -join ' ') ` -NoNewWindow ` @@ -247,18 +245,21 @@ function Invoke-DismWithProgress { $lastPct = -1 while (-not $p.HasExited) { if (Test-Path $outf) { - # Read current output and grab the latest percentage like "63.8%" or "63%" $content = Get-Content -Path $outf -Raw -ErrorAction SilentlyContinue if ($content) { - $m = [regex]::Matches($content, '(\d{1,3}(?:\.\d+)?)%') + # Capture "NN", optionally followed by decimal with . or , then % + # Examples matched: "4.9%", "4,9 %", "62%" -> group 1 = integer part + $m = [regex]::Matches($content, '(\d{1,3})(?:[.,]\d+)?\s*%') if ($m.Count -gt 0) { - $pct = [int][math]::Min(100, [double]$m[$m.Count-1].Groups[1].Value) + $pctText = $m[$m.Count-1].Groups[1].Value + $pct = 0 + [void][int]::TryParse($pctText, [ref]$pct) + if ($pct -gt 100) { $pct = 100 } if ($pct -ne $lastPct) { Write-Progress -Activity "DISM $($Arguments -join ' ')" -Status "$pct%" -PercentComplete $pct $lastPct = $pct } } else { - # No explicit percent yet—show a spinner Write-Progress -Activity "DISM $($Arguments -join ' ')" -Status "Working..." -PercentComplete 0 } } @@ -266,17 +267,13 @@ function Invoke-DismWithProgress { Start-Sleep -Milliseconds 400 } - # Complete the bar cleanly Write-Progress -Activity "DISM $($Arguments -join ' ')" -Completed $exit = $p.ExitCode - - # Show a quick summary if it failed if ($exit -ne 0) { Write-Warning "DISM exited with code $exit" if (Test-Path $errf) { Get-Content $errf -Tail 25 | Write-Warning } } - return $exit }