PowerShell可自动生成系统健康报告,涵盖CPU/内存使用率、磁盘余量、关键服务状态、24小时错误事件及运行时长,并支持HTML输出与任务计划自动执行。
powershell 是 windows 系统管理员的得力工具,用它生成定期系统健康报告既高效又可控。核心思路是:收集关键指标(cpu、内存、磁盘、服务状态、事件日志告警等)→ 格式化输出为易读报告(html 或 csv)→ 通过任务计划程序自动执行。
一、确定要监控的核心健康指标
一份实用的系统健康报告不需要面面俱到,重点覆盖以下几类:
- CPU 和内存使用率:取最近5分钟平均值,避免瞬时峰值干扰判断
- 磁盘空间余量:重点关注系统盘(C:)和日志存储盘,低于15%触发提示
- 关键服务状态:如 WinRM、DHCP、DNS、Spooler 等,检查是否运行中且启动类型为“自动”
- 近24小时严重/错误事件:筛选 System 和 Application 日志中 Level=1(错误)或 Level=2(严重)的条目
- 系统启动时间与正常运行时长:辅助判断是否近期非计划重启
二、编写基础报告脚本(含 HTML 输出)
以下是一个可直接运行的简化版脚本框架,保存为 SystemHealthReport.ps1:
# 获取当前时间戳
$Date = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$ReportPath = "C:\Reports\SystemHealth_$(Get-Date -Format 'yyyyMMdd').html"
<h1>CPU & Memory</h1><p>$CPU = (Get-Counter '\Processor(<em>Total)\% Processor Time' -SampleInterval 2 -MaxSamples 3).CounterSamples.CookedValue | Measure-Object -Average | ForEach-Object { [math]::Round($</em>.Average, 2) }
$Memory = (Get-Counter '\Memory\% Committed Bytes In Use').CounterSamples.CookedValue | ForEach-Object { [math]::Round($_, 2) }</p><h1>Disk space (C:)</h1><p>$Disk = Get-PSDrive C | Select-Object Used, Free, Root
$DiskFreePct = [math]::Round(($Disk.Free / ($Disk.Used + $Disk.Free)) * 100, 2)</p><h1>Critical services</h1><p>$CriticalServices = 'WinRM', 'Dhcp', 'DNS', 'Spooler'
$ServiceStatus = foreach ($svc in $CriticalServices) {
$s = Get-Service $svc -ErrorAction SilentlyContinue
[PSCustomObject]@{
Name = $svc
Status = if ($s) { $s.Status } else { 'Not Found' }
StartType = if ($s) { $s.StartType } else { '-' }
}
}</p><h1>Recent errors (last 24h)</h1><p>$Errors = Get-WinEvent -FilterHashtable @{
LogName='System','Application'
Level=1,2
StartTime=(Get-Date).AddHours(-24)
} -ErrorAction SilentlyContinue |
Select-Object TimeCreated, LogName, Id, LevelDisplayName, Message |
Select-Object -First 10</p><h1>Uptime</h1><p>$BootTime = (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime
$Uptime = (Get-Date) - [Management.ManagementDateTimeConverter]::ToDateTime($BootTime)</p><h1>Build HTML report</h1><p>$HTML = @"
<html><head><title>System Health Report</title>
<style>body{font-family:Segoe UI,sans-serif;margin:20px}table{border-collapse:collapse;width:100%}th,td{border:1px solid #ccc;padding:8px;text-align:left}</style>
</head><body>
<h2>System Health Report — $Date</h2>
<p><strong>Uptime:</strong> $($Uptime.Days) days, $($Uptime.Hours) hours<br>
<strong>CPU Usage:</strong> $CPU% <br>
<strong>Memory Usage:</strong> $Memory% <br>
<strong>C: Free Space:</strong> $DiskFreePct% ($([math]::Round($Disk.Free/1GB,2)) GB)</p></p><p><h3>Critical Services</h3>
<table><tr><th>Service</th><th>Status</th><th>Startup</th></tr>
$($ServiceStatus | ConvertTo-Html -Fragment | Out-String)
</table></p><p><h3>Recent Errors (Top 10)</h3>
$($Errors | ConvertTo-Html -Fragment | Out-String)</p><p></body></html>
"@</p><p>$HTML | Out-File -FilePath $ReportPath -Encoding UTF8
Write-Host "Report saved to: $ReportPath"
三、设置定时自动执行(Windows 任务计划)
让脚本每天早上8点自动生成最新报告:
- 以管理员身份打开 任务计划程序
- 创建基本任务 → 命名为 “Daily System Health Report”
- 触发器设为“每天”,时间为 08:00
- 操作选择“启动程序”,填写:
程序/脚本: powershell.exe
参数: -ExecutionPolicy Bypass -File "C:\Scripts\SystemHealthReport.ps1"
起始于: C:\Scripts\(确保路径存在且脚本放在此处) - 在“常规”选项卡勾选“使用最高权限运行”和“不管用户是否登录都要运行”(如需后台执行)
四、增强实用性的小技巧
让报告更可靠、更易维护:
-
邮件自动发送:在脚本末尾添加
Send-MailMessage,调用公司 SMTP 发送 HTML 报告(注意配置凭据或使用无认证中继) -
历史归档:修改脚本,将旧报告按日期文件夹存放,例如
C:\Reports\2024-05\,避免单目录堆积 -
多服务器批量采集:用
Invoke-Command -ComputerName Server01,Server02封装逻辑,集中生成汇总报告 - 异常高亮:在 HTML 中为 CPU >90%、磁盘

















