If ( Is64Bit ) { $BlueScreenView = $Global:ToolsDirectory + '\BlueScreenView-x64.exe' } Else { $BlueScreenView = $Global:ToolsDirectory + '\BlueScreenView-x86.exe' }
If ( !(Test-Path $BlueScreenView) ) { Write-Error "File does not exist: ${BlueScreenView}" ; Return }

# Remove existing BSOD report
$BSODReport = $Global:OutputDirectory + '\BSODReport.txt'
If ( Test-Path $BSODReport ) { Write-Output "Deleting existing BSOD report: ${BSODReport}" ; Remove-Item $BSODReport -Force -ErrorAction SilentlyContinue }

# Run BlueScreenView
Start-Process $BlueScreenView -ArgumentList "/stext ${BSODReport} /sort ~1" -Wait

# Wait for BSOD report
For ($i = 0; $i -lt 1200; $i++) {
    If ( Test-Path $BSODReport ) { Break }
    Start-Sleep -Seconds 1
}

# Exit if the BSOD report was not created
If ( !(Test-Path $BSODReport) ) { Write-Output "BSOD report was not found: ${BSODReport}" ; Return }

# Exit if the BSOD report is empty
If ( (Get-Content -Path $BSODReport).Length -eq 0 ) {
    Write-Output "BSOD report is empty: ${BSODReport}"
    Remove-Item $BSODReport -Force
    Return
}

# Create ticket
$BSODReportContent = $(Get-Content -Path $BSODReport) -join "`n"
New-RMMTicket -Subject "System Crash on ${Env:COMPUTERNAME}" -InitialIssue $BSODReportContent

# Update the ticket
New-TicketComment -Comment "It looks like your computer crashed. I'll get this resolved as soon as possible."

# Wait a bit
Start-Sleep -Seconds 120

# Setup providers for matching BSOD report content
$ProviderMicrosoft = 'Microsoft|Windows'

# Get the provider data from the most recent BSOD
$CulpritProvider = $BSODReportContent[12..13] -join '`n'

Switch ( $CulpritProvider ) {
    { $_ -match $ProviderMicrosoft } {
        New-TicketComment -Comment "I've analyzed the crash report and it indicates a problem with a Windows component. I'll repair the system now."
        Source-PSScript -ScriptName "Fix-WindowsHealth"
    }

}
