Appearance
故障排查
Windows 系统在使用过程中可能会遇到各种问题,本章介绍常见的故障排查方法和工具。
故障排查概述
故障排查步骤
text
故障排查的基本步骤:
├── 1. 确认问题 - 了解问题的具体表现
├── 2. 收集信息 - 查看错误信息、日志
├── 3. 分析原因 - 根据信息分析可能的原因
├── 4. 制定方案 - 确定解决方案
├── 5. 实施修复 - 执行修复操作
└── 6. 验证结果 - 确认问题是否解决常见问题类型
| 问题类型 | 常见表现 | 排查工具 |
|---|---|---|
| 启动问题 | 无法开机、蓝屏 | 安全模式、启动修复 |
| 性能问题 | 卡顿、慢 | 任务管理器、资源监视器 |
| 网络问题 | 无法上网、断网 | 网络诊断、ping |
| 软件问题 | 程序崩溃、无响应 | 事件查看器、兼容性 |
| 硬件问题 | 设备不识别 | 设备管理器 |
| 系统错误 | 错误提示、异常 | 事件查看器、SFC |
系统诊断工具
事件查看器
事件查看器是查看系统日志的重要工具:
powershell
# 打开事件查看器
eventvwr.msc
# 查看系统事件
Get-WinEvent -LogName System -MaxEvents 20
# 查看应用程序事件
Get-WinEvent -LogName Application -MaxEvents 20
# 查看错误级别的事件
Get-WinEvent -FilterHashtable @{LogName='System'; Level=2} -MaxEvents 10
# 查看特定事件 ID
Get-WinEvent -FilterHashtable @{LogName='System'; ID=7000,7001,7002}
# 查看特定时间范围的事件
$startTime = (Get-Date).AddHours(-24)
Get-WinEvent -FilterHashtable @{LogName='System'; StartTime=$startTime}
# 按关键字搜索事件
Get-WinEvent -LogName System | Where-Object { $_.Message -like "*错误*" }
# 导出事件日志
wevtutil epl System C:\system_log.evtx系统文件检查器(SFC)
SFC 用于检查和修复系统文件:
cmd
:: 扫描系统文件
sfc /scannow
:: 扫描但不修复
sfc /verifyonly
:: 扫描特定文件
sfc /scanfile="C:\Windows\System32\kernel32.dll"
:: 从安装源恢复文件
sfc /scannow /offbootdir=C:\ /offwindir=C:\WindowsDISM 工具
DISM 用于修复 Windows 映像:
cmd
:: 检查系统映像健康状态
dism /online /cleanup-image /checkhealth
:: 扫描系统映像
dism /online /cleanup-image /scanhealth
:: 恢复系统映像
dism /online /cleanup-image /restorehealth
:: 使用本地源恢复
dism /online /cleanup-image /restorehealth /source:WIM:D:\sources\install.wim:1 /limitaccess性能监视器
powershell
# 打开性能监视器
perfmon
# 查看性能计数器
Get-Counter -Counter "\Processor(_Total)\% Processor Time"
Get-Counter -Counter "\Memory\Available MBytes"
Get-Counter -Counter "\PhysicalDisk(_Total)\% Disk Time"
# 创建性能报告
# 性能监视器 → 数据收集器集 → 系统 → 系统性能启动问题排查
安全模式
安全模式以最小配置启动系统:
text
进入安全模式的方法:
方法一:系统配置工具
1. Win + R → msconfig
2. 引导选项卡 → 安全引导 → 最小
3. 重启电脑
方法二:高级启动
1. 设置 → 更新和安全 → 恢复
2. 高级启动 → 立即重启
3. 疑难解答 → 高级选项 → 启动设置
4. 重启 → 按 4 进入安全模式
方法三:命令行
shutdown /r /o /t 0
# 重启后选择:疑难解答 → 高级选项 → 启动设置启动修复
powershell
# 运行启动修复
# 高级启动 → 疑难解答 → 高级选项 → 启动修复
# 使用命令行工具
# 进入 Windows 恢复环境后:
# 检查磁盘
chkdsk C: /f /r
# 修复引导
bootrec /fixmbr
bootrec /fixboot
bootrec /rebuildbcd
# 修复 EFI 引导(UEFI 系统)
bcdboot C:\Windows /s S: /f UEFI
# S: 是 EFI 分区引导配置
cmd
:: 查看引导配置
bcdedit /enum
:: 查看默认引导项
bcdedit /default
:: 设置默认引导项
bcdedit /default {current}
:: 设置引导超时时间
bcdedit /timeout 10
:: 删除引导项
bcdedit /delete {identifier}
:: 导出引导配置
bcdedit /export C:\bcd_backup
:: 导入引导配置
bcdedit /import C:\bcd_backup性能问题排查
CPU 性能问题
powershell
# 查看 CPU 使用率
Get-Counter -Counter "\Processor(_Total)\% Processor Time" -Continuous
# 查看高 CPU 占用进程
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10 Name, CPU, Id
# 查看进程线程
$process = Get-Process -Name "chrome" | Select-Object -First 1
$process.Threads | Select-Object Id, TotalProcessorTime, ThreadState
# 查看进程详情
Get-WmiObject Win32_Process |
Where-Object { $_.Name -eq "chrome.exe" } |
Select-Object Name, ProcessId, CommandLine
# 结束高 CPU 进程
Get-Process -Name "chrome" | Stop-Process -Force内存问题
powershell
# 查看内存使用情况
$os = Get-CimInstance Win32_OperatingSystem
$freeMemory = [math]::Round($os.FreePhysicalMemory / 1MB, 2)
$totalMemory = [math]::Round($os.TotalVisibleMemorySize / 1MB, 2)
Write-Host "总内存: $totalMemory GB, 可用: $freeMemory GB"
# 查看高内存占用进程
Get-Process | Sort-Object WorkingSet -Descending |
Select-Object -First 10 Name, @{N='Memory(MB)';E={[math]::Round($_.WorkingSet/1MB,2)}}
# 清理内存缓存
# 注意:这不会真正释放内存,只是将数据写入页面文件
Clear-Content -Path "C:\Windows\Temp\*" -Force -ErrorAction SilentlyContinue
# 查看页面文件使用
Get-CimInstance Win32_PageFileUsage | Format-List *磁盘性能问题
powershell
# 查看磁盘活动
Get-Counter -Counter "\PhysicalDisk(_Total)\% Disk Time"
# 查看磁盘队列
Get-Counter -Counter "\PhysicalDisk(_Total)\Avg. Disk Queue Length"
# 查看磁盘读写速度
Get-Counter -Counter "\PhysicalDisk(_Total)\Disk Read Bytes/sec"
Get-Counter -Counter "\PhysicalDisk(_Total)\Disk Write Bytes/sec"
# 检查磁盘错误
chkdsk C: /scan
# 优化磁盘(SSD 使用 TRIM,HDD 使用碎片整理)
Optimize-Volume -DriveLetter C -Defrag
Optimize-Volume -DriveLetter C -Trim
# 查看磁盘健康状态
Get-PhysicalDisk | Select-Object FriendlyName, MediaType, HealthStatus网络问题排查
基本网络诊断
powershell
# 检查网络连接
ping 127.0.0.1 # 本地回环
ping 192.168.1.1 # 网关
ping 8.8.8.8 # 外部 IP
ping www.baidu.com # 域名
# 检查 DNS 解析
nslookup www.baidu.com
Resolve-DnsName www.baidu.com
# 跟踪路由
tracert www.baidu.com
Test-NetConnection www.baidu.com -TraceRoute
# 检查端口连通性
Test-NetConnection -ComputerName www.baidu.com -Port 80网络配置检查
powershell
# 查看网络适配器
Get-NetAdapter
# 查看 IP 配置
Get-NetIPConfiguration
ipconfig /all
# 查看 DNS 配置
Get-DnsClientServerAddress
# 查看 DNS 缓存
Get-DnsClientCache
# 清除 DNS 缓存
Clear-DnsClientCache
# 查看路由表
Get-NetRoute
route print
# 查看网络连接
Get-NetTCPConnection | Where-Object State -eq "Established"
netstat -an网络重置
powershell
# 重置网络适配器
netsh winsock reset
# 重置 IP 配置
netsh int ip reset
# 重置防火墙
netsh advfirewall reset
# 刷新 DNS
ipconfig /flushdns
# 释放并重新获取 IP
ipconfig /release
ipconfig /renew
# 完整网络重置后需要重启电脑蓝屏问题排查
蓝屏错误代码
text
常见蓝屏错误代码:
├── CRITICAL_PROCESS_DIED - 关键进程终止
├── SYSTEM_SERVICE_EXCEPTION - 系统服务异常
├── IRQL_NOT_LESS_OR_EQUAL - 内存访问错误
├── PAGE_FAULT_IN_NONPAGED_AREA - 页面错误
├── KERNEL_DATA_INPAGE_ERROR - 内核数据读取错误
├── DRIVER_IRQL_NOT_LESS_OR_EQUAL - 驱动程序错误
└── WHEA_UNCORRECTABLE_ERROR - 硬件错误分析蓝屏转储文件
powershell
# 查看转储文件位置
Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\CrashControl" | Select-Object DumpFile, MinidumpDir
# 转储文件位置
# 完整转储:C:\Windows\MEMORY.DMP
# 小内存转储:C:\Windows\Minidump\*.dmp
# 使用 WinDbg 或 BlueScreenView 分析转储文件
# WinDbg 下载:https://developer.microsoft.com/windows-hardware/drivers/windb
# 查看蓝屏相关事件
Get-WinEvent -FilterHashtable @{LogName='System'; ProviderName='Microsoft-Windows-WER-SystemErrorReporting'}防止蓝屏的措施
powershell
# 检查驱动程序
Get-WmiObject Win32_PnPSignedDriver |
Where-Object { $_.DeviceName -ne $null } |
Select-Object DeviceName, DriverVersion, Date
# 检查内存问题
# 运行 Windows 内存诊断
mdsched.exe
# 检查磁盘错误
chkdsk C: /f /r
# 检查系统文件
sfc /scannow程序问题排查
程序崩溃排查
powershell
# 查看应用程序事件
Get-WinEvent -FilterHashtable @{LogName='Application'; Level=2} -MaxEvents 20
# 查看特定程序的事件
Get-WinEvent -FilterHashtable @{LogName='Application'; ProviderName='Application Error'}
# 查看程序兼容性设置
$program = "C:\Program Files\MyApp\app.exe"
$compat = Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" -ErrorAction SilentlyContinue
$compat.$program
# 以兼容模式运行程序
# 右键程序 → 属性 → 兼容性选项卡程序无响应
powershell
# 查找无响应的进程
Get-Process | Where-Object { $_.Responding -eq $false }
# 结束无响应进程
Get-Process | Where-Object { $_.Responding -eq $false } | Stop-Process -Force
# 查看进程句柄
$process = Get-Process -Name "notepad"
$process.HandleCount
$process.Threads.Count
# 查看进程加载的模块
$process.Modules | Select-Object ModuleName, FileName程序安装问题
powershell
# 查看 Windows Installer 日志
# 日志位置:%TEMP%\MSI*.log
# 启用 Windows Installer 日志
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Installer" -Name "Logging" -Value "voicewarmup" -Type String
# 重新注册 Windows Installer
msiexec /unregister
msiexec /regserver
# 清理安装缓存
# 注意:谨慎操作
Remove-Item -Path "C:\Windows\Installer\$PatchCache$" -Recurse -Force -ErrorAction SilentlyContinue系统还原和恢复
创建还原点
powershell
# 检查系统保护状态
Get-ComputerRestorePoint
# 启用系统保护
Enable-ComputerRestore -Drive "C:\"
# 创建还原点
Checkpoint-Computer -Description "安装新软件前" -RestorePointType MODIFY_SETTINGS
# 查看还原点
Get-ComputerRestorePoint | Format-Table SequenceNumber, CreationTime, Description
# 恢复到还原点
Restore-Computer -RestorePoint 1系统重置
powershell
# 打开重置选项
# 设置 → 系统 → 恢复 → 重置此电脑
# 使用命令行重置
# 保留个人文件
systemreset -keepmyfiles
# 删除所有内容
systemreset -removeall
# 使用 DISM 重置
dism /online /cleanup-image /restorehealth恢复环境
powershell
# 进入 Windows 恢复环境
# 方法一:设置 → 更新和安全 → 恢复 → 高级启动
# 方法二:按住 Shift 键点击重启
# 方法三:命令行
# 重启到恢复环境
shutdown /r /o /t 0
# 恢复环境中的选项
# - 启动修复
# - 启动设置(安全模式)
# - 系统还原
# - 系统映像恢复
# - 命令提示符硬件问题排查
设备管理器
powershell
# 打开设备管理器
devmgmt.msc
# 查看有问题的设备
Get-PnpDevice | Where-Object { $_.Status -ne "OK" }
# 查看特定类型的设备
Get-PnpDevice -Class "Display"
Get-PnpDevice -Class "NetworkAdapter"
# 禁用设备
Disable-PnpDevice -InstanceId "设备实例ID"
# 启用设备
Enable-PnpDevice -InstanceId "设备实例ID"
# 更新驱动程序
# 右键设备 → 更新驱动程序硬件诊断
powershell
# 检查磁盘健康
Get-PhysicalDisk | Select-Object FriendlyName, MediaType, HealthStatus, OperationalStatus
# 检查 SMART 信息
Get-WmiObject -Namespace root\wmi -Class MSStorageDriver_FailurePredictStatus
# 运行内存诊断
mdsched.exe
# 检查电池状态(笔记本)
Get-CimInstance -ClassName Win32_Battery |
Select-Object Name, Status, EstimatedChargeRemaining
# 检查 CPU 温度(需要第三方工具)
# 推荐工具:HWMonitor、Core Temp日志分析
系统日志分析
powershell
# 查看最近的系统错误
Get-WinEvent -FilterHashtable @{LogName='System'; Level=2} -MaxEvents 20 |
Format-List TimeCreated, Id, ProviderName, Message
# 按事件 ID 分组统计
Get-WinEvent -LogName System |
Group-Object Id |
Sort-Object Count -Descending |
Select-Object Count, Name -First 10
# 查看特定提供程序的事件
Get-WinEvent -FilterHashtable @{LogName='System'; ProviderName='Service Control Manager'}
# 导出事件日志
wevtutil epl System C:\system_log.evtx
wevtutil epl Application C:\app_log.evtx应用程序日志分析
powershell
# 查看应用程序错误
Get-WinEvent -FilterHashtable @{LogName='Application'; Level=2} -MaxEvents 20
# 查看特定应用程序的事件
Get-WinEvent -FilterHashtable @{LogName='Application'; ProviderName='Application Error'}
# 搜索包含特定文本的事件
Get-WinEvent -LogName Application |
Where-Object { $_.Message -like "*错误*" } |
Select-Object TimeCreated, Message -First 10故障排查脚本
系统诊断脚本
powershell
# 系统诊断脚本
function Invoke-SystemDiagnostic {
Write-Host "=== Windows 系统诊断报告 ===" -ForegroundColor Cyan
Write-Host "生成时间: $(Get-Date)" -ForegroundColor Gray
# 系统信息
Write-Host "`n[系统信息]" -ForegroundColor Yellow
$os = Get-CimInstance Win32_OperatingSystem
Write-Host " 操作系统: $($os.Caption)"
Write-Host " 版本: $($os.Version)"
Write-Host " 最后启动: $($os.LastBootUpTime)"
# CPU 使用率
Write-Host "`n[CPU 状态]" -ForegroundColor Yellow
$cpu = Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 3
$avgCpu = ($cpu.CounterSamples.CookedValue | Measure-Object -Average).Average
Write-Host " CPU 使用率: $([math]::Round($avgCpu, 2))%"
# 内存状态
Write-Host "`n[内存状态]" -ForegroundColor Yellow
$os = Get-CimInstance Win32_OperatingSystem
$freeMem = [math]::Round($os.FreePhysicalMemory / 1MB, 2)
$totalMem = [math]::Round($os.TotalVisibleMemorySize / 1MB, 2)
$usedMem = $totalMem - $freeMem
$usedPercent = [math]::Round(($usedMem / $totalMem) * 100, 2)
Write-Host " 总内存: $totalMem GB"
Write-Host " 已使用: $usedMem GB ($usedPercent%)"
Write-Host " 可用: $freeMem GB"
# 磁盘状态
Write-Host "`n[磁盘状态]" -ForegroundColor Yellow
Get-CimInstance Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 } | ForEach-Object {
$free = [math]::Round($_.FreeSpace / 1GB, 2)
$total = [math]::Round($_.Size / 1GB, 2)
$used = $total - $free
$percent = [math]::Round(($used / $total) * 100, 2)
Write-Host " $($_.DeviceID) 总: $total GB, 已用: $used GB ($percent%), 可用: $free GB"
}
# 网络状态
Write-Host "`n[网络状态]" -ForegroundColor Yellow
$adapters = Get-NetAdapter | Where-Object { $_.Status -eq "Up" }
foreach ($adapter in $adapters) {
Write-Host " $($adapter.Name): $($adapter.Status)"
}
# 服务状态
Write-Host "`n[服务状态]" -ForegroundColor Yellow
$stoppedAuto = Get-Service | Where-Object { $_.StartType -eq "Automatic" -and $_.Status -eq "Stopped" }
if ($stoppedAuto) {
Write-Host " 已停止的自动服务:" -ForegroundColor Red
$stoppedAuto | ForEach-Object { Write-Host " - $($_.Name)" }
} else {
Write-Host " 所有自动服务正常运行" -ForegroundColor Green
}
# 最近的错误事件
Write-Host "`n[最近的系统错误]" -ForegroundColor Yellow
$errors = Get-WinEvent -FilterHashtable @{LogName='System'; Level=2} -MaxEvents 5 -ErrorAction SilentlyContinue
if ($errors) {
$errors | ForEach-Object {
Write-Host " $($_.TimeCreated) - $($_.ProviderName) - ID: $($_.Id)"
}
} else {
Write-Host " 没有发现最近的系统错误" -ForegroundColor Green
}
Write-Host "`n=== 诊断完成 ===" -ForegroundColor Cyan
}
# 运行诊断
Invoke-SystemDiagnostic小结
本章介绍了 Windows 故障排查的主要内容:
- 排查步骤:了解故障排查的基本方法
- 诊断工具:使用事件查看器、SFC、DISM 等工具
- 启动问题:解决无法启动、蓝屏等问题
- 性能问题:排查 CPU、内存、磁盘性能问题
- 网络问题:诊断网络连接问题
- 蓝屏问题:分析和解决蓝屏错误
- 程序问题:解决程序崩溃和无响应
- 系统恢复:使用还原点和系统重置
- 硬件问题:诊断硬件故障
- 日志分析:分析系统日志定位问题
建议:
- 定期创建系统还原点
- 保持系统和驱动更新
- 使用事件查看器追踪问题
- 遇到问题先尝试安全模式
- 重要数据定期备份
- 使用诊断脚本定期检查系统状态
