Files
management-scripts/Fix-WindowsHealth.ps1
T

63 lines
3.6 KiB
PowerShell

# Check the RMM runtime variable to see if it's set to 'null', if not then set the $TicketID
If ( $TicketNumber.Trim().ToLower() -ne 'null' ) { $TicketID = $TicketNumber }
# Run SFC to check the filesystem, convert the result from Unicode to UTF8, delete the intermediate $tempFile
$tempFile = New-TemporaryFile
$(sfc.exe /scannow) | Out-File $tempFile
Set-Content -Path $tempFile -Value $(Get-Content -Path $tempFile -Encoding Unicode) -Encoding UTF8
$SFCOutput = Get-Content -Path $tempFile
$TicketComment = "SFC Output:`n"
ForEach ( $line in $SFCOutput ) {
If ( $line -notmatch 'Verification \d{1,2}% complete' ) { $TicketComment = $TicketComment + $line + "`n" }
}
If ( $TicketID ) { Create-Syncro-Ticket-Comment -TicketIdOrNumber $TicketID -Subject "Update" -Body $TicketComment -Hidden $true -DoNotEmail $true }
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
# 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 }
# Run enhanced DISM for Windows 8 and higher endpoints
If ( IsWindowsVersion -ge "6.2" ) {
# Record start of DISM repair
$BeginDISMRun = (Get-Date)
Write-Output "Beginning repair of Windows Component Store"
Start-Process "dism.exe" -ArgumentList "/online /cleanup-image /restorehealth" -Wait
Write-Output "Beginning cleanup of superceded components in Windows Component Store"
Start-Process "dism.exe" -ArgumentList "/online /cleanup-image /startcomponentcleanup" -Wait
# Record end of DISM repair
$EndDISMRun = (Get-Date)
# Extract results of DISM repair from system log and add them to this log
Write-Output "Beginning Windows Component Store log data collection"
Get-Content -Path "${Env:WinDir}\Logs\DISM\dism.log" | Select-String -Pattern '^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})' |
ForEach-Object {
If ( ((Get-Date $_.Line.Substring(0,19)) -gt $BeginDISMRun) -and ((Get-Date $_.Line.Substring(0,19)) -le $EndDISMRun) ) {
Write-Output $_.Line
}
}
Write-Output "Completed Windows Component Store log data collection"
}
Else { Write-Output "Online image cleanup and restoration is only supported on Windows 8 and higher" }
# Schedule Check Disk if needed
If ( $ScheduleCheckDisk ) {
Write-Output "Scheduling disk repair at next boot"
Start-Process "cmd.exe" -ArgumentList '/C echo y | chkdsk /F /R /X' -Wait
If ( $TicketID ) {
$TicketComment = "Scheduled disk repair at next boot"
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
}
} Else {
If ( $TicketID ) {
$TicketComment = "Disk check was not scheduled to run, as no Windows integrity violations were found."
Create-Syncro-Ticket-Comment -TicketIdOrNumber $TicketID -Subject "Update" -Body $TicketComment -Hidden $true -DoNotEmail $true
$TicketComment = "Your computer has been repaired. Please restart as soon as you're able."
Create-Syncro-Ticket-Comment -TicketIdOrNumber $TicketID -Subject "Update" -Body $TicketComment -Hidden $false -DoNotEmail $false
}
}