1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| function New-DailyHealthReport {
param( [string]$OutputPath = "C:\Reports\daily-health.html" )
$os = Get-CimInstance Win32_OperatingSystem $cpu = Get-CimInstance Win32_Processor | Select-Object -First 1 $disks = Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3" $topProcesses = Get-Process | Sort-Object WorkingSet64 -Descending | Select-Object -First 10 $services = Get-Service | Where-Object { $_.StartType -eq 'Automatic' -and $_.Status -ne 'Running' }
$css = @" <style> body { font-family: 'Segoe UI', Arial, sans-serif; margin: 20px; background: #f5f6fa; color: #2d3436; } .container { max-width: 1200px; margin: 0 auto; } h1 { color: #2d3436; border-bottom: 3px solid #0984e3; padding-bottom: 10px; } h2 { color: #636e72; margin-top: 30px; border-left: 4px solid #0984e3; padding-left: 10px; } .summary { display: flex; flex-wrap: wrap; gap: 15px; margin: 20px 0; } .card { background: white; border-radius: 8px; padding: 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); min-width: 200px; flex: 1; } .card .label { font-size: 12px; color: #b2bec3; text-transform: uppercase; } .card .value { font-size: 28px; font-weight: bold; color: #0984e3; margin-top: 5px; } .card.warning .value { color: #fdcb6e; } .card.danger .value { color: #d63031; } table { width: 100%; border-collapse: collapse; background: white; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 8px rgba(0,0,0,0.1); margin: 10px 0; } th { background: #0984e3; color: white; padding: 12px 15px; text-align: left; font-size: 13px; } td { padding: 10px 15px; border-bottom: 1px solid #dfe6e9; } tr:hover { background: #f0f8ff; } .bar { height: 8px; background: #dfe6e9; border-radius: 4px; overflow: hidden; min-width: 80px; } .bar-fill { height: 100%; border-radius: 4px; } .good { background: #00b894; } .warning { background: #fdcb6e; } .danger { background: #d63031; } .footer { text-align: center; color: #b2bec3; margin-top: 30px; font-size: 12px; } .status-badge { padding: 3px 10px; border-radius: 12px; font-size: 12px; font-weight: bold; } .status-ok { background: #e8f5e9; color: #27ae60; } .status-error { background: #ffebee; color: #c0392b; } </style> "@
$report = @" <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>系统健康报告 - $($os.CSName)</title> $css </head> <body> <div class="container"> <h1>系统健康报告</h1> <p>计算机:<strong>$($os.CSName)</strong> | 操作系统:$($os.Caption) | 报告时间:$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')</p>
<div class="summary"> <div class="card"> <div class="label">CPU 使用率</div> <div class="value">$($cpu.LoadPercentage)%</div> </div> <div class="card"> <div class="label">内存使用率</div> <div class="value">$([math]::Round(($os.TotalVisibleMemorySize - $os.FreePhysicalMemory) / $os.TotalVisibleMemorySize * 100, 1))%</div> </div> <div class="card"> <div class="label">运行时间</div> <div class="value">$([math]::Round(((Get-Date) - $os.LastBootUpTime).TotalDays, 1)) 天</div> </div> <div class="card"> <div class="label">异常服务</div> <div class="value">$($services.Count)</div> </div> </div>
<h2>磁盘状态</h2> <table> <tr><th>驱动器</th><th>文件系统</th><th>总容量</th><th>已使用</th><th>可用</th><th>使用率</th></tr> "@
foreach ($disk in $disks) { $totalGB = [math]::Round($disk.Size / 1GB, 2) $freeGB = [math]::Round($disk.FreeSpace / 1GB, 2) $usedGB = [math]::Round(($disk.Size - $disk.FreeSpace) / 1GB, 2) $usedPct = [math]::Round(($disk.Size - $disk.FreeSpace) / $disk.Size * 100, 1) $barClass = if ($usedPct -gt 90) { 'danger' } elseif ($usedPct -gt 70) { 'warning' } else { 'good' }
$report += @" <tr> <td>$($disk.DeviceID)</td> <td>$($disk.FileSystem)</td> <td>${totalGB} GB</td> <td>${usedGB} GB</td> <td>${freeGB} GB</td> <td> <div class="bar"><div class="bar-fill $barClass" style="width:$usedPct%"></div></div> $usedPct% </td> </tr> "@ }
$report += @" </table>
<h2>内存占用 Top 10</h2> <table> <tr><th>进程名</th><th>PID</th><th>内存 (MB)</th><th>CPU (s)</th></tr> "@
foreach ($proc in $topProcesses) { $memMB = [math]::Round($proc.WorkingSet64 / 1MB, 1) $cpuS = [math]::Round($proc.CPU, 2) $report += " <tr><td>$($proc.Name)</td><td>$($proc.Id)</td><td>$memMB</td><td>$cpuS</td></tr>`n" }
$report += @" </table> "@
if ($services) { $report += @" <h2>异常自动启动服务</h2> <table> <tr><th>服务名</th><th>显示名</th><th>状态</th></tr> "@ foreach ($svc in $services) { $report += " <tr><td>$($svc.Name)</td><td>$($svc.DisplayName)</td><td><span class=`"status-badge status-error`">$($svc.Status)</span></td></tr>`n" } $report += " </table>`n" }
$report += @" <div class="footer"> 报告由 PowerShell 自动生成 | $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') </div> </div> </body> </html> "@
$report | Set-Content $OutputPath -Encoding UTF8 Write-Host "健康报告已生成:$OutputPath" -ForegroundColor Green }
New-DailyHealthReport
|