From e0b603f1b601279d7a2f312cca2752123b51340b Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Wed, 12 Oct 2022 14:58:26 +0200 Subject: [PATCH] Update check-cpu.ps1 --- Scripts/check-cpu.ps1 | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/Scripts/check-cpu.ps1 b/Scripts/check-cpu.ps1 index 645dd558..11f4a66d 100755 --- a/Scripts/check-cpu.ps1 +++ b/Scripts/check-cpu.ps1 @@ -12,27 +12,38 @@ Author: Markus Fleschutz | License: CC0 #> +function GetCPUTemperatureInCelsius { + $Temp = 99999.9 # unsupported + 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) + } + } else { + $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 { - 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) - } 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) { - $Reply = "⚠️ CPU is too hot at $($Temp)°C!" + $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) { - $Reply = "✅ CPU is $($Temp)°C hot." + "✅ CPU is $($Temp)°C hot." } elseif ($Temp -gt 0) { - $Reply = "✅ CPU is $($Temp)°C warm." + "✅ CPU is $($Temp)°C warm." } elseif ($Temp -gt -20) { - $Reply = "✅ CPU is $($Temp)°C cold." + "✅ CPU is $($Temp)°C cold." } else { - $Reply = "⚠️ CPU is too cold at $($Temp)°C!" + "⚠️ CPU is too cold at $($Temp)°C!" } - "$Reply" exit 0 # success } catch { "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"