This commit is contained in:
Joeri 2026-02-21 15:41:48 +01:00
parent cd1d8c2e72
commit 5c43bdeabf

View file

@ -31,8 +31,8 @@ $ScriptResult = @{
$tempPath = $env:TEMP
# Function to run process with live output using job
function Start-ProcessWithLiveOutput {
# Function to run process in new window with live output
function Start-ProcessWithNewWindow {
param(
[string]$FilePath,
[string]$Arguments,
@ -46,60 +46,53 @@ function Start-ProcessWithLiveOutput {
if (Test-Path $stdoutFile) { Remove-Item $stdoutFile -Force -ErrorAction SilentlyContinue }
if (Test-Path $stderrFile) { Remove-Item $stderrFile -Force -ErrorAction SilentlyContinue }
Write-Host "Starting $Name..."
Write-Host "Starting $Name in new window..."
# Start process
$process = Start-Process -FilePath $FilePath -ArgumentList $Arguments -NoNewWindow -PassThru -RedirectStandardOutput $stdoutFile -RedirectStandardError $stderrFile -ErrorAction Stop
# Start process in new window (removed -NoNewWindow)
$process = Start-Process -FilePath $FilePath -ArgumentList $Arguments -PassThru -RedirectStandardOutput $stdoutFile -RedirectStandardError $stderrFile -ErrorAction Stop
# Start background job to read output while process runs
$job = Start-Job -ScriptBlock {
param($stdoutFile, $stderrFile, $processId)
# Poll output files while process runs
$lastStdoutPos = 0
$lastStderrPos = 0
while (-not $process.HasExited) {
Start-Sleep -Milliseconds 300
$lastStdoutPos = 0
$lastStderrPos = 0
while ($true) {
# Check if process still running
$proc = Get-Process -Id $processId -ErrorAction SilentlyContinue
if (-not $proc) { break }
# Read stdout
if (Test-Path $stdoutFile) {
$stdoutSize = (Get-Item $stdoutFile).Length
if ($stdoutSize -gt $lastStdoutPos) {
$content = Get-Content -Path $stdoutFile -Raw
if ($content) {
$newContent = $content.Substring($lastStdoutPos)
if ($newContent) { Write-Host $newContent -NoNewline }
}
$lastStdoutPos = $stdoutSize
}
# Read new stdout content
if (Test-Path $stdoutFile) {
$stdoutSize = (Get-Item $stdoutFile).Length
if ($stdoutSize -gt $lastStdoutPos) {
$stream = [System.IO.File]::OpenRead($stdoutFile)
$stream.Seek($lastStdoutPos, [System.IO.SeekOrigin]::Begin) | Out-Null
$bytesToRead = $stdoutSize - $lastStdoutPos
$buffer = New-Object byte[] $bytesToRead
$stream.Read($buffer, 0, $bytesToRead) | Out-Null
$stream.Close()
$text = [System.Text.Encoding]::UTF8.GetString($buffer)
if ($text) { Write-Host $text -NoNewline }
$lastStdoutPos = $stdoutSize
}
# Read stderr
if (Test-Path $stderrFile) {
$stderrSize = (Get-Item $stderrFile).Length
if ($stderrSize -gt $lastStderrPos) {
$content = Get-Content -Path $stderrFile -Raw
if ($content) {
$newContent = $content.Substring($lastStderrPos)
if ($newContent) { Write-Host $newContent -NoNewline }
}
$lastStderrPos = $stderrSize
}
}
Start-Sleep -Milliseconds 200
}
} -ArgumentList $stdoutFile, $stderrFile, $process.Id
# Read new stderr content
if (Test-Path $stderrFile) {
$stderrSize = (Get-Item $stderrFile).Length
if ($stderrSize -gt $lastStderrPos) {
$stream = [System.IO.File]::OpenRead($stderrFile)
$stream.Seek($lastStderrPos, [System.IO.SeekOrigin]::Begin) | Out-Null
$bytesToRead = $stderrSize - $lastStderrPos
$buffer = New-Object byte[] $bytesToRead
$stream.Read($buffer, 0, $bytesToRead) | Out-Null
$stream.Close()
$text = [System.Text.Encoding]::UTF8.GetString($buffer)
if ($text) { Write-Host $text -NoNewline }
$lastStderrPos = $stderrSize
}
}
}
# Wait for process to complete
$process.WaitForExit()
# Wait for job to finish
$job | Wait-Job | Out-Null
Receive-Job -Job $job | Out-Null
Remove-Job -Job $job -Force -ErrorAction SilentlyContinue
# Wait a moment for any final output
Start-Sleep -Milliseconds 500
# Read final output
$outputText = ""
@ -119,10 +112,11 @@ function Start-ProcessWithLiveOutput {
# Run CHKDSK
Write-Host "=========================================="
Write-Host "Running CHKDSK with /R option..."
Write-Host "Note: A new window will open for CHKDSK"
Write-Host "=========================================="
try {
$result = Start-ProcessWithLiveOutput -FilePath "cmd" -Arguments "/c echo Y | chkdsk C: /R" -Name "chkdsk"
$result = Start-ProcessWithNewWindow -FilePath "cmd" -Arguments "/c echo Y | chkdsk C: /R" -Name "chkdsk"
$ChkdskExitCode = $result.ExitCode
$outputText = $result.Output
@ -154,10 +148,11 @@ try {
# Run SFC
Write-Host "=========================================="
Write-Host "Running System File Checker (SFC /scannow)..."
Write-Host "Note: A new window will open for SFC"
Write-Host "=========================================="
try {
$result = Start-ProcessWithLiveOutput -FilePath "sfc" -Arguments "/scannow" -Name "sfc"
$result = Start-ProcessWithNewWindow -FilePath "sfc" -Arguments "/scannow" -Name "sfc"
$ExitCode = $result.ExitCode
$outputText = $result.Output
@ -184,10 +179,11 @@ try {
# Run DISM
Write-Host "=========================================="
Write-Host "Running DISM /Online /Cleanup-Image /RestoreHealth..."
Write-Host "Note: A new window will open for DISM"
Write-Host "=========================================="
try {
$result = Start-ProcessWithLiveOutput -FilePath "DISM" -Arguments "/Online /Cleanup-Image /RestoreHealth" -Name "dism"
$result = Start-ProcessWithNewWindow -FilePath "DISM" -Arguments "/Online /Cleanup-Image /RestoreHealth" -Name "dism"
$ExitCode = $result.ExitCode
$outputText = $result.Output