当前位置: 首页 > news >正文

PowerShell脚本检查业务健康状态

该脚本是一个全面的业务健康状态检查工具
目前版本为V5,后续为继续改进,请读者多多指教。

# 业务健康状态检查脚本
# 检查指标:流量、错误率、响应时间、磁盘空间、服务状态等
# 兼容PowerShell 5.1及更高版本param([switch]$VerboseOutput = $false,[switch]$CheckAll = $false
)# 日志函数
function Write-Log {param([string]$Message, [string]$Level = "INFO")$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"$logEntry = "[$timestamp] [$Level] $Message"$color = switch ($Level) {"ERROR" { "Red" }"WARNING" { "Yellow" }"INFO" { "White" }"SUCCESS" { "Green" }default { "Gray" }}Write-Host $logEntry -ForegroundColor $color$logEntry | Out-File -Append -FilePath "healthcheck.log" -Encoding UTF8
}# 显示系统信息
function Show-SystemInfo {Write-Host "`n=== 系统信息 ===" -ForegroundColor Cyantry {$os = Get-WmiObject -Class Win32_OperatingSystemWrite-Host "操作系统: $($os.Caption) $($os.Version)"Write-Host "系统架构: $($os.OSArchitecture)"Write-Host "启动时间: $($os.ConvertToDateTime($os.LastBootUpTime))"} catch {Write-Host "操作系统: Windows (无法获取详细信息)" -ForegroundColor Yellow}Write-Host "计算机名: $env:COMPUTERNAME"Write-Host "运行用户: $env:USERNAME"Write-Host "PowerShell版本: $($PSVersionTable.PSVersion)"Write-Host "运行时间: $(Get-Date)"
}# 检查服务状态
function Test-ServiceHealth {$results = @()# 常见的关键服务$criticalServices = @(@{ Name = "EventLog"; DisplayName = "Windows Event Log" },@{ Name = "Dnscache"; DisplayName = "DNS Client" },@{ Name = "Dhcp"; DisplayName = "DHCP Client" },@{ Name = "Winmgmt"; DisplayName = "Windows Management Instrumentation" })# 可选的服务(如果存在就检查)$optionalServices = @(@{ Name = "WinRM"; DisplayName = "Windows Remote Management" },@{ Name = "Spooler"; DisplayName = "Print Spooler" },@{ Name = "Themes"; DisplayName = "Themes" })foreach ($service in $criticalServices) {try {$svc = Get-Service -Name $service.Name -ErrorAction Stop$status = @{Name = $service.DisplayNameStatus = $svc.StatusHealthy = $svc.Status -eq "Running"Critical = $true}$results += $statusif (-not $status.Healthy) {Write-Log "关键服务异常: $($service.DisplayName) - 状态: $($svc.Status)" "ERROR"}elseif ($VerboseOutput) {Write-Log "关键服务正常: $($service.DisplayName)" "SUCCESS"}}catch {Write-Log "关键服务缺失: $($service.Name) - $($_.Exception.Message)" "ERROR"$results += @{Name = $service.DisplayNameStatus = "NotFound"Healthy = $falseCritical = $true}}}foreach ($service in $optionalServices) {try {$svc = Get-Service -Name $service.Name -ErrorAction Stop$status = @{Name = $service.DisplayNameStatus = $svc.StatusHealthy = $svc.Status -eq "Running"Critical = $false}$results += $statusif (-not $status.Healthy) {Write-Log "可选服务未运行: $($service.DisplayName) - 状态: $($svc.Status)" "WARNING"}elseif ($VerboseOutput) {Write-Log "可选服务正常: $($service.DisplayName)" "INFO"}}catch {# 可选服务不存在不算错误if ($VerboseOutput) {Write-Log "可选服务不存在: $($service.Name)" "INFO"}}}return $results
}# 检查网络连通性
function Test-NetworkConnectivity {$results = @()# 测试一些常见的网络端点$testEndpoints = @(@{ Name = "Baidu"; Url = "https://www.baidu.com"; Timeout = 10 },@{ Name = "GoogleDNS"; Url = "https://dns.google"; Timeout = 10 },@{ Name = "Microsoft"; Url = "https://www.microsoft.com"; Timeout = 10 })foreach ($endpoint in $testEndpoints) {try {$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()$response = Invoke-WebRequest -Uri $endpoint.Url -Method Get -TimeoutSec $endpoint.Timeout -ErrorAction Stop$stopwatch.Stop()$responseTime = $stopwatch.ElapsedMilliseconds$isHealthy = $response.StatusCode -eq 200$status = @{Name = $endpoint.NameUrl = $endpoint.UrlStatusCode = $response.StatusCodeResponseTimeMs = $responseTimeHealthy = $isHealthy}$results += $statusif ($isHealthy) {Write-Log "网络连通性正常: $($endpoint.Name) - $responseTime ms" "SUCCESS"} else {Write-Log "网络连通性异常: $($endpoint.Name) - 状态码: $($response.StatusCode)" "WARNING"}}catch {Write-Log "网络连通性测试失败: $($endpoint.Name) - $($_.Exception.Message)" "WARNING"$results += @{Name = $endpoint.NameUrl = $endpoint.UrlStatusCode = "Error"ResponseTimeMs = 0Healthy = $falseError = $_.Exception.Message}}}return $results
}# 检查性能计数器
function Test-Performance {$results = @()$performanceCounters = @(@{Category = "Processor"Counter = "% Processor Time"Instance = "_Total"WarningThreshold = 85CriticalThreshold = 95IsValueThreshold = $false  # 高于阈值告警},@{Category = "Memory"Counter = "Available MBytes"Instance = ""WarningThreshold = 1024  # 1GBCriticalThreshold = 512  # 512MBIsValueThreshold = $true  # 低于阈值告警},@{Category = "LogicalDisk"Counter = "% Free Space"Instance = "C:"WarningThreshold = 15  # 15%CriticalThreshold = 5   # 5%IsValueThreshold = $true})foreach ($counter in $performanceCounters) {try {# 构建计数器路径$counterPath = if ($counter.Instance) {"\$($counter.Category)($($counter.Instance))\$($counter.Counter)"} else {"\$($counter.Category)\$($counter.Counter)"}# 获取计数器值$counterData = Get-Counter -Counter $counterPath -SampleInterval 1 -MaxSamples 1 -ErrorAction Stop$value = [math]::Round($counterData.CounterSamples.CookedValue, 2)$status = @{Counter = $counterPathValue = $valueHealthy = $true}# 检查阈值if ($counter.IsValueThreshold) {# 值低于阈值告警if ($value -lt $counter.CriticalThreshold) {$status.Healthy = $falseWrite-Log "性能严重异常: $counterPath - 值: $value (阈值: <$($counter.CriticalThreshold))" "ERROR"}elseif ($value -lt $counter.WarningThreshold) {Write-Log "性能警告: $counterPath - 值: $value (阈值: <$($counter.WarningThreshold))" "WARNING"}} else {# 值高于阈值告警if ($value -gt $counter.CriticalThreshold) {$status.Healthy = $falseWrite-Log "性能严重异常: $counterPath - 值: $value (阈值: >$($counter.CriticalThreshold))" "ERROR"}elseif ($value -gt $counter.WarningThreshold) {Write-Log "性能警告: $counterPath - 值: $value (阈值: >$($counter.WarningThreshold))" "WARNING"}}if ($status.Healthy -and $VerboseOutput) {Write-Log "性能正常: $counterPath - 值: $value" "INFO"}$results += $status}catch {Write-Log "无法获取性能计数器: $($counter.Category)\$($counter.Counter) - $($_.Exception.Message)" "WARNING"}}return $results
}# 检查磁盘空间
function Test-DiskSpace {$results = @()try {$disks = Get-WmiObject -Class Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 }  # 固定磁盘foreach ($disk in $disks) {if ($disk.Size -gt 0) {$freePercent = ($disk.FreeSpace / $disk.Size) * 100$freeGB = [math]::Round($disk.FreeSpace / 1GB, 2)$totalGB = [math]::Round($disk.Size / 1GB, 2)$isHealthy = $freePercent -ge 10  # 至少10%空闲空间$status = @{Drive = $disk.DeviceIDTotalGB = $totalGBFreeGB = $freeGBFreePercent = [math]::Round($freePercent, 2)Healthy = $isHealthy}$results += $statusif (-not $isHealthy) {Write-Log "磁盘空间不足: $($disk.DeviceID) - 空闲: $([math]::Round($freePercent, 2))% ($freeGB GB)" "ERROR"} else {Write-Log "磁盘空间正常: $($disk.DeviceID) - 空闲: $([math]::Round($freePercent, 2))% ($freeGB GB)" "SUCCESS"}}}}catch {Write-Log "无法检查磁盘空间: $($_.Exception.Message)" "WARNING"}return $results
}# 检查网络流量
function Test-NetworkTraffic {try {# 使用WMI获取网络流量信息$networkAdapters = Get-WmiObject -Class Win32_PerfFormattedData_Tcpip_NetworkInterface | Where-Object { $_.BytesTotalPersec -gt 0 } |Sort-Object BytesTotalPersec -Descending |Select-Object -First 5$totalTraffic = ($networkAdapters | Measure-Object -Property BytesTotalPersec -Sum).Sum$trafficMB = [math]::Round($totalTraffic / 1MB, 2)Write-Log "网络流量: $trafficMB MB/s (Top 5 网卡)" "INFO"return @{TotalTrafficMB = $trafficMBHealthy = $trueAdapters = $networkAdapters | ForEach-Object {@{Name = $_.NameTrafficMB = [math]::Round($_.BytesTotalPersec / 1MB, 2)}}}}catch {Write-Log "无法获取网络流量信息: $($_.Exception.Message)" "WARNING"return @{TotalTrafficMB = 0Healthy = $false}}
}# 检查进程状态
function Test-ProcessHealth {$results = @()# 检查一些关键进程$criticalProcesses = @(@{ Name = "explorer"; Description = "Windows Explorer" },@{ Name = "svchost"; Description = "Service Host" },@{ Name = "winlogon"; Description = "Windows Logon" })foreach ($process in $criticalProcesses) {$procCount = (Get-Process -Name $process.Name -ErrorAction SilentlyContinue).Count$isHealthy = $procCount -gt 0$status = @{Name = $process.DescriptionCount = $procCountHealthy = $isHealthy}$results += $statusif ($isHealthy) {if ($VerboseOutput) {Write-Log "进程正常: $($process.Description) ($procCount 个实例)" "INFO"}} else {Write-Log "进程异常: $($process.Description) 未运行" "WARNING"}}return $results
}# 生成健康报告
function Get-HealthReport {param($ServiceResults, $NetworkResults, $PerfResults, $DiskResults, $ProcessResults)$totalChecks = $ServiceResults.Count + $NetworkResults.Count + $PerfResults.Count + $DiskResults.Count + $ProcessResults.Count$healthyChecks = ($ServiceResults | Where-Object { $_.Healthy }).Count +($NetworkResults | Where-Object { $_.Healthy }).Count +($PerfResults | Where-Object { $_.Healthy }).Count +($DiskResults | Where-Object { $_.Healthy }).Count +($ProcessResults | Where-Object { $_.Healthy }).Count$healthPercentage = if ($totalChecks -gt 0) { ($healthyChecks / $totalChecks) * 100 } else { 0 }return @{Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"TotalChecks = $totalChecksHealthyChecks = $healthyChecksHealthPercentage = [math]::Round($healthPercentage, 2)Services = $ServiceResultsNetwork = $NetworkResultsPerformance = $PerfResultsDiskSpace = $DiskResultsProcesses = $ProcessResultsOverallHealthy = $healthPercentage -ge 60  # 60%以上认为健康}
}# 主执行逻辑
try {Write-Log "开始业务健康状态检查..." "INFO"Show-SystemInfo# 执行各项检查Write-Log "检查服务状态..." "INFO"$serviceResults = Test-ServiceHealthWrite-Log "检查网络连通性..." "INFO"$networkResults = Test-NetworkConnectivityWrite-Log "检查性能指标..." "INFO"$perfResults = Test-PerformanceWrite-Log "检查磁盘空间..." "INFO"$diskResults = Test-DiskSpaceWrite-Log "检查网络流量..." "INFO"$networkTraffic = Test-NetworkTrafficWrite-Log "检查进程状态..." "INFO"$processResults = Test-ProcessHealth# 生成报告$healthReport = Get-HealthReport -ServiceResults $serviceResults -NetworkResults $networkResults -PerfResults $perfResults -DiskResults $diskResults -ProcessResults $processResults# 输出报告Write-Host "`n=== 业务健康状态报告 ===" -ForegroundColor GreenWrite-Host "检查时间: $($healthReport.Timestamp)"Write-Host "检查项目: $($healthReport.TotalChecks)"Write-Host "健康项目: $($healthReport.HealthyChecks)"Write-Host "健康度: $($healthReport.HealthPercentage)%"Write-Host "网络流量: $($networkTraffic.TotalTrafficMB) MB/s"Write-Host "`n--- 服务状态 ---" -ForegroundColor Cyan$serviceResults | ForEach-Object {$color = if ($_.Healthy) { "Green" } else { "Red" }Write-Host "$($_.Name): $($_.Status)" -ForegroundColor $color}Write-Host "`n--- 网络连通性 ---" -ForegroundColor Cyan$networkResults | ForEach-Object {$color = if ($_.Healthy) { "Green" } else { "Yellow" }$status = if ($_.Healthy) { "OK ($($_.ResponseTimeMs)ms)" } else { "FAILED" }Write-Host "$($_.Name): $status" -ForegroundColor $color}Write-Host "`n--- 性能指标 ---" -ForegroundColor Cyan$perfResults | ForEach-Object {$color = if ($_.Healthy) { "Green" } else { "Red" }Write-Host "$($_.Counter): $($_.Value)" -ForegroundColor $color}Write-Host "`n--- 磁盘空间 ---" -ForegroundColor Cyan$diskResults | ForEach-Object {$color = if ($_.Healthy) { "Green" } else { "Red" }Write-Host "$($_.Drive): $($_.FreeGB)GB/$($_.TotalGB)GB ($($_.FreePercent)%)" -ForegroundColor $color}Write-Host "`n--- 进程状态 ---" -ForegroundColor Cyan$processResults | ForEach-Object {$color = if ($_.Healthy) { "Green" } else { "Yellow" }$status = if ($_.Healthy) { "Running ($($_.Count))" } else { "Stopped" }Write-Host "$($_.Name): $status" -ForegroundColor $color}# 总体状态Write-Host "`n=== 总体状态 ===" -ForegroundColor $(if ($healthReport.OverallHealthy) { "Green" } else { "Red" })if ($healthReport.OverallHealthy) {Write-Host "业务系统健康状态: 正常" -ForegroundColor GreenWrite-Host "所有关键组件运行正常" -ForegroundColor Green} else {Write-Host "业务系统健康状态: 需要关注" -ForegroundColor YellowWrite-Host "建议检查标红或标黄的异常项目" -ForegroundColor Yellow}Write-Log "健康检查完成" "SUCCESS"Write-Host "`n详细日志已保存到: healthcheck.log" -ForegroundColor Gray# 返回适当的退出代码if ($healthReport.OverallHealthy) {exit 0} else {exit 1}
}
catch {Write-Log "脚本执行失败: $($_.Exception.Message)" "ERROR"exit 2
}

这个脚本的特点:

  1. 完全自包含:不需要任何配置文件
  2. 通用性:检查所有Windows系统都有的基本组件
  3. 详细的健康报告:包括服务、网络、性能、磁盘、进程等
  4. 友好的输出:彩色编码的状态显示
  5. 错误处理:优雅地处理各种异常情况
  6. 退出代码:0=健康,1=需要关注,2=脚本错误

使用方法:

# 基本检查
.\healthcheck.ps1# 详细输出模式
.\healthcheck.ps1 -VerboseOutput# 检查所有项目(包括可选项目)
.\healthcheck.ps1 -CheckAll

这个脚本应该能在您的系统上正常运行,并提供有用的健康状态信息。

http://www.dtcms.com/a/341711.html

相关文章:

  • 解决Docker 无法连接到官方镜像仓库
  • Lecture 6 Kernels, Triton 课程笔记
  • JVM基础知识总结
  • Docker 核心技术:Linux Cgroups
  • GDB 的多线程调试
  • 针对具有下垂控制光伏逆变器的主动配电网络的多目标分层协调电压/无功控制方法的复现
  • 音频读写速度优化 音频格式
  • Transformer内容详解(通透版)
  • pip install -e中e 参数解释
  • 八辊矫平机·第三篇
  • 卸载win10/win11系统里导致磁盘故障的补丁
  • 广东省省考备考(第八十二天8.20)——资料分析、数量、言语(强化训练)
  • 【蒸蒸日上】军八武将篇——标1
  • 8 webUI中-Controlnet(控制与约束)的应用分类与使用方法
  • 【语法】markdown非常用场景
  • Netty HashedWheelTimer设计原理:从时间轮算法到源码实现
  • 跨平台 RTSP/RTMP 播放器工程化实践:低延迟与高稳定性的挑战与突破
  • 【数据分享】东北大鼠疫传播与死亡空间数据
  • Vue透传 Attributes(详细解析)2
  • 恶补DSP:2.F28335的定时器系统
  • 买返商城网站源码多平台购物返现搭建图解源码二开
  • 万象生鲜配送系统 2025 年 8 月 15 日更新日志
  • 八月月报丨MaxKB在教育及教学科研领域的应用进展
  • Hadoop学习
  • 达梦数据库-实时主备集群部署详解(附图文)手工搭建一主一备数据守护集群DW
  • HyDE vs HyPE:AI检索界的‘假想敌’革命,如何让RAG系统从‘找资料’变成‘懂你心’?”
  • Firefox 142 引入 CRLite 用于私有证书撤销
  • 【AI应用】部署AI向量数据库Milvus
  • Oracle:配置让插入语句时id自动输入
  • Sora网页打不开怎么办?常见原因与解决方法