編碼的世界 / 優質文選 / 財富

Windows系統下獲取主板各區域溫度的命令


2022年5月12日
-   

Windows下的WMI裏提供了MSAcpi_ThermalZoneTemperature接口的,調用一下即可。 WMI即Windows Management Instrumentation,是一個Windows管理工具/模塊,可以從這裏獲得大部分你需要的系統信息。
 
1. 以管理員身份啟動powershell,輸入以下命令:
function Get-Temperature {
$t = Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace "root/wmi"
$returntemp = @()
foreach ($temp in $t.CurrentTemperature)
{
$currentTempKelvin = $temp/10
$currentTempCelsius = $currentTempKelvin - 273.15
$currentTempFahrenheit = (9/5) * $currentTempCelsius + 32
$returntemp += $currentTempCelsius.ToString() + " C : " + $currentTempFahrenheit.ToString() + " F : " + $currentTempKelvin + "K"
}
return $returntemp
}
Get-Temperature

 結果如下(因為我的電腦主板有8個溫區,所以顯示了8個溫度):

 
2. 這樣堆積顯示的比較亂,如果想單獨看主板某個區域的溫度,可先通過如下命令獲取主板區域各接口的 InstanceName 信息:
Get-CimInstance -Namespace root/WMI -ClassName MSAcpi_ThermalZoneTemperature

 劃紅線處這就是主板各區域接口的 InstanceName (CPUZ_0就代表CPU Zone): 
 再通過如下代碼獲取該區域溫度(記得替換對應的InstanceName):
"CPU: $(((Get-CimInstance -Namespace root/WMI -ClassName MSAcpi_ThermalZoneTemperature | where InstanceName -eq "ACPIThermalZoneCPUZ_0").CurrentTemperature - 2731.5) / 10) C"

結果如下(CPU區域的CPUZ_0當前溫度為75.05℃) :

 
 
不過上述方法都只能獲取主板各區域的溫度,無法獲得確切的CPU溫度。 主要是因為CPU溫度等信息在MSR寄存器或PCI配置空間中,而讀取MSR的兩條匯編指令:WRMSR 和RDMSR都是特權指令,普通應用層面無法使用該指令。不嫌麻煩的話,可以寫個適配當前電腦主板硬件的底層驅動,來獲取電腦的CPU溫度信息 其實這都是Windows的歷史遺留問題,Windows設計之初就沒想著像Linux一樣給CPU溫度留API調用接口.. 所以現在只能湊合著看個主板溫區了,除非有精力像魯大師那種去寫適配硬件的底層驅動

熱門文章