You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell调用LibreHardwareMonitorLib.dll无法获取CPU温度与时钟速度

LibreHardwareMonitorLib.dll无法获取CPU温度/时钟速度的解决方法

问题描述

需通过PowerShell获取Windows系统指标(重点为温度),采用加载LibreHardwareMonitorLib.dll的方案,但仅能获取CPU负载,温度、时钟速度均无数据。运行LibreHardwareMonitor GUI及通过其服务WMI获取温度正常,但无法通过WMI cimv2获取温度。

原脚本:

cls
$dll = "LibreHardwareMonitorLib.dll"

Unblock-File -LiteralPath $dll
Add-Type -LiteralPath $dll
$monitor = [LibreHardwareMonitor.Hardware.Computer]::new()
$monitor.IsCPUEnabled = $true
$monitor.IsStorageEnabled = $true

$monitor.Open()
foreach ($sensor in $monitor.Hardware.Sensors) {
    write-host $sensor.Name
    write-host $sensor.SensorType
    write-host $sensor.Value
}
$monitor.Close()

原输出示例:

CPU Core #1 Thread #1
Load
99.38645
...
CPU Core #1
Temperature

CPU Core #1
Clock

问题原因

核心问题是未主动触发硬件数据更新:LibreHardwareMonitor不会自动采集温度、时钟这类动态传感器数据,必须调用硬件的Update()方法才能获取实时数值;原脚本仅遍历传感器但未执行数据刷新操作,仅Load类传感器默认有值。

修正后的脚本

cls
$dll = "LibreHardwareMonitorLib.dll"

# 确保DLL未被系统阻止
Unblock-File -LiteralPath $dll
# 加载LibreHardwareMonitor的类库
Add-Type -LiteralPath $dll

# 初始化监测对象,启用必要的硬件监测项
$monitor = [LibreHardwareMonitor.Hardware.Computer]::new()
$monitor.IsCPUEnabled = $true
$monitor.IsStorageEnabled = $true
$monitor.IsMemoryEnabled = $true
$monitor.IsMotherboardEnabled = $true

$monitor.Open()
try {
    # 遍历每个硬件并强制更新数据(这是获取温度/时钟的关键步骤)
    foreach ($hardware in $monitor.Hardware) {
        $hardware.Update()
        
        # 格式化输出传感器信息
        foreach ($sensor in $hardware.Sensors) {
            $value = if ($sensor.Value -ne $null) { $sensor.Value.ToString("F2") } else { "无数据" }
            Write-Host "[$($sensor.SensorType)] $($sensor.Name): $value"
        }
    }
}
finally {
    # 确保监测对象被正确关闭
    $monitor.Close()
}

关键修改点

  1. 添加硬件数据更新:遍历硬件时调用$hardware.Update(),触发传感器实时数据采集,这是获取温度、时钟数值的核心操作。
  2. 扩展硬件启用项:按需启用主板、内存等硬件监测,确保覆盖所需传感器类型。
  3. 优化输出格式:统一传感器信息展示形式,清晰区分有数据/无数据的情况。

额外注意事项

  • 需以管理员权限运行PowerShell,部分硬件传感器需要系统权限才能访问。
  • 确保使用的LibreHardwareMonitorLib.dll版本与系统架构(x86/x64)匹配。

内容的提问来源于stack exchange,提问作者iotaukm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 18:52:40