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
| function Get-EnergyConsumption { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$ServerName )
$cpuUsage = (Get-Counter -ComputerName $ServerName '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue $memoryUsage = (Get-Counter -ComputerName $ServerName '\Memory\Available MBytes').CounterSamples.CookedValue $energyCost = [math]::Round(($cpuUsage * 0.7) + ($memoryUsage * 0.05), 2)
[PSCustomObject]@{ ServerName = $ServerName Timestamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss') CPUUsage = "$cpuUsage%" MemoryAvailable = "${memoryUsage}MB" EstimatedPower = "${energyCost}W" OptimizationSuggestion = if ($cpuUsage -lt 30) {'建议启用节能模式'} else {'建议优化负载分配'} } }
'SRV01','SRV02','SRV03' | ForEach-Object { Get-EnergyConsumption -ServerName $_ -Verbose } | Export-Csv -Path "Energy_Report_$(Get-Date -Format yyyyMMdd).csv"
|