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
| function Start-HardwareMonitoring { param( [string[]]$ComputerNames = @($env:COMPUTERNAME), [int]$Interval = 300, [string]$LogPath, [int]$Duration = 3600, [scriptblock]$AlertAction, [switch]$EnableEmailAlerts, [string]$SmtpServer, [string]$FromAddress, [string[]]$ToAddresses ) try { $startTime = Get-Date $endTime = $startTime.AddSeconds($Duration) $currentTime = $startTime Write-Host "开始硬件监控,将持续到 $($endTime.ToString('yyyy-MM-dd HH:mm:ss'))" if ($LogPath) { $logFile = Join-Path -Path $LogPath -ChildPath "HardwareMonitoring_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv" $logHeaders = "Timestamp,ComputerName,Status,CPUUsage,MemoryUsage,FreeDiskSpace,Temperature,Warnings,Errors" $logHeaders | Out-File -FilePath $logFile } while ($currentTime -lt $endTime) { foreach ($computer in $ComputerNames) { $healthInfo = Get-HardwareHealth -ComputerName $computer -IncludeSMARTData if ($LogPath) { $cpuUsage = $healthInfo.ResourceMetrics.CPU.CurrentUsage -replace '%', '' $memoryUsage = $healthInfo.ResourceMetrics.Memory.UsagePercent -replace '%', '' $freeDiskSpace = ($healthInfo.ResourceMetrics.Disk.Drives | ForEach-Object { $_.PercentFree -replace '%', '' }) -join '|' $temperature = ($healthInfo.TemperatureMetrics | ForEach-Object { "$($_.SensorType):$($_.TemperatureCelsius)" }) -join '|' $warnings = ($healthInfo.Warnings -join '|').Replace(',', ';') $errors = ($healthInfo.Errors -join '|').Replace(',', ';') $logLine = "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'),$computer,$($healthInfo.Status),$cpuUsage,$memoryUsage,$freeDiskSpace,$temperature,`"$warnings`",`"$errors`"" $logLine | Out-File -FilePath $logFile -Append } $statusColor = switch ($healthInfo.Status) { "Healthy" { "Green" } "Warning" { "Yellow" } "Critical" { "Red" } default { "White" } } Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $computer - 状态: " -NoNewline Write-Host $healthInfo.Status -ForegroundColor $statusColor foreach ($warning in $healthInfo.Warnings) { Write-Host " 警告: $warning" -ForegroundColor Yellow } foreach ($error in $healthInfo.Errors) { Write-Host " 错误: $error" -ForegroundColor Red } if ($healthInfo.Status -ne "Healthy") { if ($AlertAction) { & $AlertAction -HealthInfo $healthInfo } if ($EnableEmailAlerts -and $SmtpServer -and $FromAddress -and $ToAddresses) { $subject = "硬件监控警报 - $computer - $($healthInfo.Status)" $body = @" 计算机: $computer 状态: $($healthInfo.Status) 时间: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')
警告: $($healthInfo.Warnings | ForEach-Object { "- $_" } | Out-String)
错误: $($healthInfo.Errors | ForEach-Object { "- $_" } | Out-String)
系统指标: - CPU使用率: $($healthInfo.ResourceMetrics.CPU.CurrentUsage) - 内存使用率: $($healthInfo.ResourceMetrics.Memory.UsagePercent) - 磁盘使用情况: $($healthInfo.ResourceMetrics.Disk.Drives | ForEach-Object { " $($_.Drive): 可用空间 $($_.PercentFree)" } | Out-String) "@ $emailParams = @{ SmtpServer = $SmtpServer From = $FromAddress To = $ToAddresses Subject = $subject Body = $body } try { Send-MailMessage @emailParams Write-Host " 已发送电子邮件警报" -ForegroundColor Cyan } catch { Write-Host " 发送电子邮件警报失败: $_" -ForegroundColor Red } } } } $currentTime = Get-Date if ($currentTime -lt $endTime) { $nextCheckTime = $currentTime.AddSeconds($Interval) $waitTime = ($nextCheckTime - $currentTime).TotalSeconds Write-Host "下一次检查将在 $($nextCheckTime.ToString('HH:mm:ss')) 进行 (等待 $([Math]::Round($waitTime, 0)) 秒)" -ForegroundColor Cyan Start-Sleep -Seconds ([Math]::Round($waitTime, 0)) } } Write-Host "硬件监控已完成。监控持续了 $([Math]::Round(((Get-Date) - $startTime).TotalMinutes, 0)) 分钟。" if ($LogPath) { Write-Host "监控日志已保存至: $logFile" } } catch { Write-Error "硬件监控过程中出错: $_" } }
|