在我的计算机中,我试图获取 CPU 温度。在 StackOverflow 上搜索我发现了这个:
C:\WINDOWS\system32>wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature
但我收到此错误:
Node - ADMIN
ERROR:
Description = Not supported
您可以使用此代码:
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
您可以使用 Open Hardware Monitor,它是一个开源软件 (MPL v2)。 您可以在此处访问命令行版本:
输出示例部分:
PS C:\Users\myuser\OpenHardwareMonitorReport> .\OpenHardwareMonitorReport.exe
Open Hardware Monitor Report
--------------------------------------------------------------------------------
Version: 0.8.0.2
--------------------------------------------------------------------------------
Common Language Runtime: 4.0.30319.42000
Operating System: Microsoft Windows NT 6.2.9200.0
Process Type: 32-Bit
--------------------------------------------------------------------------------
Sensors
|
+- HP 00F52W (/mainboard)
|
+- Intel Core i7-3770 (/intelcpu/0)
| +- Bus Speed : 99.7734 99.7734 99.7784 (/intelcpu/0/clock/0)
| +- CPU Core #1 : 3691.62 3691.62 3791.58 (/intelcpu/0/clock/1)
| +- CPU Core #2 : 3691.62 3691.62 3791.58 (/intelcpu/0/clock/2)
| +- CPU Core #3 : 3791.39 3791.39 3891.36 (/intelcpu/0/clock/3)
| +- CPU Core #4 : 3691.62 3691.62 3891.36 (/intelcpu/0/clock/4)
| +- CPU Core #1 : 42 42 43 (/intelcpu/0/temperature/0)
| +- CPU Core #2 : 43 37 43 (/intelcpu/0/temperature/1)
| +- CPU Core #3 : 42 35 42 (/intelcpu/0/temperature/2)
| +- CPU Core #4 : 45 41 45 (/intelcpu/0/temperature/3)
| +- CPU Package : 45 43 45 (/intelcpu/0/temperature/4)
以管理员身份在命令提示符中运行以下命令:
wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature
这会给你一些像这样的输出:
CurrentTemperature 3000 3010
但请确保您以 管理员
身份运行 cmd在我的笔记本电脑上,上述所有结果都给了我错误的结果。只有这个以摄氏度显示 CPU 温度:
$data = Get-WMIObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2"
@($data)[0].HighPrecisionTemperature
我猜,每个 CPU 版本可能有不同的位置/公式来获取正确的 CPU 温度。
使用新的传感器,或者使用我拥有的传感器和高度。 它还显示临界温度和百分比(以摄氏度为单位) 它留下了一个文件Temperatures.txt以方便调试,以及带有来自传感器的序列化对象的xml
function Get-Temperature {
$TempFormat = "#"
$TempFile = "temperature"
$Command = 'Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace "root/wmi" ' + " > $pwd\$TempFile.txt"
$Command = 'Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace "root/wmi" ' + " | Export-Clixml $pwd\$TempFile.xml"
$p = Start-Process -Verb runas -FilePath "powershell" -ArgumentList $command -WorkingDirectory $pwd -PassThru
$p.WaitForExit()
$t = Import-Clixml pippo.xml
$returntemp = @()
foreach ($Sensor in $t)
{
$Active = if($sensor.Active){"On "}else{"Off"}
$temp = $Sensor.CurrentTemperature
$Critical = $Sensor.CriticalTripPoint
$currentTempKelvin = $temp / 10
$currentTempCelsius = $currentTempKelvin - 273.15
$currentTempFahrenheit = (9/5) * $currentTempCelsius + 32
$StrKelvin = $currentTempKelvin.ToString($TempFormat).PadLeft(3, " ")
$StrCelsius = $currentTempCelsius.ToString($TempFormat).PadLeft(3, " ")
$StrFahrenheit = $currentTempFahrenheit.ToString($TempFormat).PadLeft(3, " ")
$CriticalKelvin = $Critical / 10
$CriticalCelsius = $CriticalKelvin - 273.15
$CriticalFahrenheit = (9/5) * $CriticalCelsius + 32
$StrCritKelvin = $CriticalKelvin.ToString($TempFormat).PadRight(3, " ")
$StrCritCelsius = $CriticalCelsius.ToString($TempFormat).PadRight(3, " ")
$StrCritFahrenheit = $CriticalFahrenheit.ToString($TempFormat).PadRight(3, " ")
$PerCrit = ($currentTempCelsius/$CriticalCelsius * 100)
$StrPerCrit = $PerCrit.ToString($TempFormat).PadLeft(3, " ")
$returntemp += "$Active $StrPerCrit% $StrCelsius/$StrCritCelsius C : $StrFahrenheit/$StrCritFahrenheit F : $StrKelvin/$StrCritKelvin K - " + $Sensor.InstanceName
}
return $returntemp
}
Get-Temperature
获取 CPU 温度的最稳定、最可靠的方法是使用 LibreHardwareMonitor 项目(其取代 OpenHardwareMonitor)中的 DLL。
这是 Powershell 中的代码片段:
# script to get the current CPU temperature, needs to run as admin/system
# requires an external DLL from the GitHub-Project "LibreHardwareMonitor"
# on first execution the script downloads the DLL from the gitHub-Project
# [email protected]
#Requires -RunAsAdministrator
cls
$dll = "$env:windir\system32\LibreHardwareMonitorLib.dll"
$storeDll = $true
if (!(Test-Path $dll)) {
$web = [System.Net.WebClient]::new()
# download the package:
$url = "https://github.com/LibreHardwareMonitor/LibreHardwareMonitor/releases/download/v0.9.2/LibreHardwareMonitor-net472.zip"
$zip = $web.DownloadData($url)
# extract the dll:
Add-Type -AssemblyName System.IO.Compression
$stream = [System.IO.Memorystream]::new()
$stream.Write($zip,0,$zip.Length)
$archive = [System.IO.Compression.ZipArchive]::new($stream)
$entry = $archive.GetEntry('LibreHardwareMonitorLib.dll')
$bytes = [byte[]]::new($entry.Length)
[void]$entry.Open().Read($bytes, 0, $bytes.Length)
# check MD5:
$md5 = [Security.Cryptography.MD5CryptoServiceProvider]::new().ComputeHash($bytes)
$hash = [string]::Concat($md5.foreach{$_.ToString("x2")})
if ($hash -ne 'd4a02aa7bd43a5c5ffd3d673bb20b54d') {break}
# save the dll:
if ($storeDll) {
[System.IO.File]::WriteAllBytes($dll, $bytes)
Unblock-File -LiteralPath $dll
}
} else {
$bytes = [System.IO.File]::ReadAllBytes($dll)
if (!$storeDll) {Remove-Item $dll -Force}
}
Add-Type -LiteralPath $dll
$monitor = [LibreHardwareMonitor.Hardware.Computer]::new()
$monitor.IsCPUEnabled = $true
$monitor.Open()
foreach ($sensor in $monitor.Hardware.Sensors) {
if ($sensor.SensorType -eq 'Temperature' -and $sensor.Name -eq 'CPU Package'){
$temp = $sensor.Value
break
}
}
$monitor.Close()
write-host "CPU-Package Temperature = $temp°C" -f y
根据这个问题的答案:
这可以解释为什么本页上的一些答案会返回温度,但它与实际的 CPU 温度有很大不同。要获得 CPU(和每个核心)的准确温度,您需要编写 内核驱动程序, 更复杂的事情。
CurrentTemperature 返回某个热区的温度,即主板上的某个地方。