PowerShell/docs/check-cpu.md

110 lines
3.0 KiB
Markdown
Raw Permalink Normal View History

2024-11-08 12:38:20 +01:00
The *check-cpu.ps1* Script
===========================
2022-11-17 20:02:26 +01:00
check-cpu.ps1
2021-12-09 16:19:09 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-12-09 16:19:09 +01:00
```powershell
2022-11-17 19:46:02 +01:00
2021-12-09 16:19:09 +01:00
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2023-05-26 12:20:18 +02:00
Checks the CPU status
2022-11-17 20:02:26 +01:00
.DESCRIPTION
2023-10-19 08:12:00 +02:00
This PowerShell script queries the CPU status (name, type, speed, temperature, etc) and prints it.
2022-11-17 20:02:26 +01:00
.EXAMPLE
2023-08-06 21:36:33 +02:00
PS> ./check-cpu.ps1
2023-12-07 20:24:45 +01:00
✅ Intel(R) Core(TM) i9-10900X CPU @ 3.70GHz (AMD64, 20 cores, CPU0, 3696MHz, CPU0 socket, 31.3°C)
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-12-07 20:24:45 +01:00
function GetCPUArchitecture {
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" }
} elseif ([System.Environment]::Is64BitOperatingSystem) { return "64-bit" } else { return "32-bit" }
}
}
function GetCPUTemperature {
2023-10-19 08:12:00 +02:00
$temp = 99999.9 # unsupported
2022-11-17 20:02:26 +01: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-19 08:12:00 +02:00
$temp = [math]::round($IntTemp / 1000.0, 1)
2022-11-17 20:02:26 +01:00
}
} else {
2023-10-19 08:12:00 +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-11-17 20:02:26 +01:00
}
}
2023-10-19 08:12:00 +02:00
return $temp
2022-11-17 20:02:26 +01:00
}
try {
2024-08-15 09:51:46 +02:00
Write-Progress "Querying CPU status..."
2023-10-19 08:12:00 +02:00
$status = "✅"
2023-12-07 20:24:45 +01:00
$arch = GetCPUArchitecture
2022-11-17 20:02:26 +01:00
if ($IsLinux) {
2023-10-19 08:12:00 +02:00
$cpuName = "$arch CPU"
$arch = ""
$deviceID = ""
$speed = ""
$socket = ""
2022-11-17 20:02:26 +01:00
} else {
2023-10-19 08:12:00 +02:00
$details = Get-WmiObject -Class Win32_Processor
$cpuName = $details.Name.trim()
$arch = "$arch, "
2024-11-08 12:35:11 +01:00
$deviceID = ", $($details.DeviceID)"
$speed = ", $($details.MaxClockSpeed)MHz"
$socket = ", $($details.SocketDesignation) socket"
2022-11-17 20:02:26 +01:00
}
2023-10-19 08:12:00 +02:00
$cores = [System.Environment]::ProcessorCount
2023-12-07 20:24:45 +01:00
$celsius = GetCPUTemperature
if ($celsius -eq 99999.9) {
2024-11-08 12:35:11 +01:00
$temp = ""
} elseif ($celsius -gt 80) {
$temp = ", $($celsius)°C TOO HOT"
$status = "⚠️"
2023-12-07 20:24:45 +01:00
} elseif ($celsius -gt 50) {
2024-11-08 12:35:11 +01:00
$temp = ", $($celsius)°C HOT"
2023-12-07 20:24:45 +01:00
$status = "⚠️"
} elseif ($celsius -lt 0) {
2024-11-08 12:35:11 +01:00
$temp = ", $($celsius)°C TOO COLD"
2023-12-07 20:24:45 +01:00
$status = "⚠️"
2024-11-08 12:35:11 +01:00
} elseif ($celsius -lt 30) {
$temp = ", $($celsius)°C cool"
2023-12-07 20:24:45 +01:00
} else {
2024-11-08 12:35:11 +01:00
$temp = ", $($celsius)°C"
2023-12-07 20:24:45 +01:00
}
Write-Progress -completed "Done."
2024-11-08 12:35:11 +01:00
Write-Host "$status $cpuName ($($arch)$cores cores$($temp)$($deviceID)$($speed)$($socket))"
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:51)*