-
This commit is contained in:
parent
cd1d8c2e72
commit
5c43bdeabf
1 changed files with 49 additions and 53 deletions
|
|
@ -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 background job to read output while process runs
|
||||
$job = Start-Job -ScriptBlock {
|
||||
param($stdoutFile, $stderrFile, $processId)
|
||||
# Start process in new window (removed -NoNewWindow)
|
||||
$process = Start-Process -FilePath $FilePath -ArgumentList $Arguments -PassThru -RedirectStandardOutput $stdoutFile -RedirectStandardError $stderrFile -ErrorAction Stop
|
||||
|
||||
# Poll output files while process runs
|
||||
$lastStdoutPos = 0
|
||||
$lastStderrPos = 0
|
||||
|
||||
while ($true) {
|
||||
# Check if process still running
|
||||
$proc = Get-Process -Id $processId -ErrorAction SilentlyContinue
|
||||
if (-not $proc) { break }
|
||||
while (-not $process.HasExited) {
|
||||
Start-Sleep -Milliseconds 300
|
||||
|
||||
# Read stdout
|
||||
# Read new stdout content
|
||||
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 }
|
||||
}
|
||||
$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
|
||||
# Read new stderr content
|
||||
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 }
|
||||
}
|
||||
$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
|
||||
}
|
||||
}
|
||||
|
||||
Start-Sleep -Milliseconds 200
|
||||
}
|
||||
} -ArgumentList $stdoutFile, $stderrFile, $process.Id
|
||||
|
||||
# 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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue