PowerShell/scripts/check-cpu.ps1

85 lines
2.4 KiB
PowerShell
Raw Normal View History

2023-10-31 11:25:11 +01: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-10-10 09:20:46 +02:00
This PowerShell script queries the CPU status (name, type, speed, temperature, etc) and prints it.
2021-07-13 21:10:02 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./check-cpu.ps1
2023-10-10 09:20:46 +02:00
Intel(R) Core(TM) i9-10900X CPU @ 3.70GHz (AMD64, 20 cores, CPU0, 3696MHz, CPU0 socket, 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
#>
2023-10-10 09:20:46 +02:00
function GetProcessorTemperature {
$temp = 99999.9 # unsupported
2022-10-12 14:58:26 +02:00
if ($IsLinux) {
if (Test-Path "/sys/class/thermal/thermal_zone0/temp" -pathType leaf) {
[int]$IntTemp = Get-Content "/sys/class/thermal/thermal_zone0/temp"
2023-10-10 09:20:46 +02:00
$temp = [math]::round($IntTemp / 1000.0, 1)
2022-10-12 14:58:26 +02:00
}
2021-03-20 16:13:54 +01:00
} else {
2023-10-10 09:20:46 +02:00
$objects = Get-WmiObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2"
foreach ($object in $objects) {
$highPrec = $object.HighPrecisionTemperature
$temp = [math]::round($highPrec / 100.0, 1)
2022-10-12 14:58:26 +02:00
}
2021-03-20 16:13:54 +01:00
}
2023-10-10 09:20:46 +02:00
return $temp
2022-10-12 14:58:26 +02:00
}
2023-04-27 14:40:00 +02:00
function GetProcessorArchitecture {
if ("$env:PROCESSOR_ARCHITECTURE" -ne "") { return "$env:PROCESSOR_ARCHITECTURE" }
if ($IsLinux) {
$Name = $PSVersionTable.OS
if ($Name -like "*-generic *") {
if ([System.Environment]::Is64BitOperatingSystem) { return "x64" } else { return "x86" }
} elseif ($Name -like "*-raspi *") {
if ([System.Environment]::Is64BitOperatingSystem) { return "ARM64" } else { return "ARM32" }
} else {
return ""
}
}
}
2022-10-12 14:58:26 +02:00
try {
Write-Progress "Querying CPU status... "
2023-10-10 09:20:46 +02:00
$status = ""
$celsius = GetProcessorTemperature
if ($celsius -eq 99999.9) {
$temp = "no temp"
} elseif ($celsius -gt 50) {
$temp = "$($celsius)°C"
$status = "⚠️"
} elseif ($celsius -lt 0) {
$temp = "$($celsius)°C"
$status = "⚠️"
2021-03-20 16:23:49 +01:00
} else {
2023-10-10 09:20:46 +02:00
$temp = "$($celsius)°C"
2022-10-17 19:24:48 +02:00
}
2022-10-25 19:15:01 +02:00
2023-10-10 09:20:46 +02:00
$arch = GetProcessorArchitecture
2022-10-25 19:15:01 +02:00
if ($IsLinux) {
2023-10-10 09:20:46 +02:00
$cpuName = "$arch CPU"
$arch = ""
$deviceID = ""
$speed = ""
$socket = ""
2022-10-25 19:15:01 +02:00
} else {
2023-10-10 09:20:46 +02:00
$details = Get-WmiObject -Class Win32_Processor
$cpuName = $details.Name.trim()
$arch = "$arch, "
$deviceID = "$($details.DeviceID), "
$speed = "$($details.MaxClockSpeed)MHz, "
$socket = "$($details.SocketDesignation) socket, "
2022-10-25 19:15:01 +02:00
}
2023-10-10 09:20:46 +02:00
$cores = [System.Environment]::ProcessorCount
Write-Progress -completed " "
Write-Host "$status $cpuName ($($arch)$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
}