PowerShell/Docs/check-cpu.md

104 lines
2.8 KiB
Markdown
Raw Normal View History

2023-07-29 09:55:25 +02:00
## Script: *check-cpu.ps1*
2022-11-17 20:02:26 +01:00
check-cpu.ps1
2021-12-09 16:19:09 +01:00
## Parameters
```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 09:55:25 +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-05-26 12:20:18 +02:00
This PowerShell script queries the CPU status and prints it (name, type, speed, temperature, etc).
2022-11-17 20:02:26 +01:00
.EXAMPLE
PS> ./check-cpu
2023-05-26 12:20:18 +02:00
✅ AMD Ryzen 5 5500U with Radeon Graphics (CPU0, 2100MHz, 31.3°C)
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
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;
}
2023-05-26 12:20:18 +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-11-17 20:02:26 +01:00
try {
2023-05-26 12:20:18 +02:00
Write-Progress "⏳ Querying CPU details..."
$Status = "✅"
2022-11-17 20:02:26 +01:00
$Celsius = GetCPUTemperatureInCelsius
if ($Celsius -eq 99999.9) {
$Temp = "no temp"
} elseif ($Celsius -gt 50) {
2023-05-26 12:20:18 +02:00
$Temp = "$($Celsius)°C"
2023-07-29 09:45:37 +02:00
$Status = "⚠️"
2022-12-04 10:40:18 +01:00
} elseif ($Celsius -lt 0) {
2023-05-26 12:20:18 +02:00
$Temp = "$($Celsius)°C"
2023-07-29 09:45:37 +02:00
$Status = "⚠️"
2022-11-17 20:02:26 +01:00
} else {
2022-12-04 10:40:18 +01:00
$Temp = "$($Celsius)°C"
2022-11-17 20:02:26 +01:00
}
2023-05-26 12:20:18 +02:00
$Arch = GetProcessorArchitecture
2022-11-17 20:02:26 +01:00
if ($IsLinux) {
2023-05-26 12:20:18 +02:00
$CPUName = "$Arch CPU"
$Arch = ""
$DeviceID = ""
$Speed = ""
$Socket = ""
2022-11-17 20:02:26 +01:00
} else {
$Details = Get-WmiObject -Class Win32_Processor
2022-12-04 10:40:18 +01:00
$CPUName = $Details.Name.trim()
2023-05-26 12:20:18 +02:00
$Arch = "$Arch, "
$DeviceID = "$($Details.DeviceID), "
$Speed = "$($Details.MaxClockSpeed)MHz, "
$Socket = "$($Details.SocketDesignation) socket, "
2022-11-17 20:02:26 +01:00
}
2023-05-26 12:20:18 +02:00
$Cores = [System.Environment]::ProcessorCount
Write-Progress -completed "done."
Write-Host "$Status $CPUName ($($Arch)$Cores cores, $($DeviceID)$($Speed)$($Socket)$Temp)"
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
2023-07-29 09:55:25 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of check-cpu.ps1 as of 07/29/2023 09:55:09)*