diff --git a/optimization.ps1 b/optimization.ps1 index 6a578af..ee49d24 100644 --- a/optimization.ps1 +++ b/optimization.ps1 @@ -1,45 +1,76 @@ -$ScriptResult.system_optimization = @{ - "chkdsk" = @{ - "run" = $false - "success" = $false - "exit_code" = $null - "message" = "" - "reboot_required" = $false - "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" = @() +$ScriptResult = @{ + 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" = @() + } } } +# Temp files for output capture +$tempPath = $env:TEMP +$chkdskStdout = "$tempPath\chkdsk_stdout.txt" +$chkdskStderr = "$tempPath\chkdsk_stderr.txt" +$sfcStdout = "$tempPath\sfc_stdout.txt" +$sfcStderr = "$tempPath\sfc_stderr.txt" +$dismStdout = "$tempPath\dism_stdout.txt" +$dismStderr = "$tempPath\dism_stderr.txt" + # Run CHKDSK Write-Host "Running CHKDSK with /R option..." Write-Host "Note: If volume is in use, CHKDSK will be scheduled for next reboot." + try { - # Use cmd /c echo Y | to automatically answer "Y" to any prompts - $ChkdskOutput = cmd /c "echo Y | chkdsk C: /R" 2>&1 | Out-String - $ChkdskExitCode = $LASTEXITCODE + # Use cmd /c with echo Y piped, redirect output + $process = Start-Process -FilePath "cmd" -ArgumentList "/c", "echo Y | chkdsk C: /R" -NoNewWindow -Wait -PassThru -RedirectStandardOutput $chkdskStdout -RedirectStandardError $chkdskStderr -ErrorAction Stop + $ChkdskExitCode = $process.ExitCode + + # Read output + $outputText = "" + if (Test-Path $chkdskStdout) { + $outputText = Get-Content -Path $chkdskStdout -Raw -ErrorAction SilentlyContinue + } + if (Test-Path $chkdskStderr) { + $stderrText = Get-Content -Path $chkdskStderr -Raw -ErrorAction SilentlyContinue + if ($stderrText) { + $outputText += $stderrText + } + } $ScriptResult.system_optimization.chkdsk.run = $true $ScriptResult.system_optimization.chkdsk.exit_code = $ChkdskExitCode - # Parse output for scheduled check - if ($ChkdskOutput -match "will be checked on next boot|Chkdsk cannot run because the volume is in use") { + # Parse output + 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 - Write-Host "CHKDSK scheduled for next system restart." + } + + # Parse disk space freed (if available in output) + if ($outputText -match "(\d+) bytes in (\d+) data streams recovered") { + $bytesRecovered = [int64]$matches[1] + $ScriptResult.system_optimization.chkdsk.diskSpace = "{0:N2} MB" -f ($bytesRecovered / 1MB) } # Parse exit codes @@ -58,9 +89,7 @@ try { } 3 { $ScriptResult.system_optimization.chkdsk.success = $true - if (-not $ScriptResult.system_optimization.chkdsk.reboot_required) { - $ScriptResult.system_optimization.chkdsk.reboot_required = $true - } + $ScriptResult.system_optimization.chkdsk.reboot_required = $true $ScriptResult.system_optimization.chkdsk.message = "CHKDSK completed with errors and requires a reboot to complete." } 4 { @@ -69,9 +98,7 @@ try { } 5 { $ScriptResult.system_optimization.chkdsk.success = $false - if (-not $ScriptResult.system_optimization.chkdsk.reboot_required) { - $ScriptResult.system_optimization.chkdsk.reboot_required = $true - } + $ScriptResult.system_optimization.chkdsk.reboot_required = $true $ScriptResult.system_optimization.chkdsk.message = "CHKDSK found errors but requires a reboot to fix them." } default { @@ -80,13 +107,10 @@ try { } } + Write-Host $outputText Write-Host "CHKDSK exit code: $ChkdskExitCode" Write-Host "CHKDSK message: $($ScriptResult.system_optimization.chkdsk.message)" - Write-Host "CHKDSK output: $ChkdskOutput" - if ($ScriptResult.system_optimization.chkdsk.reboot_required) { - Write-Host "System reboot is required to complete CHKDSK." - } } catch { $ErrorMsg = "Failed to run CHKDSK: $($_.Exception.Message)" $ScriptResult.system_optimization.chkdsk.errors += $ErrorMsg @@ -95,13 +119,33 @@ try { # Run SFC Write-Host "Running System File Checker (SFC /scannow)..." + try { - $SfcProcess = Start-Process -FilePath "sfc" -ArgumentList "/scannow" -NoNewWindow -Wait -PassThru -ErrorAction Stop - $ExitCode = $SfcProcess.ExitCode + $process = Start-Process -FilePath "sfc" -ArgumentList "/scannow" -NoNewWindow -Wait -PassThru -RedirectStandardOutput $sfcStdout -RedirectStandardError $sfcStderr -ErrorAction Stop + $ExitCode = $process.ExitCode + + # Read output + $outputText = "" + if (Test-Path $sfcStdout) { + $outputText = Get-Content -Path $sfcStdout -Raw -ErrorAction SilentlyContinue + } + if (Test-Path $sfcStderr) { + $stderrText = Get-Content -Path $sfcStderr -Raw -ErrorAction SilentlyContinue + if ($stderrText) { + $outputText += $stderrText + } + } $ScriptResult.system_optimization.sfc.run = $true $ScriptResult.system_optimization.sfc.exit_code = $ExitCode + # Parse files repaired from output + if ($outputText -match "Windows Resource Protection found integrity violations|found corrupt files and repaired them") { + if ($outputText -match "repairing corrupted files") { + $ScriptResult.system_optimization.sfc.files_repaired = 1 + } + } + # Parse exit codes switch ($ExitCode) { 0 { @@ -123,7 +167,7 @@ try { $ScriptResult.system_optimization.sfc.message = "SFC could not fix some files. See CBS.log for details." $ScriptResult.system_optimization.sfc.files_repaired = -1 } - -1 { # Often shows as 0xFFFFFFFF or -1 + -1 { $ScriptResult.system_optimization.sfc.success = $false $ScriptResult.system_optimization.sfc.message = "SFC encountered an error. Check CBS.log for details." } @@ -133,9 +177,10 @@ try { } } + Write-Host $outputText Write-Host "SFC exit code: $ExitCode" Write-Host "SFC message: $($ScriptResult.system_optimization.sfc.message)" - Write-Host "Files repaired: $($ScriptResult.system_optimization.sfc.files_repaired)" + } catch { $ErrorMsg = "Failed to run SFC: $($_.Exception.Message)" $ScriptResult.system_optimization.sfc.errors += $ErrorMsg @@ -144,9 +189,22 @@ try { # Run DISM Write-Host "Running DISM /Online /Cleanup-Image /RestoreHealth..." + try { - $DismProcess = Start-Process -FilePath "DISM" -ArgumentList "/Online /Cleanup-Image /RestoreHealth" -NoNewWindow -Wait -PassThru -ErrorAction Stop - $ExitCode = $DismProcess.ExitCode + $process = Start-Process -FilePath "DISM" -ArgumentList "/Online /Cleanup-Image /RestoreHealth" -NoNewWindow -Wait -PassThru -RedirectStandardOutput $dismStdout -RedirectStandardError $dismStderr -ErrorAction Stop + $ExitCode = $process.ExitCode + + # Read output + $outputText = "" + if (Test-Path $dismStdout) { + $outputText = Get-Content -Path $dismStdout -Raw -ErrorAction SilentlyContinue + } + if (Test-Path $dismStderr) { + $stderrText = Get-Content -Path $dismStderr -Raw -ErrorAction SilentlyContinue + if ($stderrText) { + $outputText += $stderrText + } + } $ScriptResult.system_optimization.dism.run = $true $ScriptResult.system_optimization.dism.exit_code = $ExitCode @@ -186,17 +244,26 @@ try { } } + Write-Host $outputText Write-Host "DISM exit code: $ExitCode" Write-Host "DISM message: $($ScriptResult.system_optimization.dism.message)" - if ($ExitCode -eq 3010) { - Write-Host "System reboot is recommended after DISM." - } } catch { $ErrorMsg = "Failed to run DISM: $($_.Exception.Message)" $ScriptResult.system_optimization.dism.errors += $ErrorMsg Write-Host $ErrorMsg } +# Cleanup temp files +Write-Host "Cleaning up temporary files..." +$tempFiles = @($chkdskStdout, $chkdskStderr, $sfcStdout, $sfcStderr, $dismStdout, $dismStderr) +foreach ($file in $tempFiles) { + if (Test-Path $file) { + Remove-Item $file -ErrorAction SilentlyContinue + } +} + +# Output JSON result Write-Host "System optimization section completed." -"" | Out-File -FilePath $LogFile -Append -Encoding UTF8 \ No newline at end of file +$result = $ScriptResult | ConvertTo-Json -Compress -Depth 10 +Write-Output $result \ No newline at end of file