-
This commit is contained in:
parent
94b344f59c
commit
ee9b6e0fdf
1 changed files with 21 additions and 9 deletions
30
cleanup.ps1
30
cleanup.ps1
|
|
@ -671,16 +671,23 @@ $ScriptResult.system_optimization = @{
|
||||||
|
|
||||||
# Run CHKDSK
|
# Run CHKDSK
|
||||||
Write-Log "Running CHKDSK with /R option..." -Level "INFO"
|
Write-Log "Running CHKDSK with /R option..." -Level "INFO"
|
||||||
Write-Log "Note: CHKDSK may schedule a disk check on next reboot." -Level "WARNING"
|
Write-Log "Note: If volume is in use, CHKDSK will be scheduled for next reboot." -Level "WARNING"
|
||||||
try {
|
try {
|
||||||
$ChkdskProcess = Start-Process -FilePath "chkdsk" -ArgumentList "/R" -NoNewWindow -Wait -PassThru -ErrorAction Stop
|
# Use cmd /c echo Y | to automatically answer "Y" to any prompts
|
||||||
$ExitCode = $ChkdskProcess.ExitCode
|
$ChkdskOutput = cmd /c "echo Y | chkdsk C: /R" 2>&1 | Out-String
|
||||||
|
$ChkdskExitCode = $LASTEXITCODE
|
||||||
|
|
||||||
$ScriptResult.system_optimization.chkdsk.run = $true
|
$ScriptResult.system_optimization.chkdsk.run = $true
|
||||||
$ScriptResult.system_optimization.chkdsk.exit_code = $ExitCode
|
$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") {
|
||||||
|
$ScriptResult.system_optimization.chkdsk.reboot_required = $true
|
||||||
|
Write-Log "CHKDSK scheduled for next system restart." -Level "INFO"
|
||||||
|
}
|
||||||
|
|
||||||
# Parse exit codes
|
# Parse exit codes
|
||||||
switch ($ExitCode) {
|
switch ($ChkdskExitCode) {
|
||||||
0 {
|
0 {
|
||||||
$ScriptResult.system_optimization.chkdsk.success = $true
|
$ScriptResult.system_optimization.chkdsk.success = $true
|
||||||
$ScriptResult.system_optimization.chkdsk.message = "CHKDSK completed successfully. No errors found."
|
$ScriptResult.system_optimization.chkdsk.message = "CHKDSK completed successfully. No errors found."
|
||||||
|
|
@ -695,7 +702,9 @@ try {
|
||||||
}
|
}
|
||||||
3 {
|
3 {
|
||||||
$ScriptResult.system_optimization.chkdsk.success = $true
|
$ScriptResult.system_optimization.chkdsk.success = $true
|
||||||
$ScriptResult.system_optimization.chkdsk.reboot_required = $true
|
if (-not $ScriptResult.system_optimization.chkdsk.reboot_required) {
|
||||||
|
$ScriptResult.system_optimization.chkdsk.reboot_required = $true
|
||||||
|
}
|
||||||
$ScriptResult.system_optimization.chkdsk.message = "CHKDSK completed with errors and requires a reboot to complete."
|
$ScriptResult.system_optimization.chkdsk.message = "CHKDSK completed with errors and requires a reboot to complete."
|
||||||
}
|
}
|
||||||
4 {
|
4 {
|
||||||
|
|
@ -704,17 +713,20 @@ try {
|
||||||
}
|
}
|
||||||
5 {
|
5 {
|
||||||
$ScriptResult.system_optimization.chkdsk.success = $false
|
$ScriptResult.system_optimization.chkdsk.success = $false
|
||||||
$ScriptResult.system_optimization.chkdsk.reboot_required = $true
|
if (-not $ScriptResult.system_optimization.chkdsk.reboot_required) {
|
||||||
|
$ScriptResult.system_optimization.chkdsk.reboot_required = $true
|
||||||
|
}
|
||||||
$ScriptResult.system_optimization.chkdsk.message = "CHKDSK found errors but requires a reboot to fix them."
|
$ScriptResult.system_optimization.chkdsk.message = "CHKDSK found errors but requires a reboot to fix them."
|
||||||
}
|
}
|
||||||
default {
|
default {
|
||||||
$ScriptResult.system_optimization.chkdsk.success = $false
|
$ScriptResult.system_optimization.chkdsk.success = $false
|
||||||
$ScriptResult.system_optimization.chkdsk.message = "CHKDSK completed with unexpected exit code: $ExitCode"
|
$ScriptResult.system_optimization.chkdsk.message = "CHKDSK completed with exit code: $ChkdskExitCode"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Write-Log "CHKDSK exit code: $ExitCode" -Level "INFO"
|
Write-Log "CHKDSK exit code: $ChkdskExitCode" -Level "INFO"
|
||||||
Write-Log "CHKDSK message: $($ScriptResult.system_optimization.chkdsk.message)" -Level "INFO"
|
Write-Log "CHKDSK message: $($ScriptResult.system_optimization.chkdsk.message)" -Level "INFO"
|
||||||
|
Write-Log "CHKDSK output: $ChkdskOutput" -Level "INFO"
|
||||||
|
|
||||||
if ($ScriptResult.system_optimization.chkdsk.reboot_required) {
|
if ($ScriptResult.system_optimization.chkdsk.reboot_required) {
|
||||||
Write-Log "System reboot is required to complete CHKDSK." -Level "WARNING"
|
Write-Log "System reboot is required to complete CHKDSK." -Level "WARNING"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue