A Tela Azul da Morte (BSOD) é um termo que causa arrepios em muitos usuários de computador. É uma tela de erro de parada exibida em um sistema de computador Windows após um erro fatal do sistema. Geralmente causadas por problemas de hardware ou driver, as BSODs também podem ser acionadas por erros de software, o que leva a uma interrupção abrupta das operações do sistema. Entender e diagnosticar esses erros é fundamental na área de TI, e este artigo se aprofunda em um script do PowerShell desenvolvido para detectar e registrar esses desligamentos inesperados.
Histórico
O script utiliza a ferramenta BlueScreenView da Nirsoft, um utilitário projetado especificamente para analisar arquivos minidump gerados durante uma BSOD. Para profissionais de TI e provedores de serviços gerenciados (MSPs), a automação do processo de detecção e análise desses arquivos minidump é inestimável, oferecendo uma abordagem sistemática para a solução de problemas.
O roteiro
#Requires -Version 5.1
<#
.SYNOPSIS
Conditional script for detecting BSOD's. Uses BlueScreenView from Nirsoft.
.DESCRIPTION
Conditional script for detecting BSOD's. Uses BlueScreenView from Nirsoft.
Will always show the number of Unexpected shutdowns if system is setup to log those events.
This doesn't always mean that there was a BSOD as this includes things like holding the power button or pressing the rest button.
When a mini dump is detected in C:WindowsMinidump then this will output the results and exit with an exit code of 1.
When none have been found then this will exit with an exit code of 0.
When it couldn't download or extract BlueScreenView then this will exit with an exit code of 2.
.OUTPUTS
None
.NOTES
This should be the default, but in case this was modified instructions below.
Minimal Setup:
Open System Properties.
Click on Settings under Startup and Recovery.
Make sure that "Write an event to the system log" is checked.
Under System failure change to "Write debugging information" to Automatic memory dump.
Minimum OS Architecture Supported: Windows 10, Windows Server 2016
Release Notes:
Initial Release
(c) 2023 NinjaOne
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider.
Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>
[CmdletBinding()]
param ()
process {
if (-not (Test-IsElevated)) {
Write-Error -Message "Access Denied. Please run with Administrator privileges."
exit 1
}
# Get unexpected shutdown events from System log
$UnexpectedShutdownEvents = Get-WinEvent -FilterHashtable @{LogName = 'System'; ID = 6008 }
if ($UnexpectedShutdownEvents) {
Write-Host "Unexpected shutdowns found: $($UnexpectedShutdownEvents.Count)"
Write-Host ""
}
# Check if any minidumps exist and exit if none are found
if (-not $(Get-ChildItem -Path "C:WindowsMinidump" -ErrorAction SilentlyContinue)) {
Write-Host "No mini dumps found."
exit 0
}
# Download Blue Screen View, run, and export results to a csv file
try {
Invoke-WebRequest -Uri $BlueScreenViewUrl -OutFile $ZipPath -ErrorAction Stop
Expand-Archive -Path $ZipPath -DestinationPath $ENV:Temp -Force -ErrorAction Stop
Start-Process -FilePath $ExePath -ArgumentList "/scomma ""$CsvPath""" -Wait -ErrorAction Stop
}
catch {
Write-Host "Blue Screen View Command has Failed: $($_.Exception.Message)"
# Clean Up
Remove-DownloadedFiles -Path $CsvPath, $ZipPath, $ExePath, "$($ENV:Temp)BlueScreenView.chm", "$($ENV:Temp)readme.txt"
exit 2
}
# Convert the CSV to an array of objects
$MiniDumps = Get-Content -Path $CsvPath |
ConvertFrom-Csv -Delimiter ',' -Header $Header |
Select-Object -Property @{
'n' = "Timestamp";
'e' = { [DateTime]::Parse($_.timestamp, [System.Globalization.CultureInfo]::CurrentCulture) }
}, Dumpfile, Reason, Errorcode, CausedByDriver
# Clean Up
Remove-DownloadedFiles -Path $CsvPath, $ZipPath, $ExePath, "$($ENV:Temp)BlueScreenView.chm", "$($ENV:Temp)readme.txt"
# Output the results
$MiniDumps | Out-String | Write-Host
if ($MiniDumps) {
exit 1
}
exit 0
}
begin {
function Test-IsElevated {
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
$p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Remove-DownloadedFiles {
param([string[]]$Path)
process { Remove-Item -Path $Path -Force -ErrorAction SilentlyContinue }
}
# CSV Headers
$Header = @(
"Dumpfile"
"Timestamp"
"Reason"
"Errorcode"
"Parameter1"
"Parameter2"
"Parameter3"
"Parameter4"
"CausedByDriver"
)
# Build path variables
$CsvFileName = "bluescreenview-export.csv"
$BlueScreenViewZip = "bluescreenview.zip"
$BlueScreenViewExe = "BlueScreenView.exe"
$BlueScreenViewUrl = "https://www.nirsoft.net/utils/$BlueScreenViewZip"
$ZipPath = Join-Path -Path $ENV:Temp -ChildPath $BlueScreenViewZip
$ExePath = Join-Path -Path $ENV:Temp -ChildPath $BlueScreenViewExe
$CsvPath = Join-Path -Path $ENV:Temp -ChildPath $CsvFileName
}
end {}
Detalhamento
- Pré-requisitos: O script requer a versão 5.1 do PowerShell e foi projetado para o Windows 10 e o Windows Server 2016.
- Inicialização: Ele começa garantindo que tenha privilégios de administrador, essenciais para acessar os logs do sistema e os arquivos de minidump.
- Paralisações inesperadas: Em seguida, o script verifica se há eventos inesperados de desligamento no registro do sistema.
- Verificação de minidump: Ele procura por arquivos de minidump no diretório designado.
- Integração com o BlueScreenView: Se forem detectados arquivos minidump, o script fará o download e executará o BlueScreenView, exportando os resultados para um arquivo CSV.
- Análise de dados: Os dados CSV são convertidos em uma matriz de objetos para exibição pelo usuário.
- Limpeza: Após a análise, todos os arquivos baixados ou extraídos são removidos.
Casos de uso em potencial
Considere um profissional de TI, Alex, que foi alertado sobre BSODs recorrentes no departamento de design de uma empresa. Em vez de examinar manualmente cada sistema, Alex implementa esse script. Em instantes, ele identifica um driver específico que está causando as BSODs, permitindo uma solução direcionada. Esse script, portanto, torna-se uma ferramenta poderosa em seu arsenal de solução de problemas.
Comparações
Embora o Windows Event Viewer ofereça insights, ele não possui a análise detalhada de minidump que o BlueScreenView fornece. Esse script preenche essa lacuna com eficiência, oferecendo uma abordagem mais abrangente do que os métodos manuais.
Implicações de segurança
As BSODs não são apenas um incômodo; elas podem ser um problema de segurança. Uma BSOD recorrente pode ser um sinal de que uma entidade mal-intencionada está tentando comprometer o sistema. Ao registrar e analisar esses eventos, os profissionais de TI podem identificar possíveis ameaças e garantir a segurança do sistema.
Recomendações
- Sempre execute o script com privilégios de administrador para garantir a funcionalidade total.
- Atualize regularmente a ferramenta BlueScreenView para aproveitar seus recursos mais recentes e garantir a compatibilidade.
- Monitore os sistemas quanto a BSODs recorrentes, pois eles podem indicar problemas de segurança mais profundos.
Considerações finais
No mundo dinâmico da TI, ferramentas como o NinjaOne, quando combinadas com scripts como o discutido, capacitam os profissionais a manter a integridade e a segurança do sistema. A detecção e a análise de logs de BSOD e desligamentos inesperados tornam-se um processo simplificado, garantindo operações eficientes e seguras.