【问题标题】:How to return a single integer from this WMIObject in PowerShell (Temperature)如何在 PowerShell(温度)中从此 WMIObject 返回单个整数
【发布时间】:2020-02-11 14:21:56
【问题描述】:
    $temps = Get-CIMInstance MSAcpi_ThermalZoneTemperature -Namespace "root/wmi"
    $temps | Select-Object -Property InstanceName,@{n="TempF";e={(($_.currenttemperature /10 -273.15) *1.8 +32)}}

我似乎无法像往常一样选择对象 -ExpandProperty 来获得 TempF 结果:

    InstanceName            TempF
    ------------            -----
    ACPI\ThermalZone\THM__0 77.09

只想要 77.09

如果能以摄氏度显示,则加分:)

【问题讨论】:

    标签: powershell get-wmiobject


    【解决方案1】:

    如果使用powershell 3或更高版本,可以使用对象的成员名来调用属性。

    例子:

    $temps.TempF 将返回77.09 的结果

    $tempt = Get-CimInstance MSAcpi_ThermalZoneTemperature -Namespace "root/wmi"
    $temps = ($tempt | Select-Object -Property InstanceName,@{n="TempF";e={(($_.currenttemperature /10 -273.15) *1.8 +32)}},@{n="TempC";e={($_.currenttemperature /10 -273.15)}})
    

    我还将 Celcius 添加到对象/变量 $temps

    您可以获取华氏温度$temps.tempF 或摄氏温度$temps.tempC

    【讨论】:

      【解决方案2】:
          $tempt = Get-CimInstance MSAcpi_ThermalZoneTemperature -Namespace "root/wmi"
          $tempF = ($tempt | Select-Object -Property InstanceName,@{n="TempF";e={(($_.currenttemperature /10 -273.15) *1.8 +32)}})|Select-Object -ExpandProperty TempF
      

      顿悟并想通了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-22
        • 2014-07-03
        • 2021-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-14
        相关资源
        最近更新 更多