powershell 在CMD/POWER Shell中获取CPU温度

cwtwac6a  于 2023-10-18  发布在  Shell
关注(0)|答案(7)|浏览(376)

在我的电脑里,我试图得到CPU的温度。在StackOverflow上搜索,我发现了这个:

C:\WINDOWS\system32>wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature

但我得到了这个错误:

Node - ADMIN
ERROR:
Description = Not supported
ohtdti5x

ohtdti5x1#

你可以使用这个代码:

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
uidvcgyl

uidvcgyl2#

您可以使用Open Hardware Monitor,它是一个开源软件(MPL v2)。您可以在这里访问命令行版本:
OpenHardwareMonitorReport.zip
输出的示例部分:

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)
  • Open Hardware Monitor Official website
  • 链接到命令行版本链接的问题:#776
  • 使用较新版本的挂起拉取请求:https://github.com/openhardwaremonitor/openhardwaremonitor/pull/1115#issuecomment-616230088
w3nuxt5m

w3nuxt5m3#

管理员身份在命令提示符下运行以下命令:
wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature
这将给你给予一些像这样的输出:
CurrentTemperature 3000 3010
但请确保您以管理员身份运行cmd

apeeds0o

apeeds0o4#

在我的笔记本电脑上面给了我错误的结果。只有这一个显示了CPU温度(摄氏度):

$data = Get-WMIObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2"
@($data)[0].HighPrecisionTemperature

我猜,每个CPU版本可能有不同的地方/公式来获得正确的CPU温度。

2wnc66cl

2wnc66cl5#

用新的传感器,或者用我现有的和海拔。它还显示了临界温度和百分比(摄氏度)它留下了一个文件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
cmssoen2

cmssoen26#

获取CPU温度的最稳定和可靠的方法是使用LibreHardwareMonitor项目中的DLL(它是OpenHardwareMonitor的超种子)。
下面是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
tkqqtvp1

tkqqtvp17#

根据the answer on this question
要获得CPU(和每个核心)的确切温度,你需要编写内核驱动程序,这要复杂得多。
CurrentTemperature返回主板上某个热区的温度。
这可以解释为什么这个页面上的一些答案返回一个温度,但它与实际的CPU温度相差很大。

相关问题