# 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" }
}
New-TicketComment -Comment $TicketComment -Hidden -DoNotEmail
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue

# Examine $SFCOutput for integrity violations, if found make sure chkdsk runs at the next boot
Switch ( $SFCOutput ) {
    { $_ -match 'Windows Resource Protection found integrity violations' } { $ScheduleCheckDisk = $true }
    { $_ -match 'Windows Resource Protection did not find any integrity violations.' } { $ScheduleCheckDisk = $false }
    { $_ -match 'There is a system repair pending which requires reboot to complete.' } {
        $ScheduleCheckDisk = $false
        New-TicketComment -Comment "System repair is already pending. Reboot needs to occur first, then SFC should be run again." -Hidden -DoNotEmail
    }
}

# Run enhanced DISM for Windows 8 and higher endpoints
$DISMRun = $false
If ( IsWindowsVersion -ge "6.2" ) {

    # Record start of DISM repair
    $BeginDISMRun = (Get-Date)

    Try {
        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

        $DISMRun = $true
    }
    Catch { Write-Error $_.Exception.Message }

    # 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
    New-TicketComment -Comment "Scheduled disk repair at next boot" -Hidden -DoNotEmail
    New-TicketComment -Comment "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."
} Else {
    New-TicketComment -Comment "Disk check was not scheduled to run, as no Windows integrity violations were found." -Hidden -DoNotEmail
    New-TicketComment -Comment "Your computer has been repaired. Please restart as soon as you're able."
}

# Bill ticket if action was taken
If ( $DISMRun -or $ScheduleCheckDisk ) { New-TicketTimerEntry -Comment "Corruption was found and has been repaired." }