Update check-cpu.ps1

This commit is contained in:
Markus Fleschutz 2023-10-10 09:20:46 +02:00
parent 1756897ae9
commit 55f5e8984c

View File

@ -2,31 +2,31 @@
.SYNOPSIS .SYNOPSIS
Checks the CPU status Checks the CPU status
.DESCRIPTION .DESCRIPTION
This PowerShell script queries the CPU status and prints it (name, type, speed, temperature, etc). This PowerShell script queries the CPU status (name, type, speed, temperature, etc) and prints it.
.EXAMPLE .EXAMPLE
PS> ./check-cpu.ps1 PS> ./check-cpu.ps1
AMD Ryzen 5 5500U with Radeon Graphics (CPU0, 2100MHz, 31.3°C) Intel(R) Core(TM) i9-10900X CPU @ 3.70GHz (AMD64, 20 cores, CPU0, 3696MHz, CPU0 socket, 31.3°C)
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
function GetCPUTemperatureInCelsius { function GetProcessorTemperature {
$Temp = 99999.9 # unsupported $temp = 99999.9 # unsupported
if ($IsLinux) { if ($IsLinux) {
if (Test-Path "/sys/class/thermal/thermal_zone0/temp" -pathType leaf) { if (Test-Path "/sys/class/thermal/thermal_zone0/temp" -pathType leaf) {
[int]$IntTemp = Get-Content "/sys/class/thermal/thermal_zone0/temp" [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 { } else {
$Objects = Get-WmiObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2" $objects = Get-WmiObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2"
foreach ($Obj in $Objects) { foreach ($object in $objects) {
$HiPrec = $Obj.HighPrecisionTemperature $highPrec = $object.HighPrecisionTemperature
$Temp = [math]::round($HiPrec / 100.0, 1) $temp = [math]::round($highPrec / 100.0, 1)
} }
} }
return $Temp; return $temp
} }
function GetProcessorArchitecture { function GetProcessorArchitecture {
@ -44,39 +44,39 @@ function GetProcessorArchitecture {
} }
try { try {
Write-Progress "⏳ Querying CPU details..." Write-Progress "⏳ Querying CPU status... "
$Status = "" $status = ""
$Celsius = GetCPUTemperatureInCelsius $celsius = GetProcessorTemperature
if ($Celsius -eq 99999.9) { if ($celsius -eq 99999.9) {
$Temp = "no temp" $temp = "no temp"
} elseif ($Celsius -gt 50) { } elseif ($celsius -gt 50) {
$Temp = "$($Celsius)°C" $temp = "$($celsius)°C"
$Status = "⚠️" $status = "⚠️"
} elseif ($Celsius -lt 0) { } elseif ($celsius -lt 0) {
$Temp = "$($Celsius)°C" $temp = "$($celsius)°C"
$Status = "⚠️" $status = "⚠️"
} else { } else {
$Temp = "$($Celsius)°C" $temp = "$($celsius)°C"
} }
$Arch = GetProcessorArchitecture $arch = GetProcessorArchitecture
if ($IsLinux) { if ($IsLinux) {
$CPUName = "$Arch CPU" $cpuName = "$arch CPU"
$Arch = "" $arch = ""
$DeviceID = "" $deviceID = ""
$Speed = "" $speed = ""
$Socket = "" $socket = ""
} else { } else {
$Details = Get-WmiObject -Class Win32_Processor $details = Get-WmiObject -Class Win32_Processor
$CPUName = $Details.Name.trim() $cpuName = $details.Name.trim()
$Arch = "$Arch, " $arch = "$arch, "
$DeviceID = "$($Details.DeviceID), " $deviceID = "$($details.DeviceID), "
$Speed = "$($Details.MaxClockSpeed)MHz, " $speed = "$($details.MaxClockSpeed)MHz, "
$Socket = "$($Details.SocketDesignation) socket, " $socket = "$($details.SocketDesignation) socket, "
} }
$Cores = [System.Environment]::ProcessorCount $cores = [System.Environment]::ProcessorCount
Write-Progress -completed "done." Write-Progress -completed " "
Write-Host "$Status $CPUName ($($Arch)$Cores cores, $($DeviceID)$($Speed)$($Socket)$Temp)" Write-Host "$status $cpuName ($($arch)$cores cores, $($deviceID)$($speed)$($socket)$temp)"
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"