PowerShell/Scripts/check-cpu.ps1

84 lines
2.2 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2023-04-11 10:48:59 +02:00
Checks the CPU status
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2023-04-11 10:48:59 +02:00
This PowerShell script queries the CPU status and prints it (name, type, speed, temperature, etc).
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-12-01 10:23:15 +01:00
PS> ./check-cpu
2022-12-13 08:27:19 +01:00
AMD Ryzen 5 5500U with Radeon Graphics (CPU0, 2100MHz, 31.3°C)
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2022-05-31 14:38:34 +02:00
Author: Markus Fleschutz | License: CC0
2021-03-20 16:13:54 +01:00
#>
2022-10-12 14:58:26 +02:00
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)
}
2021-03-20 16:13:54 +01:00
} else {
2022-10-12 14:58:26 +02:00
$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)
}
2021-03-20 16:13:54 +01:00
}
2022-10-12 14:58:26 +02:00
return $Temp;
}
try {
2023-01-23 14:04:23 +01:00
Write-Progress "⏳ Querying CPU details..."
2022-12-12 19:02:32 +01:00
$Status = ""
2022-10-17 19:24:48 +02:00
$Celsius = GetCPUTemperatureInCelsius
if ($Celsius -eq 99999.9) {
2022-10-25 19:15:01 +02:00
$Temp = "no temp"
2022-10-17 19:24:48 +02:00
} elseif ($Celsius -gt 50) {
2022-12-12 19:02:32 +01:00
$Temp = "$($Celsius)°C"
$Status = ""
2022-12-04 09:57:36 +01:00
} elseif ($Celsius -lt 0) {
2022-12-12 19:02:32 +01:00
$Temp = "$($Celsius)°C"
$Status = ""
2021-03-20 16:23:49 +01:00
} else {
2022-12-04 09:57:36 +01:00
$Temp = "$($Celsius)°C"
2022-10-17 19:24:48 +02:00
}
2022-10-25 19:15:01 +02:00
if ($IsLinux) {
$Name = $PSVersionTable.OS
if ($Name -like "*-generic *") {
2023-01-13 18:06:21 +01:00
if ([System.Environment]::Is64BitOperatingSystem) {
2023-01-18 20:47:12 +01:00
$Arch = "x64"
2023-01-13 18:06:21 +01:00
} else {
2023-01-18 20:47:12 +01:00
$Arch = "x86"
2023-01-13 18:06:21 +01:00
}
} elseif ($Name -like "*-raspi *") {
2023-01-13 18:06:21 +01:00
if ([System.Environment]::Is64BitOperatingSystem) {
$Arch = "ARM64"
} else {
$Arch = "ARM32"
}
} else {
$Arch = ""
}
2023-01-13 18:06:21 +01:00
$CPUName = "$Arch CPU"
2022-12-28 17:49:03 +01:00
$DeviceID = ""
$Speed = ""
$Socket = ""
2022-10-25 19:15:01 +02:00
} else {
$Details = Get-WmiObject -Class Win32_Processor
2022-12-04 09:57:36 +01:00
$CPUName = $Details.Name.trim()
2022-12-28 17:49:03 +01:00
$DeviceID = "$($Details.DeviceID), "
$Speed = "$($Details.MaxClockSpeed)MHz, "
$Socket = "$($Details.SocketDesignation) socket, "
2022-10-25 19:15:01 +02:00
}
2022-12-28 17:49:03 +01:00
$Cores = [System.Environment]::ProcessorCount
Write-Progress -completed "done."
2022-12-28 17:49:03 +01:00
Write-Host "$Status $CPUName ($Cores cores, $($DeviceID)$($Speed)$($Socket)$Temp)"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-03-20 16:13:54 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-03-20 16:13:54 +01:00
exit 1
2022-12-12 19:02:32 +01:00
}