-
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
|
$tempPath = $env:TEMP
|
||||||
|
|
||||||
# Function to run process with live output using job
|
# Function to run process in new window with live output
|
||||||
function Start-ProcessWithLiveOutput {
|
function Start-ProcessWithNewWindow {
|
||||||
param(
|
param(
|
||||||
[string]$FilePath,
|
[string]$FilePath,
|
||||||
[string]$Arguments,
|
[string]$Arguments,
|
||||||
|
|
@ -46,60 +46,53 @@ function Start-ProcessWithLiveOutput {
|
||||||
if (Test-Path $stdoutFile) { Remove-Item $stdoutFile -Force -ErrorAction SilentlyContinue }
|
if (Test-Path $stdoutFile) { Remove-Item $stdoutFile -Force -ErrorAction SilentlyContinue }
|
||||||
if (Test-Path $stderrFile) { Remove-Item $stderrFile -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
|
# Start process in new window (removed -NoNewWindow)
|
||||||
$process = Start-Process -FilePath $FilePath -ArgumentList $Arguments -NoNewWindow -PassThru -RedirectStandardOutput $stdoutFile -RedirectStandardError $stderrFile -ErrorAction Stop
|
$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
|
$lastStdoutPos = 0
|
||||||
$lastStderrPos = 0
|
$lastStderrPos = 0
|
||||||
|
|
||||||
while ($true) {
|
while (-not $process.HasExited) {
|
||||||
# Check if process still running
|
Start-Sleep -Milliseconds 300
|
||||||
$proc = Get-Process -Id $processId -ErrorAction SilentlyContinue
|
|
||||||
if (-not $proc) { break }
|
|
||||||
|
|
||||||
# Read stdout
|
# Read new stdout content
|
||||||
if (Test-Path $stdoutFile) {
|
if (Test-Path $stdoutFile) {
|
||||||
$stdoutSize = (Get-Item $stdoutFile).Length
|
$stdoutSize = (Get-Item $stdoutFile).Length
|
||||||
if ($stdoutSize -gt $lastStdoutPos) {
|
if ($stdoutSize -gt $lastStdoutPos) {
|
||||||
$content = Get-Content -Path $stdoutFile -Raw
|
$stream = [System.IO.File]::OpenRead($stdoutFile)
|
||||||
if ($content) {
|
$stream.Seek($lastStdoutPos, [System.IO.SeekOrigin]::Begin) | Out-Null
|
||||||
$newContent = $content.Substring($lastStdoutPos)
|
$bytesToRead = $stdoutSize - $lastStdoutPos
|
||||||
if ($newContent) { Write-Host $newContent -NoNewline }
|
$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
|
$lastStdoutPos = $stdoutSize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Read stderr
|
# Read new stderr content
|
||||||
if (Test-Path $stderrFile) {
|
if (Test-Path $stderrFile) {
|
||||||
$stderrSize = (Get-Item $stderrFile).Length
|
$stderrSize = (Get-Item $stderrFile).Length
|
||||||
if ($stderrSize -gt $lastStderrPos) {
|
if ($stderrSize -gt $lastStderrPos) {
|
||||||
$content = Get-Content -Path $stderrFile -Raw
|
$stream = [System.IO.File]::OpenRead($stderrFile)
|
||||||
if ($content) {
|
$stream.Seek($lastStderrPos, [System.IO.SeekOrigin]::Begin) | Out-Null
|
||||||
$newContent = $content.Substring($lastStderrPos)
|
$bytesToRead = $stderrSize - $lastStderrPos
|
||||||
if ($newContent) { Write-Host $newContent -NoNewline }
|
$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
|
$lastStderrPos = $stderrSize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Start-Sleep -Milliseconds 200
|
|
||||||
}
|
}
|
||||||
} -ArgumentList $stdoutFile, $stderrFile, $process.Id
|
|
||||||
|
|
||||||
# Wait for process to complete
|
# Wait a moment for any final output
|
||||||
$process.WaitForExit()
|
Start-Sleep -Milliseconds 500
|
||||||
|
|
||||||
# Wait for job to finish
|
|
||||||
$job | Wait-Job | Out-Null
|
|
||||||
Receive-Job -Job $job | Out-Null
|
|
||||||
Remove-Job -Job $job -Force -ErrorAction SilentlyContinue
|
|
||||||
|
|
||||||
# Read final output
|
# Read final output
|
||||||
$outputText = ""
|
$outputText = ""
|
||||||
|
|
@ -119,10 +112,11 @@ function Start-ProcessWithLiveOutput {
|
||||||
# Run CHKDSK
|
# Run CHKDSK
|
||||||
Write-Host "=========================================="
|
Write-Host "=========================================="
|
||||||
Write-Host "Running CHKDSK with /R option..."
|
Write-Host "Running CHKDSK with /R option..."
|
||||||
|
Write-Host "Note: A new window will open for CHKDSK"
|
||||||
Write-Host "=========================================="
|
Write-Host "=========================================="
|
||||||
|
|
||||||
try {
|
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
|
$ChkdskExitCode = $result.ExitCode
|
||||||
$outputText = $result.Output
|
$outputText = $result.Output
|
||||||
|
|
||||||
|
|
@ -154,10 +148,11 @@ try {
|
||||||
# Run SFC
|
# Run SFC
|
||||||
Write-Host "=========================================="
|
Write-Host "=========================================="
|
||||||
Write-Host "Running System File Checker (SFC /scannow)..."
|
Write-Host "Running System File Checker (SFC /scannow)..."
|
||||||
|
Write-Host "Note: A new window will open for SFC"
|
||||||
Write-Host "=========================================="
|
Write-Host "=========================================="
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$result = Start-ProcessWithLiveOutput -FilePath "sfc" -Arguments "/scannow" -Name "sfc"
|
$result = Start-ProcessWithNewWindow -FilePath "sfc" -Arguments "/scannow" -Name "sfc"
|
||||||
$ExitCode = $result.ExitCode
|
$ExitCode = $result.ExitCode
|
||||||
$outputText = $result.Output
|
$outputText = $result.Output
|
||||||
|
|
||||||
|
|
@ -184,10 +179,11 @@ try {
|
||||||
# Run DISM
|
# Run DISM
|
||||||
Write-Host "=========================================="
|
Write-Host "=========================================="
|
||||||
Write-Host "Running DISM /Online /Cleanup-Image /RestoreHealth..."
|
Write-Host "Running DISM /Online /Cleanup-Image /RestoreHealth..."
|
||||||
|
Write-Host "Note: A new window will open for DISM"
|
||||||
Write-Host "=========================================="
|
Write-Host "=========================================="
|
||||||
|
|
||||||
try {
|
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
|
$ExitCode = $result.ExitCode
|
||||||
$outputText = $result.Output
|
$outputText = $result.Output
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue