Update check-cpu.ps1

This commit is contained in:
Markus Fleschutz 2022-10-12 14:58:26 +02:00
parent bffb7d6a11
commit e0b603f1b6

View File

@ -12,27 +12,38 @@
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
try { function GetCPUTemperatureInCelsius {
if (test-path "/sys/class/thermal/thermal_zone0/temp" -pathType leaf) { $Temp = 99999.9 # unsupported
[int]$IntTemp = get-content "/sys/class/thermal/thermal_zone0/temp" if ($IsLinux) {
if (Test-Path "/sys/class/thermal/thermal_zone0/temp" -pathType leaf) {
[int]$IntTemp = Get-Content "/sys/class/thermal/thermal_zone0/temp"
$Temp = [math]::round($IntTemp / 1000.0, 1) $Temp = [math]::round($IntTemp / 1000.0, 1)
} else {
$data = Get-WMIObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2"
$Temp = @($data)[0].HighPrecisionTemperature
$Temp = [math]::round($Temp / 100.0, 1)
} }
if ($Temp -gt 80) { } else {
$Reply = "⚠️ CPU is too hot at $($Temp)°C!" $Objects = Get-WmiObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2"
foreach ($Obj in $Objects) {
$HiPrec = $Obj.HighPrecisionTemperature
$Temp = [math]::round($HiPrec / 100.0, 1)
}
}
return $Temp;
}
try {
$Temp = GetCPUTemperatureInCelsius
if ($Temp -eq 99999.9) {
"⚠️ CPU temperature query is unsupported."
} elseif ($Temp -gt 80) {
"⚠️ CPU is too hot at $($Temp)°C!"
} elseif ($Temp -gt 50) { } elseif ($Temp -gt 50) {
$Reply = "✅ CPU is $($Temp)°C hot." "✅ CPU is $($Temp)°C hot."
} elseif ($Temp -gt 0) { } elseif ($Temp -gt 0) {
$Reply = "✅ CPU is $($Temp)°C warm." "✅ CPU is $($Temp)°C warm."
} elseif ($Temp -gt -20) { } elseif ($Temp -gt -20) {
$Reply = "✅ CPU is $($Temp)°C cold." "✅ CPU is $($Temp)°C cold."
} else { } else {
$Reply = "⚠️ CPU is too cold at $($Temp)°C!" "⚠️ CPU is too cold at $($Temp)°C!"
} }
"$Reply"
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"