Files
management-scripts/Fix-WindowsHealth.ps1
T

41 lines
1.9 KiB
PowerShell
Raw Normal View History

2022-11-23 15:52:30 -05:00
$ScheduleCheckDisk = $false
2022-06-30 16:45:58 -04:00
# Run the System File Checker to scan for and repair any problems with protected system files
LogMsg "Running System File Checker"
$SFCOutput = $(sfc /scannow) | Out-String
LogMsg $SFCOutput
2022-11-23 15:52:30 -05:00
$SFCOutputWithSpaces = " W i n d o w s R e s o u r c e P r o t e c t i o n d i d n o t f i n d a n y i n t e g r i t y v i o l a t i o n s ."
$SFCOutputWithoutSpaces = "Windows Resource Protection did not find any integrity violations."
If ( !($SFCOutput -match $SFCOutputWithSpaces) -or !($SFCOutput -match $SFCOutputWithoutSpaces) ) { $ScheduleCheckDisk = $true }
2022-06-30 16:45:58 -04:00
# Run enhanced DISM for Windows 8 and higher endpoints
2020-09-09 21:06:44 -04:00
If ( IsWindowsVersion -ge "6.2" )
{
$BeginDISMRun = $(Get-Date).AddSeconds(-1)
2022-06-30 16:45:58 -04:00
LogMsg "Beginning repair of Windows Component Store"
2022-06-30 17:50:07 -04:00
Start-Process "dism.exe" -ArgumentList "/online /cleanup-image /restorehealth" -Wait
2020-09-09 21:06:44 -04:00
2022-06-30 16:45:58 -04:00
LogMsg "Beginning cleanup of superceded components in Windows Component Store"
2022-07-01 13:14:34 -04:00
Start-Process "dism.exe" -ArgumentList "/online /cleanup-image /startcomponentcleanup" -Wait
2020-09-09 21:06:44 -04:00
$EndDISMRun = $(Get-Date).AddSeconds(1)
2022-06-30 16:45:58 -04:00
# Adding results of DISM log file to this log file
2020-10-22 14:01:35 -04:00
LogMsg "Beginning Windows Component Store log data collection"
2020-09-09 21:06:44 -04:00
Get-Content -Path "${Env:WinDir}\Logs\DISM\dism.log" | Select-String -Pattern '^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})' |
2022-06-30 16:45:58 -04:00
ForEach-Object {
If ( ((Get-Date $_.Line.Substring(0,19)) -gt $BeginDISMRun) -and ((Get-Date $_.Line.Substring(0,19)) -le $EndDISMRun) ) {
LogMsg $_.Line
2019-11-11 19:22:39 -05:00
}
}
2020-10-22 14:01:35 -04:00
LogMsg "Completed Windows Component Store log data collection"
2019-11-11 19:22:39 -05:00
}
2022-11-23 15:52:30 -05:00
Else { LogMsg "Online image cleanup and restoration is only supported on Windows 8 and higher" }
# Schedule Check Disk if needed
If ( $ScheduleCheckDisk ) {
LogMsg "Check Disk scheduled to repair disk at next boot"
& echo y | chkdsk /F /R /X
}