Files
management-scripts/Fix-WindowsHealth.ps1
T

53 lines
2.9 KiB
PowerShell
Raw Normal View History

2023-10-05 13:11:38 -04:00
# Run SFC to check the filesystem, convert the result from Unicode to UTF8, delete the intermediate $tempFile
2023-10-06 14:16:19 -04:00
$tempFile = New-TemporaryFile
2023-10-05 13:11:38 -04:00
$(sfc.exe /scannow) | Out-File $tempFile
Set-Content -Path $tempFile -Value $(Get-Content -Path $tempFile -Encoding Unicode) -Encoding UTF8
$SFCOutput = Get-Content -Path $tempFile
2024-06-14 21:22:16 -04:00
$TicketComment = "SFC Output:`n"
ForEach ( $line in $SFCOutput ) {
If ( $line -notmatch '^Verification \d{1,3}% complete\.$' ) { $TicketComment = $TicketComment + $line + "`n" }
}
2024-06-14 16:41:02 -04:00
If ( $TicketID ) { Create-Syncro-Ticket-Comment -TicketIdOrNumber $TicketID -Subject "Update" -Body $TicketComment -Hidden $true -DoNotEmail $true }
2023-10-05 13:11:38 -04:00
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
2022-11-23 15:52:30 -05:00
2023-10-05 13:11:38 -04:00
# Examine $SFCOutput for integrity violations, if found make sure chkdsk runs at the next boot
If ( $SFCOutput -contains 'Windows Resource Protection did not find any integrity violations.' ) { $ScheduleCheckDisk = $false } Else { $ScheduleCheckDisk = $true }
2022-11-23 15:52:30 -05:00
2022-06-30 16:45:58 -04:00
# Run enhanced DISM for Windows 8 and higher endpoints
2024-06-14 16:41:02 -04:00
If ( IsWindowsVersion -ge "6.2" ) {
2023-10-05 14:10:02 -04:00
# Record start of DISM repair
2023-10-05 13:11:38 -04:00
$BeginDISMRun = (Get-Date)
2020-09-09 21:06:44 -04:00
2023-10-05 13:11:38 -04:00
Write-Output "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
2023-10-05 13:11:38 -04:00
Write-Output "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
2023-10-05 14:10:02 -04:00
# Record end of DISM repair
$EndDISMRun = (Get-Date)
2020-09-09 21:06:44 -04:00
2023-10-05 14:10:02 -04:00
# Extract results of DISM repair from system log and add them to this log
2023-10-05 13:11:38 -04:00
Write-Output "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})' |
2024-06-14 16:41:02 -04:00
ForEach-Object {
If ( ((Get-Date $_.Line.Substring(0,19)) -gt $BeginDISMRun) -and ((Get-Date $_.Line.Substring(0,19)) -le $EndDISMRun) ) {
2024-06-14 21:22:16 -04:00
Write-Output $_.Line
2019-11-11 19:22:39 -05:00
}
}
2023-10-05 13:11:38 -04:00
Write-Output "Completed Windows Component Store log data collection"
2019-11-11 19:22:39 -05:00
}
2023-10-05 13:11:38 -04:00
Else { Write-Output "Online image cleanup and restoration is only supported on Windows 8 and higher" }
2022-11-23 15:52:30 -05:00
# Schedule Check Disk if needed
If ( $ScheduleCheckDisk ) {
2023-10-05 13:35:12 -04:00
Write-Output "Scheduling disk repair at next boot"
Start-Process "cmd.exe" -ArgumentList '/C echo y | chkdsk /F /R /X' -Wait
2024-06-14 16:41:02 -04:00
$TicketComment = "Scheduled disk repair at next boot"
If ( $TicketID ) {
Create-Syncro-Ticket-Comment -TicketIdOrNumber $TicketID -Subject "Update" -Body $TicketComment -Hidden $true -DoNotEmail $true
$TicketComment = "I've made some changes that will require you to restart your computer.`n`nWhenver you're ready, please restart your computer and allow it 30min to complete the startup process."
Create-Syncro-Ticket-Comment -TicketIdOrNumber $TicketID -Subject "Update" -Body $TicketComment -Hidden $false -DoNotEmail $false
}
2022-11-23 15:52:30 -05:00
}