Get CPU temperature in CMD/POWER Shell

眉间皱痕 提交于 2020-05-08 04:45:08

问题


In my computer I am trying to get the CPU temperature. Searching on StackOverflow I found this:

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

But I get this error:

Node - ADMIN
ERROR:
Description = Not supported

回答1:


you can use this code :

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



回答2:


You can get the CPU temp in both WMI and Openhardwaremonitor way.

Open Hardwaremonitor:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenHardwareMonitor.Hardware;
namespace Get_CPU_Temp5
{
   class Program
   {
       public class UpdateVisitor : IVisitor
       {
           public void VisitComputer(IComputer computer)
           {
               computer.Traverse(this);
           }
           public void VisitHardware(IHardware hardware)
           {
               hardware.Update();
               foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
           }
           public void VisitSensor(ISensor sensor) { }
           public void VisitParameter(IParameter parameter) { }
       }
       static void GetSystemInfo()
       {
           UpdateVisitor updateVisitor = new UpdateVisitor();
           Computer computer = new Computer();
           computer.Open();
           computer.CPUEnabled = true;
           computer.Accept(updateVisitor);
           for (int i = 0; i < computer.Hardware.Length; i++)
           {
               if (computer.Hardware[i].HardwareType == HardwareType.CPU)
               {
                   for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                   {
                       if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                               Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r");
                   }
               }
           }
           computer.Close();
       }
       static void Main(string[] args)
       {
           while (true)
           {
               GetSystemInfo();
           }
       }
   }
}

WMI:

using System;
using System.Diagnostics;
using System.Management;
class Program
{
   static void Main(string[] args)
   {
       Double CPUtprt = 0;
       System.Management.ManagementObjectSearcher mos = new System.Management.ManagementObjectSearcher(@"root\WMI", "Select * From MSAcpi_ThermalZoneTemperature");
       foreach (System.Management.ManagementObject mo in mos.Get())
       {
           CPUtprt = Convert.ToDouble(Convert.ToDouble(mo.GetPropertyValue("CurrentTemperature").ToString()) - 2732) / 10;
          Console.WriteLine("CPU temp : " + CPUtprt.ToString() + " °C");
       }
   }
}

I found a nice tutorial here, I get the CPU temp successfully.

http://www.lattepanda.com/topic-f11t3004.html




回答3:


With new sensor, or with what I have and with elevation. It also shows critical temperature and percentage (in Celsius) It leaves a file Temperatures.txt for easy debugging, and the xml with serialized object from sensors

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



回答4:


I added a little formatting to saftargholi's answer:

function Get-Temperature {
$t = Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace "root/wmi"
$returntemp = @()

foreach ($temp in $t)
{

$currentDevice = $temp.InstanceName
$currentTempKelvin =  ($temp.CurrentTemperature / 10).ToString().Padleft(6);

$currentTempCelsius = ($currentTempKelvin - 273.15).ToString("f4").Padleft(12);

$currentTempFahrenheit = ((9/5) * $currentTempCelsius + 32).ToString().Padleft(8);

$returntemp += $currentDevice +  [char]9 + $currentTempCelsius.ToString() + " C : " + [char]9 + $currentTempFahrenheit.ToString() + " F : " + [char]9 + $currentTempKelvin.ToString() + "K"  
}
return $returntemp

}




回答5:


You can use Open Hardware Monitor it's an open source software (MPL v2). You can access the command line version here:

OpenHardwareMonitorReport.zip

Example part of the output:

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
  • link to issue where the command line version is linked: #776
  • Pending pull request with more recent version: https://github.com/openhardwaremonitor/openhardwaremonitor/pull/1115#issuecomment-616230088


来源:https://stackoverflow.com/questions/39738494/get-cpu-temperature-in-cmd-power-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!