-
This commit is contained in:
parent
ed304aea44
commit
cd1d8c2e72
1 changed files with 192 additions and 40 deletions
230
optimization.ps1
230
optimization.ps1
|
|
@ -1,73 +1,225 @@
|
||||||
# Function to run process with true live streaming output
|
$ScriptResult = @{
|
||||||
function Start-ProcessWithLiveStreaming {
|
success = $true
|
||||||
|
system_optimization = @{
|
||||||
|
"chkdsk" = @{
|
||||||
|
"run" = $false
|
||||||
|
"success" = $false
|
||||||
|
"exit_code" = $null
|
||||||
|
"message" = ""
|
||||||
|
"reboot_required" = $false
|
||||||
|
"diskSpace" = ""
|
||||||
|
"errors" = @()
|
||||||
|
}
|
||||||
|
"sfc" = @{
|
||||||
|
"run" = $false
|
||||||
|
"success" = $false
|
||||||
|
"exit_code" = $null
|
||||||
|
"message" = ""
|
||||||
|
"files_repaired" = 0
|
||||||
|
"errors" = @()
|
||||||
|
}
|
||||||
|
"dism" = @{
|
||||||
|
"run" = $false
|
||||||
|
"success" = $false
|
||||||
|
"exit_code" = $null
|
||||||
|
"message" = ""
|
||||||
|
"component_repaired" = $false
|
||||||
|
"errors" = @()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$tempPath = $env:TEMP
|
||||||
|
|
||||||
|
# Function to run process with live output using job
|
||||||
|
function Start-ProcessWithLiveOutput {
|
||||||
param(
|
param(
|
||||||
[string]$FilePath,
|
[string]$FilePath,
|
||||||
[string]$Arguments,
|
[string]$Arguments,
|
||||||
[string]$Name
|
[string]$Name
|
||||||
)
|
)
|
||||||
|
|
||||||
$tempPath = $env:TEMP
|
|
||||||
$stdoutFile = "$tempPath\${Name}_stdout.txt"
|
$stdoutFile = "$tempPath\${Name}_stdout.txt"
|
||||||
$stderrFile = "$tempPath\${Name}_stderr.txt"
|
$stderrFile = "$tempPath\${Name}_stderr.txt"
|
||||||
|
|
||||||
# Clean up old files
|
# Clean up old files
|
||||||
if (Test-Path $stdoutFile) { Remove-Item $stdoutFile -ErrorAction SilentlyContinue }
|
if (Test-Path $stdoutFile) { Remove-Item $stdoutFile -Force -ErrorAction SilentlyContinue }
|
||||||
if (Test-Path $stderrFile) { Remove-Item $stderrFile -ErrorAction SilentlyContinue }
|
if (Test-Path $stderrFile) { Remove-Item $stderrFile -Force -ErrorAction SilentlyContinue }
|
||||||
|
|
||||||
# Create file handles for immediate reading
|
|
||||||
$fileStream = [System.IO.File]::Create($stdoutFile)
|
|
||||||
|
|
||||||
Write-Host "Starting $Name..."
|
Write-Host "Starting $Name..."
|
||||||
|
|
||||||
# Start the process
|
# Start process
|
||||||
$process = Start-Process -FilePath $FilePath -ArgumentList $Arguments -NoNewWindow -PassThru -RedirectStandardOutput $stdoutFile -RedirectStandardError $stderrFile -ErrorAction Stop
|
$process = Start-Process -FilePath $FilePath -ArgumentList $Arguments -NoNewWindow -PassThru -RedirectStandardOutput $stdoutFile -RedirectStandardError $stderrFile -ErrorAction Stop
|
||||||
|
|
||||||
# Read output while process runs
|
# Start background job to read output while process runs
|
||||||
$lastPosition = 0
|
$job = Start-Job -ScriptBlock {
|
||||||
while (-not $process.HasExited) {
|
param($stdoutFile, $stderrFile, $processId)
|
||||||
Start-Sleep -Milliseconds 500
|
|
||||||
|
|
||||||
if (Test-Path $stdoutFile) {
|
$lastStdoutPos = 0
|
||||||
$currentLength = (Get-Item $stdoutFile).Length
|
$lastStderrPos = 0
|
||||||
if ($currentLength -gt $lastPosition) {
|
|
||||||
$bytesToRead = $currentLength - $lastPosition
|
while ($true) {
|
||||||
$stream = [System.IO.File]::OpenRead($stdoutFile)
|
# Check if process still running
|
||||||
$stream.Seek($lastPosition, [System.IO.SeekOrigin]::Begin) | Out-Null
|
$proc = Get-Process -Id $processId -ErrorAction SilentlyContinue
|
||||||
$buffer = New-Object byte[] $bytesToRead
|
if (-not $proc) { break }
|
||||||
$stream.Read($buffer, 0, $bytesToRead) | Out-Null
|
|
||||||
$stream.Close()
|
# Read stdout
|
||||||
$text = [System.Text.Encoding]::UTF8.GetString($buffer)
|
if (Test-Path $stdoutFile) {
|
||||||
Write-Host $text -NoNewline
|
$stdoutSize = (Get-Item $stdoutFile).Length
|
||||||
$lastPosition = $currentLength
|
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 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
|
||||||
|
|
||||||
# Wait for process to complete
|
# Wait for process to complete
|
||||||
$process.WaitForExit()
|
$process.WaitForExit()
|
||||||
|
|
||||||
# Read any remaining output
|
# Wait for job to finish
|
||||||
if (Test-Path $stdoutFile) {
|
$job | Wait-Job | Out-Null
|
||||||
$content = Get-Content -Path $stdoutFile -Raw -ErrorAction SilentlyContinue
|
Receive-Job -Job $job | Out-Null
|
||||||
if ($content) { Write-Host $content }
|
Remove-Job -Job $job -Force -ErrorAction SilentlyContinue
|
||||||
}
|
|
||||||
if (Test-Path $stderrFile) {
|
|
||||||
$content = Get-Content -Path $stderrFile -Raw -ErrorAction SilentlyContinue
|
|
||||||
if ($content) { Write-Host $content }
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get output for return
|
# Read final output
|
||||||
$outputText = ""
|
$outputText = ""
|
||||||
if (Test-Path $stdoutFile) { $outputText = Get-Content -Path $stdoutFile -Raw -ErrorAction SilentlyContinue }
|
if (Test-Path $stdoutFile) { $outputText = Get-Content -Path $stdoutFile -Raw -ErrorAction SilentlyContinue }
|
||||||
if (Test-Path $stderrFile) { $outputText += Get-Content -Path $stderrFile -Raw -ErrorAction SilentlyContinue }
|
if (Test-Path $stderrFile) { $outputText += Get-Content -Path $stderrFile -Raw -ErrorAction SilentlyContinue }
|
||||||
|
|
||||||
# Cleanup
|
# Cleanup
|
||||||
Remove-Item $stdoutFile -ErrorAction SilentlyContinue
|
Remove-Item $stdoutFile -Force -ErrorAction SilentlyContinue
|
||||||
Remove-Item $stderrFile -ErrorAction SilentlyContinue
|
Remove-Item $stderrFile -Force -ErrorAction SilentlyContinue
|
||||||
$fileStream.Close()
|
|
||||||
|
|
||||||
return @{
|
return @{
|
||||||
ExitCode = $process.ExitCode
|
ExitCode = $process.ExitCode
|
||||||
Output = $outputText
|
Output = $outputText
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Run CHKDSK
|
||||||
|
Write-Host "=========================================="
|
||||||
|
Write-Host "Running CHKDSK with /R option..."
|
||||||
|
Write-Host "=========================================="
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = Start-ProcessWithLiveOutput -FilePath "cmd" -Arguments "/c echo Y | chkdsk C: /R" -Name "chkdsk"
|
||||||
|
$ChkdskExitCode = $result.ExitCode
|
||||||
|
$outputText = $result.Output
|
||||||
|
|
||||||
|
$ScriptResult.system_optimization.chkdsk.run = $true
|
||||||
|
$ScriptResult.system_optimization.chkdsk.exit_code = $ChkdskExitCode
|
||||||
|
|
||||||
|
if ($outputText -match "will be checked on next boot|Chkdsk cannot run because the volume is in use") {
|
||||||
|
$ScriptResult.system_optimization.chkdsk.reboot_required = $true
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($ChkdskExitCode) {
|
||||||
|
0 { $ScriptResult.system_optimization.chkdsk.success = $true; $ScriptResult.system_optimization.chkdsk.message = "CHKDSK completed successfully." }
|
||||||
|
1 { $ScriptResult.system_optimization.chkdsk.success = $true; $ScriptResult.system_optimization.chkdsk.message = "CHKDSK completed successfully. Errors corrected." }
|
||||||
|
2 { $ScriptResult.system_optimization.chkdsk.success = $false; $ScriptResult.system_optimization.chkdsk.message = "CHKDSK did not run due to a parameter error." }
|
||||||
|
3 { $ScriptResult.system_optimization.chkdsk.success = $true; $ScriptResult.system_optimization.chkdsk.reboot_required = $true; $ScriptResult.system_optimization.chkdsk.message = "CHKDSK completed with errors and requires a reboot." }
|
||||||
|
4 { $ScriptResult.system_optimization.chkdsk.success = $false; $ScriptResult.system_optimization.chkdsk.message = "CHKDSK encountered errors but could not fix them." }
|
||||||
|
5 { $ScriptResult.system_optimization.chkdsk.success = $false; $ScriptResult.system_optimization.chkdsk.reboot_required = $true; $ScriptResult.system_optimization.chkdsk.message = "CHKDSK found errors but requires a reboot." }
|
||||||
|
default { $ScriptResult.system_optimization.chkdsk.success = $false; $ScriptResult.system_optimization.chkdsk.message = "CHKDSK completed with exit code: $ChkdskExitCode" }
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "CHKDSK exit code: $ChkdskExitCode"
|
||||||
|
|
||||||
|
} catch {
|
||||||
|
$ErrorMsg = "Failed to run CHKDSK: $($_.Exception.Message)"
|
||||||
|
$ScriptResult.system_optimization.chkdsk.errors += $ErrorMsg
|
||||||
|
Write-Host $ErrorMsg
|
||||||
|
}
|
||||||
|
|
||||||
|
# Run SFC
|
||||||
|
Write-Host "=========================================="
|
||||||
|
Write-Host "Running System File Checker (SFC /scannow)..."
|
||||||
|
Write-Host "=========================================="
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = Start-ProcessWithLiveOutput -FilePath "sfc" -Arguments "/scannow" -Name "sfc"
|
||||||
|
$ExitCode = $result.ExitCode
|
||||||
|
$outputText = $result.Output
|
||||||
|
|
||||||
|
$ScriptResult.system_optimization.sfc.run = $true
|
||||||
|
$ScriptResult.system_optimization.sfc.exit_code = $ExitCode
|
||||||
|
|
||||||
|
switch ($ExitCode) {
|
||||||
|
0 { $ScriptResult.system_optimization.sfc.success = $true; $ScriptResult.system_optimization.sfc.message = "SFC completed successfully."; $ScriptResult.system_optimization.sfc.files_repaired = 0 }
|
||||||
|
1 { $ScriptResult.system_optimization.sfc.success = $true; $ScriptResult.system_optimization.sfc.message = "SFC completed successfully. Corrupted files were repaired."; $ScriptResult.system_optimization.sfc.files_repaired = 1 }
|
||||||
|
2 { $ScriptResult.system_optimization.sfc.success = $false; $ScriptResult.system_optimization.sfc.message = "SFC could not perform the operation." }
|
||||||
|
3 { $ScriptResult.system_optimization.sfc.success = $false; $ScriptResult.system_optimization.sfc.message = "SFC could not fix some files."; $ScriptResult.system_optimization.sfc.files_repaired = -1 }
|
||||||
|
-1 { $ScriptResult.system_optimization.sfc.success = $false; $ScriptResult.system_optimization.sfc.message = "SFC encountered an error." }
|
||||||
|
default { $ScriptResult.system_optimization.sfc.success = $false; $ScriptResult.system_optimization.sfc.message = "SFC completed with exit code: $ExitCode" }
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "SFC exit code: $ExitCode"
|
||||||
|
|
||||||
|
} catch {
|
||||||
|
$ErrorMsg = "Failed to run SFC: $($_.Exception.Message)"
|
||||||
|
$ScriptResult.system_optimization.sfc.errors += $ErrorMsg
|
||||||
|
Write-Host $ErrorMsg
|
||||||
|
}
|
||||||
|
|
||||||
|
# Run DISM
|
||||||
|
Write-Host "=========================================="
|
||||||
|
Write-Host "Running DISM /Online /Cleanup-Image /RestoreHealth..."
|
||||||
|
Write-Host "=========================================="
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = Start-ProcessWithLiveOutput -FilePath "DISM" -Arguments "/Online /Cleanup-Image /RestoreHealth" -Name "dism"
|
||||||
|
$ExitCode = $result.ExitCode
|
||||||
|
$outputText = $result.Output
|
||||||
|
|
||||||
|
$ScriptResult.system_optimization.dism.run = $true
|
||||||
|
$ScriptResult.system_optimization.dism.exit_code = $ExitCode
|
||||||
|
|
||||||
|
switch ($ExitCode) {
|
||||||
|
0 { $ScriptResult.system_optimization.dism.success = $true; $ScriptResult.system_optimization.dism.component_repaired = $true; $ScriptResult.system_optimization.dism.message = "DISM completed successfully." }
|
||||||
|
87 { $ScriptResult.system_optimization.dism.success = $false; $ScriptResult.system_optimization.dism.message = "DISM failed. A parameter was invalid." }
|
||||||
|
3010 { $ScriptResult.system_optimization.dism.success = $true; $ScriptResult.system_optimization.dism.component_repaired = $true; $ScriptResult.system_optimization.dism.message = "DISM completed successfully. A reboot is required." }
|
||||||
|
default {
|
||||||
|
if ($ExitCode -ge 0 -and $ExitCode -le 100) {
|
||||||
|
$ScriptResult.system_optimization.dism.success = $true
|
||||||
|
$ScriptResult.system_optimization.dism.message = "DISM completed with exit code: $ExitCode"
|
||||||
|
} else {
|
||||||
|
$ScriptResult.system_optimization.dism.success = $false
|
||||||
|
$ScriptResult.system_optimization.dism.message = "DISM completed with exit code: $ExitCode"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "DISM exit code: $ExitCode"
|
||||||
|
|
||||||
|
} catch {
|
||||||
|
$ErrorMsg = "Failed to run DISM: $($_.Exception.Message)"
|
||||||
|
$ScriptResult.system_optimization.dism.errors += $ErrorMsg
|
||||||
|
Write-Host $ErrorMsg
|
||||||
|
}
|
||||||
|
|
||||||
|
# Output JSON result
|
||||||
|
Write-Host "=========================================="
|
||||||
|
Write-Host "System optimization section completed."
|
||||||
|
Write-Host "=========================================="
|
||||||
|
$result = $ScriptResult | ConvertTo-Json -Compress -Depth 10
|
||||||
|
Write-Output $result
|
||||||
Loading…
Add table
Add a link
Reference in a new issue